async-graphql/tests/directive.rs

79 lines
1.6 KiB
Rust
Raw Normal View History

2020-04-22 13:31:44 +00:00
use async_graphql::*;
#[tokio::test]
2020-04-22 13:31:44 +00:00
pub async fn test_directive_skip() {
struct QueryRoot;
#[Object]
2020-04-22 13:31:44 +00:00
impl QueryRoot {
pub async fn value(&self) -> i32 {
10
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let data = schema
2020-04-22 13:31:44 +00:00
.execute(
r#"
fragment A on QueryRoot {
value5: value @skip(if: true)
value6: value @skip(if: false)
}
query {
2020-04-22 13:31:44 +00:00
value1: value @skip(if: true)
value2: value @skip(if: false)
... @skip(if: true) {
value3: value
}
... @skip(if: false) {
value4: value
}
... A
2020-04-22 13:31:44 +00:00
}
"#,
)
.await
.into_result()
.unwrap()
.data;
2020-04-22 13:31:44 +00:00
assert_eq!(
data,
value!({
2020-04-22 13:31:44 +00:00
"value2": 10,
"value4": 10,
"value6": 10,
2020-04-22 13:31:44 +00:00
})
);
}
#[tokio::test]
2020-04-22 13:31:44 +00:00
pub async fn test_directive_include() {
struct QueryRoot;
#[Object]
2020-04-22 13:31:44 +00:00
impl QueryRoot {
pub async fn value(&self) -> i32 {
10
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let resp = schema
.execute(
r#"
{
value1: value @include(if: true)
value2: value @include(if: false)
}
"#,
)
2020-09-10 11:35:48 +00:00
.await;
2020-04-22 13:31:44 +00:00
assert_eq!(
resp.data,
value!({
2020-04-22 13:31:44 +00:00
"value1": 10,
})
);
}