2020-04-22 13:31:44 +00:00
|
|
|
use async_graphql::*;
|
2020-10-10 02:32:43 +00:00
|
|
|
use std::collections::HashMap;
|
2020-04-22 13:31:44 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-04-22 13:31:44 +00:00
|
|
|
pub async fn test_variables() {
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-04-22 13:31:44 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-04-22 13:31:44 +00:00
|
|
|
pub async fn int_val(&self, value: i32) -> i32 {
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn int_list_val(&self, value: Vec<i32>) -> Vec<i32> {
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
2020-09-10 11:35:48 +00:00
|
|
|
let query = Request::new(
|
2020-04-22 13:31:44 +00:00
|
|
|
r#"
|
|
|
|
query QueryWithVariables($intVal: Int!, $intListVal: [Int!]!) {
|
|
|
|
intVal(value: $intVal)
|
|
|
|
intListVal(value: $intListVal)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
2020-10-12 02:17:05 +00:00
|
|
|
.variables(Variables::from_value(value!({
|
2020-09-08 08:30:29 +00:00
|
|
|
"intVal": 10,
|
|
|
|
"intListVal": [1, 2, 3, 4, 5],
|
|
|
|
})));
|
2020-09-10 11:35:48 +00:00
|
|
|
|
2020-04-22 13:31:44 +00:00
|
|
|
assert_eq!(
|
2020-09-10 11:35:48 +00:00
|
|
|
schema.execute(query).await.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-04-22 13:31:44 +00:00
|
|
|
"intVal": 10,
|
|
|
|
"intListVal": [1, 2, 3, 4, 5],
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-04-22 13:31:44 +00:00
|
|
|
pub async fn test_variable_default_value() {
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-04-22 13:31:44 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-04-22 13:31:44 +00:00
|
|
|
pub async fn int_val(&self, value: i32) -> i32 {
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
2020-09-10 11:35:48 +00:00
|
|
|
assert_eq!(
|
|
|
|
schema
|
|
|
|
.execute(
|
|
|
|
r#"
|
2020-04-22 13:31:44 +00:00
|
|
|
query QueryWithVariables($intVal: Int = 10) {
|
|
|
|
intVal(value: $intVal)
|
|
|
|
}
|
2020-09-10 11:35:48 +00:00
|
|
|
"#
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-04-22 13:31:44 +00:00
|
|
|
"intVal": 10,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-05-31 03:54:07 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-06-11 15:00:47 +00:00
|
|
|
pub async fn test_variable_no_value() {
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-06-11 15:00:47 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-06-11 15:00:47 +00:00
|
|
|
pub async fn int_val(&self, value: Option<i32>) -> i32 {
|
|
|
|
value.unwrap_or(10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
2020-09-10 11:35:48 +00:00
|
|
|
let resp = schema
|
|
|
|
.execute(Request::new(
|
|
|
|
r#"
|
2020-06-11 15:00:47 +00:00
|
|
|
query QueryWithVariables($intVal: Int) {
|
|
|
|
intVal(value: $intVal)
|
|
|
|
}
|
|
|
|
"#,
|
2020-09-10 11:35:48 +00:00
|
|
|
))
|
|
|
|
.await
|
2020-09-10 23:58:02 +00:00
|
|
|
.into_result()
|
2020-09-10 11:35:48 +00:00
|
|
|
.unwrap();
|
2020-06-11 15:00:47 +00:00
|
|
|
assert_eq!(
|
|
|
|
resp.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-06-11 15:00:47 +00:00
|
|
|
"intVal": 10,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-06-11 15:00:47 +00:00
|
|
|
pub async fn test_variable_null() {
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-06-11 15:00:47 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-06-11 15:00:47 +00:00
|
|
|
pub async fn int_val(&self, value: Option<i32>) -> i32 {
|
|
|
|
value.unwrap_or(10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
2020-09-10 11:35:48 +00:00
|
|
|
let query = Request::new(
|
2020-06-11 15:00:47 +00:00
|
|
|
r#"
|
|
|
|
query QueryWithVariables($intVal: Int) {
|
|
|
|
intVal(value: $intVal)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
2020-10-12 02:17:05 +00:00
|
|
|
.variables(Variables::from_value(value!({
|
2020-09-08 08:30:29 +00:00
|
|
|
"intVal": null,
|
|
|
|
})));
|
2020-09-10 11:35:48 +00:00
|
|
|
let resp = schema.execute(query).await;
|
2020-06-11 15:00:47 +00:00
|
|
|
assert_eq!(
|
|
|
|
resp.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-06-11 15:00:47 +00:00
|
|
|
"intVal": 10,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-05-31 03:54:07 +00:00
|
|
|
pub async fn test_variable_in_input_object() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject)]
|
2020-05-31 03:54:07 +00:00
|
|
|
struct MyInput {
|
|
|
|
value: i32,
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-05-31 03:54:07 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-05-31 03:54:07 +00:00
|
|
|
async fn test(&self, input: MyInput) -> i32 {
|
|
|
|
input.value
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn test2(&self, input: Vec<MyInput>) -> i32 {
|
|
|
|
input.iter().map(|item| item.value).sum()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Mutation;
|
2020-05-31 03:54:07 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Mutation {
|
2020-05-31 03:54:07 +00:00
|
|
|
async fn test(&self, input: MyInput) -> i32 {
|
|
|
|
input.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, Mutation, EmptySubscription);
|
2020-05-31 03:54:07 +00:00
|
|
|
|
|
|
|
// test query
|
|
|
|
{
|
|
|
|
let query = r#"
|
|
|
|
query TestQuery($value: Int!) {
|
|
|
|
test(input: {value: $value })
|
|
|
|
}"#;
|
2020-09-10 11:35:48 +00:00
|
|
|
let resp = schema
|
2020-10-12 02:17:05 +00:00
|
|
|
.execute(Request::new(query).variables(Variables::from_value(value!({
|
|
|
|
"value": 10,
|
|
|
|
}))))
|
2020-09-10 11:35:48 +00:00
|
|
|
.await;
|
2020-05-31 03:54:07 +00:00
|
|
|
assert_eq!(
|
|
|
|
resp.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-05-31 03:54:07 +00:00
|
|
|
"test": 10,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test query2
|
|
|
|
{
|
|
|
|
let query = r#"
|
|
|
|
query TestQuery($value: Int!) {
|
|
|
|
test2(input: [{value: $value }, {value: $value }])
|
|
|
|
}"#;
|
2020-09-10 11:35:48 +00:00
|
|
|
let resp = schema
|
2020-10-12 02:17:05 +00:00
|
|
|
.execute(Request::new(query).variables(Variables::from_value(value!({
|
|
|
|
"value": 3,
|
|
|
|
}))))
|
2020-09-10 11:35:48 +00:00
|
|
|
.await;
|
2020-05-31 03:54:07 +00:00
|
|
|
assert_eq!(
|
|
|
|
resp.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-05-31 03:54:07 +00:00
|
|
|
"test2": 6,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test mutation
|
|
|
|
{
|
|
|
|
let query = r#"
|
|
|
|
mutation TestMutation($value: Int!) {
|
|
|
|
test(input: {value: $value })
|
|
|
|
}"#;
|
2020-09-10 11:35:48 +00:00
|
|
|
let resp = schema
|
2020-10-12 02:17:05 +00:00
|
|
|
.execute(Request::new(query).variables(Variables::from_value(value!({
|
|
|
|
"value": 10,
|
|
|
|
}))))
|
2020-09-10 11:35:48 +00:00
|
|
|
.await;
|
2020-05-31 03:54:07 +00:00
|
|
|
assert_eq!(
|
|
|
|
resp.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-05-31 03:54:07 +00:00
|
|
|
"test": 10,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 05:28:18 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-09-20 05:28:18 +00:00
|
|
|
pub async fn test_variables_enum() {
|
|
|
|
#[derive(Enum, Eq, PartialEq, Copy, Clone)]
|
|
|
|
enum MyEnum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-09-20 05:28:18 +00:00
|
|
|
|
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-09-20 05:28:18 +00:00
|
|
|
pub async fn value(&self, value: MyEnum) -> i32 {
|
|
|
|
match value {
|
|
|
|
MyEnum::A => 1,
|
|
|
|
MyEnum::B => 2,
|
|
|
|
MyEnum::C => 3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
2020-09-20 05:28:18 +00:00
|
|
|
let query = Request::new(
|
|
|
|
r#"
|
|
|
|
query QueryWithVariables($value1: MyEnum, $value2: MyEnum, $value3: MyEnum) {
|
|
|
|
a: value(value: $value1)
|
|
|
|
b: value(value: $value2)
|
|
|
|
c: value(value: $value3)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
2020-10-12 02:17:05 +00:00
|
|
|
.variables(Variables::from_value(value!({
|
2020-09-20 05:28:18 +00:00
|
|
|
"value1": "A",
|
|
|
|
"value2": "B",
|
|
|
|
"value3": "C",
|
|
|
|
})));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
schema.execute(query).await.into_result().unwrap().data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-09-20 05:28:18 +00:00
|
|
|
"a": 1,
|
|
|
|
"b": 2,
|
|
|
|
"c": 3,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-10-10 02:32:43 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-10-10 02:32:43 +00:00
|
|
|
pub async fn test_variables_json() {
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-10-10 02:32:43 +00:00
|
|
|
|
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-10-10 02:32:43 +00:00
|
|
|
pub async fn value(&self, value: Json<HashMap<String, i32>>) -> i32 {
|
|
|
|
*value.get("a-b").unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
2020-10-10 02:32:43 +00:00
|
|
|
let query = Request::new(
|
|
|
|
r#"
|
|
|
|
query QueryWithVariables($value: JSON) {
|
|
|
|
value(value: $value)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
2020-10-12 02:17:05 +00:00
|
|
|
.variables(Variables::from_value(value!({
|
2020-10-10 02:32:43 +00:00
|
|
|
"value": { "a-b": 123 },
|
|
|
|
})));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
schema.execute(query).await.into_result().unwrap().data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-10-10 02:32:43 +00:00
|
|
|
"value": 123,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|