async-graphql/tests/input_object.rs

298 lines
5.9 KiB
Rust
Raw Normal View History

2020-03-07 12:54:03 +00:00
use async_graphql::*;
#[async_std::test]
pub async fn test_input_object_default_value() {
#[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)]
d: i32,
2020-03-07 12:54:03 +00:00
2020-09-28 09:44:00 +00:00
#[graphql(default = 999)]
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>,
}
#[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;
#[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,
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
}
}"#
.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-03-07 12:54:03 +00:00
serde_json::json!({
"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
}
})
);
}
#[async_std::test]
pub async fn test_inputobject_derive_and_item_attributes() {
use serde::Deserialize;
#[derive(Deserialize, PartialEq, Debug, InputObject)]
struct MyInputObject {
#[serde(alias = "other")]
real: i32,
}
assert_eq!(
serde_json::from_str::<MyInputObject>(r#"{ "other" : 100 }"#).unwrap(),
MyInputObject { real: 100 }
);
}
#[async_std::test]
pub async fn test_inputobject_flatten_recursive() {
#[derive(InputObject, Debug, Eq, PartialEq)]
struct A {
a: i32,
}
#[derive(InputObject, Debug, Eq, PartialEq)]
struct B {
2020-09-28 09:44:00 +00:00
#[graphql(default = 70)]
b: i32,
2020-09-28 09:44:00 +00:00
#[graphql(flatten)]
a_obj: A,
}
#[derive(InputObject, Debug, Eq, PartialEq)]
struct MyInputObject {
2020-09-28 09:44:00 +00:00
#[graphql(flatten)]
b_obj: B,
c: i32,
}
assert_eq!(
MyInputObject::parse(Some(
Value::from_json(serde_json::json!({
"a": 10,
"b": 20,
"c": 30,
2020-09-08 08:30:29 +00:00
}))
.unwrap()
))
.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(),
Value::from_json(serde_json::json!({
"a": 10,
"b": 20,
"c": 30,
2020-09-08 08:30:29 +00:00
}))
.unwrap()
);
struct Query;
#[Object]
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 {
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
.into_result()
.unwrap()
.data,
serde_json::json!({
"test": 60,
})
);
assert_eq!(
schema
.execute(
r#"{
test(input:{a:10, c: 30})
}"#
)
.await
.into_result()
.unwrap()
.data,
serde_json::json!({
"test": 110,
})
);
assert_eq!(
schema
.execute(
r#"{
testWithDefault
}"#
)
.await
.into_result()
.unwrap()
.data,
serde_json::json!({
"testWithDefault": 6,
})
);
}
#[async_std::test]
pub async fn test_inputobject_flatten_multiple() {
#[derive(InputObject, Debug, Eq, PartialEq)]
struct A {
a: i32,
}
#[derive(InputObject, Debug, Eq, PartialEq)]
struct B {
b: i32,
}
#[derive(InputObject, Debug, Eq, PartialEq)]
struct C {
c: i32,
}
#[derive(InputObject, Debug, Eq, PartialEq)]
struct ABC {
2020-09-28 09:44:00 +00:00
#[graphql(flatten)]
a: A,
2020-09-28 09:44:00 +00:00
#[graphql(flatten)]
b: B,
2020-09-28 09:44:00 +00:00
#[graphql(flatten)]
c: C,
}
assert_eq!(
ABC::parse(Some(
Value::from_json(serde_json::json!({
"a": 10,
"b": 20,
"c": 30,
2020-09-08 08:30:29 +00:00
}))
.unwrap()
))
.unwrap(),
ABC {
a: A { a: 10 },
b: B { b: 20 },
c: C { c: 30 }
}
);
assert_eq!(
ABC {
a: A { a: 10 },
b: B { b: 20 },
c: C { c: 30 }
}
.to_value(),
Value::from_json(serde_json::json!({
"a": 10,
"b": 20,
"c": 30,
2020-09-08 08:30:29 +00:00
}))
.unwrap()
);
}