async-graphql/tests/maybe_undefined.rs
Koxiaet 47259548c4 Support service parsing in async-graphql-parser
- Instead of adding a separate module `schema` like there was before,
since service parsing and executable parsing have a fair amount of
overlap I put them as two submodules `executable` and `service` in both
`parse` and `types`. Also, the grammar is unified under one `.pest`
file.
- Added const equivalents to `Value`, `Directive` etc
- Change the reexport `async_graphql::Value` from
`async_graphql_parser::types::Value` to
`async_graphql_parser::types::ConstValue` since in 99% of cases in this library
a const value is wanted instead of a value.
- Added consistent usage of executable/service instead of the ambiguous
query/schema.
- Some of the tests actually had invalid GraphQL so the new more correct
grammar made them fail, that was fixed.
- Added a `Name` newtype to refer to GraphQL names
(`[A-Za-z_][A-Za-z_0-9]*`) since they are used so frequently.
2020-09-08 09:21:27 +01:00

58 lines
1.3 KiB
Rust

use async_graphql::*;
#[async_std::test]
pub async fn test_maybe_undefined_type() {
#[InputObject]
struct MyInput {
value: MaybeUndefined<i32>,
}
struct Query;
#[Object]
impl Query {
async fn value1(&self, input: MaybeUndefined<i32>) -> i32 {
if input.is_null() {
1
} else if input.is_undefined() {
2
} else {
input.take().unwrap()
}
}
async fn value2(&self, input: MyInput) -> i32 {
if input.value.is_null() {
1
} else if input.value.is_undefined() {
2
} else {
input.value.take().unwrap()
}
}
}
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let query = r#"
{
v1:value1(input: 99)
v2:value1(input: null)
v3:value1
v4:value2(input: { value: 99} )
v5:value2(input: { value: null} )
v6:value2(input: {} )
}
"#;
assert_eq!(
schema.execute(&query).await.unwrap().data,
serde_json::json!({
"v1": 99,
"v2": 1,
"v3": 2,
"v4": 99,
"v5": 1,
"v6": 2,
})
);
}