2020-03-07 12:54:03 +00:00
|
|
|
use async_graphql::*;
|
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-03-07 12:54:03 +00:00
|
|
|
pub async fn test_input_object_default_value() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject)]
|
2020-03-07 12:54:03 +00:00
|
|
|
struct MyInput {
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(default = 999)]
|
2020-03-07 12:54:03 +00:00
|
|
|
a: i32,
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(default_with = "vec![1, 2, 3]")]
|
2020-03-07 12:54:03 +00:00
|
|
|
b: Vec<i32>,
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(default = "abc")]
|
2020-03-07 12:54:03 +00:00
|
|
|
c: String,
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(default = 999)]
|
2020-05-26 10:34:43 +00:00
|
|
|
d: i32,
|
2020-03-07 12:54:03 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(default = 999)]
|
2020-05-26 10:34:43 +00:00
|
|
|
e: i32,
|
2020-03-07 12:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct MyOutput {
|
|
|
|
a: i32,
|
|
|
|
b: Vec<i32>,
|
|
|
|
c: String,
|
|
|
|
d: Option<i32>,
|
|
|
|
e: Option<i32>,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-03-07 12:54:03 +00:00
|
|
|
impl MyOutput {
|
|
|
|
async fn a(&self) -> i32 {
|
|
|
|
self.a
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn b(&self) -> &Vec<i32> {
|
|
|
|
&self.b
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn c(&self) -> &String {
|
|
|
|
&self.c
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn d(&self) -> &Option<i32> {
|
|
|
|
&self.d
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn e(&self) -> &Option<i32> {
|
|
|
|
&self.e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Root;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-03-07 12:54:03 +00:00
|
|
|
impl Root {
|
|
|
|
async fn a(&self, input: MyInput) -> MyOutput {
|
|
|
|
MyOutput {
|
|
|
|
a: input.a,
|
|
|
|
b: input.b,
|
|
|
|
c: input.c,
|
2020-05-26 10:34:43 +00:00
|
|
|
d: Some(input.d),
|
|
|
|
e: Some(input.e),
|
2020-03-07 12:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
let schema = Schema::new(Root, EmptyMutation, EmptySubscription);
|
2020-09-01 01:10:12 +00:00
|
|
|
let query = r#"{
|
|
|
|
a(input:{e:777}) {
|
2020-03-07 12:54:03 +00:00
|
|
|
a b c d e
|
2020-09-01 01:10:12 +00:00
|
|
|
}
|
|
|
|
}"#
|
2020-08-31 18:18:02 +00:00
|
|
|
.to_owned();
|
2020-03-07 12:54:03 +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-03-07 12:54:03 +00:00
|
|
|
"a": {
|
|
|
|
"a": 999,
|
|
|
|
"b": [1, 2, 3],
|
|
|
|
"c": "abc",
|
|
|
|
"d": 999,
|
2020-03-09 01:33:36 +00:00
|
|
|
"e": 777,
|
2020-03-07 12:54:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-04-08 04:02:48 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-04-08 04:02:48 +00:00
|
|
|
pub async fn test_inputobject_derive_and_item_attributes() {
|
2020-08-31 18:18:02 +00:00
|
|
|
use serde::Deserialize;
|
2020-04-08 04:02:48 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(Deserialize, PartialEq, Debug, InputObject)]
|
2020-04-08 04:02:48 +00:00
|
|
|
struct MyInputObject {
|
|
|
|
#[serde(alias = "other")]
|
|
|
|
real: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
serde_json::from_str::<MyInputObject>(r#"{ "other" : 100 }"#).unwrap(),
|
|
|
|
MyInputObject { real: 100 }
|
|
|
|
);
|
|
|
|
}
|
2020-09-03 12:00:33 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-09-04 06:14:48 +00:00
|
|
|
pub async fn test_inputobject_flatten_recursive() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject, Debug, Eq, PartialEq)]
|
2020-09-03 12:00:33 +00:00
|
|
|
struct A {
|
|
|
|
a: i32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject, Debug, Eq, PartialEq)]
|
2020-09-03 12:00:33 +00:00
|
|
|
struct B {
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(default = 70)]
|
2020-09-03 12:00:33 +00:00
|
|
|
b: i32,
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(flatten)]
|
2020-09-03 12:00:33 +00:00
|
|
|
a_obj: A,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject, Debug, Eq, PartialEq)]
|
2020-09-03 12:00:33 +00:00
|
|
|
struct MyInputObject {
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(flatten)]
|
2020-09-03 12:00:33 +00:00
|
|
|
b_obj: B,
|
|
|
|
c: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-10-12 02:17:05 +00:00
|
|
|
MyInputObject::parse(Some(value!({
|
|
|
|
"a": 10,
|
|
|
|
"b": 20,
|
|
|
|
"c": 30,
|
|
|
|
})))
|
2020-09-03 12:00:33 +00:00
|
|
|
.unwrap(),
|
|
|
|
MyInputObject {
|
|
|
|
b_obj: B {
|
|
|
|
b: 20,
|
|
|
|
a_obj: A { a: 10 }
|
|
|
|
},
|
|
|
|
c: 30,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
MyInputObject {
|
|
|
|
b_obj: B {
|
|
|
|
b: 20,
|
|
|
|
a_obj: A { a: 10 }
|
|
|
|
},
|
|
|
|
c: 30,
|
|
|
|
}
|
|
|
|
.to_value(),
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-09-03 12:00:33 +00:00
|
|
|
"a": 10,
|
|
|
|
"b": 20,
|
|
|
|
"c": 30,
|
2020-10-12 02:17:05 +00:00
|
|
|
})
|
2020-09-03 12:00:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-09-03 12:00:33 +00:00
|
|
|
impl Query {
|
|
|
|
async fn test(&self, input: MyInputObject) -> i32 {
|
|
|
|
input.c + input.b_obj.b + input.b_obj.a_obj.a
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn test_with_default(
|
|
|
|
&self,
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(default_with = r#"MyInputObject {
|
2020-09-03 12:00:33 +00:00
|
|
|
b_obj: B {
|
|
|
|
b: 2,
|
|
|
|
a_obj: A { a: 1 }
|
|
|
|
},
|
|
|
|
c: 3,
|
|
|
|
}"#)]
|
|
|
|
input: MyInputObject,
|
|
|
|
) -> i32 {
|
|
|
|
input.c + input.b_obj.b + input.b_obj.a_obj.a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
|
|
|
assert_eq!(
|
|
|
|
schema
|
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
test(input:{a:10, b: 20, c: 30})
|
|
|
|
}"#
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 23:58:02 +00:00
|
|
|
.into_result()
|
2020-09-03 12:00:33 +00:00
|
|
|
.unwrap()
|
|
|
|
.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-09-03 12:00:33 +00:00
|
|
|
"test": 60,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
schema
|
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
test(input:{a:10, c: 30})
|
|
|
|
}"#
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 23:58:02 +00:00
|
|
|
.into_result()
|
2020-09-03 12:00:33 +00:00
|
|
|
.unwrap()
|
|
|
|
.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-09-03 12:00:33 +00:00
|
|
|
"test": 110,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
schema
|
|
|
|
.execute(
|
|
|
|
r#"{
|
|
|
|
testWithDefault
|
|
|
|
}"#
|
|
|
|
)
|
|
|
|
.await
|
2020-09-10 23:58:02 +00:00
|
|
|
.into_result()
|
2020-09-03 12:00:33 +00:00
|
|
|
.unwrap()
|
|
|
|
.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-09-03 12:00:33 +00:00
|
|
|
"testWithDefault": 6,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-09-04 06:14:48 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-09-04 06:14:48 +00:00
|
|
|
pub async fn test_inputobject_flatten_multiple() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject, Debug, Eq, PartialEq)]
|
2020-09-04 06:14:48 +00:00
|
|
|
struct A {
|
|
|
|
a: i32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject, Debug, Eq, PartialEq)]
|
2020-09-04 06:14:48 +00:00
|
|
|
struct B {
|
|
|
|
b: i32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject, Debug, Eq, PartialEq)]
|
2020-09-04 06:14:48 +00:00
|
|
|
struct C {
|
|
|
|
c: i32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject, Debug, Eq, PartialEq)]
|
2021-07-31 15:54:16 +00:00
|
|
|
struct Abc {
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(flatten)]
|
2020-09-04 06:14:48 +00:00
|
|
|
a: A,
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(flatten)]
|
2020-09-04 06:14:48 +00:00
|
|
|
b: B,
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(flatten)]
|
2020-09-04 06:14:48 +00:00
|
|
|
c: C,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-07-31 15:54:16 +00:00
|
|
|
Abc::parse(Some(value!({
|
2020-10-12 02:17:05 +00:00
|
|
|
"a": 10,
|
|
|
|
"b": 20,
|
|
|
|
"c": 30,
|
|
|
|
})))
|
2020-09-04 06:14:48 +00:00
|
|
|
.unwrap(),
|
2021-07-31 15:54:16 +00:00
|
|
|
Abc {
|
2020-09-04 06:14:48 +00:00
|
|
|
a: A { a: 10 },
|
|
|
|
b: B { b: 20 },
|
|
|
|
c: C { c: 30 }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-07-31 15:54:16 +00:00
|
|
|
Abc {
|
2020-09-04 06:14:48 +00:00
|
|
|
a: A { a: 10 },
|
|
|
|
b: B { b: 20 },
|
|
|
|
c: C { c: 30 }
|
|
|
|
}
|
|
|
|
.to_value(),
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-09-04 06:14:48 +00:00
|
|
|
"a": 10,
|
|
|
|
"b": 20,
|
|
|
|
"c": 30,
|
2020-10-12 02:17:05 +00:00
|
|
|
})
|
2020-09-04 06:14:48 +00:00
|
|
|
);
|
|
|
|
}
|
2020-12-15 12:29:59 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-12-15 12:29:59 +00:00
|
|
|
pub async fn test_input_object_skip_field() {
|
|
|
|
#[derive(InputObject)]
|
|
|
|
struct MyInput2 {
|
|
|
|
a: i32,
|
|
|
|
#[graphql(skip)]
|
|
|
|
b: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Root;
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
impl Root {
|
|
|
|
async fn a(&self, input: MyInput2) -> i32 {
|
|
|
|
assert_eq!(input.b, i32::default());
|
|
|
|
input.a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(Root, EmptyMutation, EmptySubscription);
|
|
|
|
let query = r#"{
|
|
|
|
a(input:{a: 777})
|
|
|
|
}"#;
|
|
|
|
assert_eq!(
|
|
|
|
schema.execute(query).await.into_result().unwrap().data,
|
|
|
|
value!({
|
|
|
|
"a": 777
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2021-01-10 05:48:18 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2021-01-10 05:48:18 +00:00
|
|
|
pub async fn test_box_input_object() {
|
|
|
|
#[derive(InputObject)]
|
|
|
|
struct MyInput {
|
|
|
|
value: i32,
|
|
|
|
input: Option<Box<MyInput>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Root;
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
impl Root {
|
|
|
|
async fn q(&self, input: MyInput) -> i32 {
|
|
|
|
input.value
|
|
|
|
+ input.input.as_ref().unwrap().value
|
|
|
|
+ input.input.as_ref().unwrap().input.as_ref().unwrap().value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(Root, EmptyMutation, EmptySubscription);
|
|
|
|
let query = r#"{
|
|
|
|
q(input: {value: 100, input: { value: 200, input: { value: 300 } } })
|
|
|
|
}"#;
|
|
|
|
assert_eq!(
|
|
|
|
schema.execute(query).await.into_result().unwrap().data,
|
|
|
|
value!({
|
|
|
|
"q": 600
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2021-11-09 10:52:10 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
pub async fn test_both_input_output() {
|
|
|
|
#[derive(SimpleObject, InputObject)]
|
|
|
|
#[graphql(input_name = "MyObjectInput")]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
struct MyObject {
|
|
|
|
#[graphql(default = 10)]
|
|
|
|
a: i32,
|
|
|
|
b: bool,
|
|
|
|
#[graphql(skip)]
|
|
|
|
c: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
impl Query {
|
|
|
|
async fn obj(&self, input: MyObject) -> MyObject {
|
|
|
|
input
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
|
|
|
assert_eq!(
|
|
|
|
schema
|
|
|
|
.execute("{ obj(input: {a: 1, b: true}) { a b } }")
|
|
|
|
.await
|
|
|
|
.into_result()
|
|
|
|
.unwrap()
|
|
|
|
.data,
|
|
|
|
value!({
|
|
|
|
"obj": {
|
|
|
|
"a": 1,
|
|
|
|
"b": true,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
schema
|
|
|
|
.execute("{ obj(input: {b: true}) { a b } }")
|
|
|
|
.await
|
|
|
|
.into_result()
|
|
|
|
.unwrap()
|
|
|
|
.data,
|
|
|
|
value!({
|
|
|
|
"obj": {
|
|
|
|
"a": 10,
|
|
|
|
"b": true,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(<MyObject as InputType>::type_name(), "MyObjectInput");
|
|
|
|
assert_eq!(<MyObject as OutputType>::type_name(), "MyObject");
|
|
|
|
}
|