async-graphql/tests/field_features.rs

168 lines
4.2 KiB
Rust
Raw Normal View History

2020-06-06 03:49:29 +00:00
#![allow(dead_code)]
use async_graphql::*;
use futures::{Stream, StreamExt};
#[async_std::test]
pub async fn test_field_features() {
#[derive(SimpleObject)]
2020-06-06 03:49:29 +00:00
struct MyObj {
value: i32,
#[cfg(feature = "bson")]
2020-06-06 03:49:29 +00:00
value_bson: i32,
#[cfg(feature = "abc")]
2020-06-06 03:49:29 +00:00
value_abc: i32,
}
struct SubscriptionRoot;
2020-06-06 03:49:29 +00:00
#[Subscription]
impl SubscriptionRoot {
2020-06-06 03:49:29 +00:00
async fn values(&self) -> impl Stream<Item = i32> {
futures::stream::once(async move { 10 })
}
#[cfg(feature = "bson")]
2020-06-06 03:49:29 +00:00
async fn values_bson(&self) -> impl Stream<Item = i32> {
futures::stream::once(async move { 10 })
}
#[cfg(feature = "abc")]
2020-06-06 03:49:29 +00:00
async fn values_abc(
&self,
) -> Pin<Box<dyn async_graphql::futures::Stream<Item = i32> + Send + 'static>> {
Box::pin(futures::stream::once(async move { 10 }))
}
}
struct QueryRoot;
#[Object]
2020-06-06 03:49:29 +00:00
impl QueryRoot {
async fn value(&self) -> i32 {
10
}
#[cfg(feature = "bson")]
2020-06-06 03:49:29 +00:00
async fn value_bson(&self) -> i32 {
10
}
#[cfg(feature = "abc")]
2020-06-06 03:49:29 +00:00
async fn value_abc(&self) -> i32 {
10
}
async fn obj(&self) -> MyObj {
MyObj {
value: 10,
#[cfg(feature = "bson")]
2020-06-06 03:49:29 +00:00
value_bson: 10,
#[cfg(feature = "abc")]
2020-06-06 03:49:29 +00:00
value_abc: 10,
}
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
2020-06-06 03:49:29 +00:00
let query = "{ value }";
assert_eq!(
2020-09-10 11:35:48 +00:00
schema.execute(query).await.data,
2020-06-06 03:49:29 +00:00
serde_json::json!({
"value": 10,
})
);
let query = "{ valueBson }";
assert_eq!(
2020-09-10 11:35:48 +00:00
schema.execute(query).await.data,
2020-06-06 03:49:29 +00:00
serde_json::json!({
"valueBson": 10,
})
);
let query = "{ valueAbc }";
assert_eq!(
schema.execute(query).await.into_result().unwrap_err(),
Error::Rule {
errors: vec![RuleError {
locations: vec![Pos { line: 1, column: 3 }],
message: r#"Unknown field "valueAbc" on type "QueryRoot". Did you mean "value"?"#
.to_string(),
}]
.into(),
2020-06-06 03:49:29 +00:00
}
);
let query = "{ obj { value } }";
assert_eq!(
2020-09-10 11:35:48 +00:00
schema.execute(query).await.data,
2020-06-06 03:49:29 +00:00
serde_json::json!({
"obj": { "value": 10 }
})
);
let query = "{ obj { valueBson } }";
assert_eq!(
2020-09-10 11:35:48 +00:00
schema.execute(query).await.data,
2020-06-06 03:49:29 +00:00
serde_json::json!({
"obj": { "valueBson": 10 }
})
);
let query = "{ obj { valueAbc } }";
assert_eq!(
schema.execute(query).await.into_result().unwrap_err(),
Error::Rule {
errors: vec![RuleError {
locations: vec![Pos { line: 1, column: 9 }],
message: r#"Unknown field "valueAbc" on type "MyObj". Did you mean "value"?"#
.to_string(),
}]
.into(),
2020-06-06 03:49:29 +00:00
}
);
2020-09-11 08:05:21 +00:00
let mut stream = schema.execute_stream("subscription { values }").boxed();
2020-06-06 03:49:29 +00:00
assert_eq!(
2020-09-14 01:46:22 +00:00
stream
.next()
.await
.map(|resp| resp.into_result().unwrap().data),
2020-09-10 11:35:48 +00:00
Some(serde_json::json!({
2020-06-06 03:49:29 +00:00
"values": 10
2020-09-10 11:35:48 +00:00
}))
2020-06-06 03:49:29 +00:00
);
2020-09-11 08:05:21 +00:00
let mut stream = schema.execute_stream("subscription { valuesBson }").boxed();
2020-06-06 03:49:29 +00:00
assert_eq!(
2020-09-10 11:35:48 +00:00
stream.next().await.map(|resp| resp.data),
Some(serde_json::json!({
2020-06-06 03:49:29 +00:00
"valuesBson": 10
2020-09-10 11:35:48 +00:00
}))
2020-06-06 03:49:29 +00:00
);
2020-09-10 11:35:48 +00:00
assert_eq!(
schema
.execute_stream("subscription { valuesAbc }")
2020-09-11 08:05:21 +00:00
.boxed()
.next()
.await
.unwrap()
.error
.unwrap(),
Error::Rule {
errors: vec![RuleError {
locations: vec![Pos {
line: 1,
column: 16
}],
message:
r#"Unknown field "valuesAbc" on type "SubscriptionRoot". Did you mean "values", "valuesBson"?"#
.to_string(),
}]
.into(),
2020-09-10 11:35:48 +00:00
}
);
2020-06-06 03:49:29 +00:00
}