2020-03-07 04:40:04 +00:00
|
|
|
use async_graphql::*;
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
pub async fn test_optional_type() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject)]
|
2020-03-07 04:40:04 +00:00
|
|
|
struct MyInput {
|
|
|
|
value: Option<i32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Root {
|
|
|
|
value1: Option<i32>,
|
|
|
|
value2: Option<i32>,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-03-07 04:40:04 +00:00
|
|
|
impl Root {
|
|
|
|
async fn value1(&self) -> Option<i32> {
|
2020-08-31 18:18:02 +00:00
|
|
|
self.value1
|
2020-03-07 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn value1_ref(&self) -> &Option<i32> {
|
|
|
|
&self.value1
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn value2(&self) -> Option<i32> {
|
2020-08-31 18:18:02 +00:00
|
|
|
self.value2
|
2020-03-07 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn value2_ref(&self) -> &Option<i32> {
|
|
|
|
&self.value2
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn test_arg(&self, input: Option<i32>) -> Option<i32> {
|
|
|
|
input
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn test_input<'a>(&self, input: MyInput) -> Option<i32> {
|
2020-08-31 18:18:02 +00:00
|
|
|
input.value
|
2020-03-07 04:40:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(
|
|
|
|
Root {
|
|
|
|
value1: Some(10),
|
|
|
|
value2: None,
|
|
|
|
},
|
2020-03-19 09:20:12 +00:00
|
|
|
EmptyMutation,
|
|
|
|
EmptySubscription,
|
2020-03-07 04:40:04 +00:00
|
|
|
);
|
2020-09-01 01:10:12 +00:00
|
|
|
let query = r#"{
|
2020-03-07 04:40:04 +00:00
|
|
|
value1
|
2020-03-09 12:00:57 +00:00
|
|
|
value1Ref
|
2020-03-07 04:40:04 +00:00
|
|
|
value2
|
2020-03-09 12:00:57 +00:00
|
|
|
value2Ref
|
|
|
|
testArg1: testArg(input: 10)
|
|
|
|
testArg2: testArg
|
2020-09-01 01:10:12 +00:00
|
|
|
testInput1: testInput(input: {value: 10})
|
|
|
|
testInput2: testInput(input: {})
|
|
|
|
}"#
|
2020-08-31 18:18:02 +00:00
|
|
|
.to_owned();
|
2020-03-07 04:40:04 +00:00
|
|
|
assert_eq!(
|
2020-09-10 11:35:48 +00:00
|
|
|
schema.execute(&query).await.data,
|
2020-03-07 04:40:04 +00:00
|
|
|
serde_json::json!({
|
|
|
|
"value1": 10,
|
2020-03-09 12:00:57 +00:00
|
|
|
"value1Ref": 10,
|
2020-03-07 04:40:04 +00:00
|
|
|
"value2": null,
|
2020-03-09 12:00:57 +00:00
|
|
|
"value2Ref": null,
|
|
|
|
"testArg1": 10,
|
|
|
|
"testArg2": null,
|
|
|
|
"testInput1": 10,
|
|
|
|
"testInput2": null,
|
2020-03-07 04:40:04 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|