2020-05-19 08:24:15 +00:00
|
|
|
use async_graphql::*;
|
2020-05-20 00:18:28 +00:00
|
|
|
use futures::StreamExt;
|
2020-05-19 08:24:15 +00:00
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
pub async fn test_defer() {
|
2020-05-20 00:18:28 +00:00
|
|
|
struct MyObj;
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
impl MyObj {
|
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
20
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn obj(&self) -> Deferred<MyObj> {
|
|
|
|
MyObj.into()
|
|
|
|
}
|
2020-05-19 08:24:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 00:18:28 +00:00
|
|
|
struct Query;
|
|
|
|
|
2020-05-19 08:24:15 +00:00
|
|
|
#[Object]
|
|
|
|
impl Query {
|
|
|
|
async fn value(&self) -> Deferred<i32> {
|
|
|
|
10.into()
|
|
|
|
}
|
2020-05-20 00:18:28 +00:00
|
|
|
|
|
|
|
async fn obj(&self) -> Deferred<MyObj> {
|
|
|
|
MyObj.into()
|
|
|
|
}
|
2020-05-19 08:24:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 00:18:28 +00:00
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
2020-05-19 08:24:15 +00:00
|
|
|
let query = r#"{
|
2020-05-20 00:18:28 +00:00
|
|
|
value @defer
|
2020-05-19 08:24:15 +00:00
|
|
|
}"#;
|
|
|
|
assert_eq!(
|
|
|
|
schema.execute(&query).await.unwrap().data,
|
|
|
|
serde_json::json!({
|
|
|
|
"value": 10,
|
|
|
|
})
|
|
|
|
);
|
2020-05-20 00:18:28 +00:00
|
|
|
|
|
|
|
let query = r#"{
|
|
|
|
value @defer
|
|
|
|
obj @defer {
|
|
|
|
value
|
|
|
|
obj @defer {
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#;
|
|
|
|
assert_eq!(
|
|
|
|
schema.execute(&query).await.unwrap().data,
|
|
|
|
serde_json::json!({
|
|
|
|
"value": 10,
|
|
|
|
"obj": {
|
|
|
|
"value": 20,
|
|
|
|
"obj": {
|
|
|
|
"value": 20
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-05-21 03:36:44 +00:00
|
|
|
let mut stream = schema.execute_stream(&query).await.into_stream();
|
2020-05-20 00:18:28 +00:00
|
|
|
assert_eq!(
|
|
|
|
stream.next().await.unwrap().unwrap().data,
|
|
|
|
serde_json::json!({
|
|
|
|
"value": null,
|
|
|
|
"obj": null,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
let next_resp = stream.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(next_resp.path, Some(vec![serde_json::json!("value")]));
|
|
|
|
assert_eq!(next_resp.data, serde_json::json!(10));
|
|
|
|
|
|
|
|
let next_resp = stream.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(next_resp.path, Some(vec![serde_json::json!("obj")]));
|
|
|
|
assert_eq!(
|
|
|
|
next_resp.data,
|
|
|
|
serde_json::json!({"value": 20, "obj": null})
|
|
|
|
);
|
|
|
|
|
|
|
|
let next_resp = stream.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
next_resp.path,
|
|
|
|
Some(vec![serde_json::json!("obj"), serde_json::json!("obj")])
|
|
|
|
);
|
|
|
|
assert_eq!(next_resp.data, serde_json::json!({"value": 20}));
|
|
|
|
|
|
|
|
assert!(stream.next().await.is_none());
|
2020-05-19 08:24:15 +00:00
|
|
|
}
|
2020-05-21 02:12:54 +00:00
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
pub async fn test_stream() {
|
|
|
|
#[SimpleObject]
|
|
|
|
struct MyObj {
|
|
|
|
value: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
impl Query {
|
|
|
|
async fn objs(&self) -> Streamed<MyObj> {
|
|
|
|
Streamed::from(vec![
|
|
|
|
MyObj { value: 1 },
|
|
|
|
MyObj { value: 2 },
|
|
|
|
MyObj { value: 3 },
|
|
|
|
MyObj { value: 4 },
|
|
|
|
MyObj { value: 5 },
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
|
|
|
let query = r#"{
|
|
|
|
objs @stream { value }
|
|
|
|
}"#;
|
|
|
|
assert_eq!(
|
|
|
|
schema.execute(&query).await.unwrap().data,
|
|
|
|
serde_json::json!({
|
|
|
|
"objs": [
|
|
|
|
{ "value": 1 },
|
|
|
|
{ "value": 2 },
|
|
|
|
{ "value": 3 },
|
|
|
|
{ "value": 4 },
|
|
|
|
{ "value": 5 },
|
|
|
|
]
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-05-21 03:36:44 +00:00
|
|
|
let mut stream = schema.execute_stream(&query).await.into_stream();
|
2020-05-21 02:12:54 +00:00
|
|
|
assert_eq!(
|
|
|
|
stream.next().await.unwrap().unwrap().data,
|
|
|
|
serde_json::json!({
|
|
|
|
"objs": [],
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
for i in 0..5 {
|
|
|
|
let next_resp = stream.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
next_resp.path,
|
|
|
|
Some(vec![serde_json::json!("objs"), i.into()])
|
|
|
|
);
|
|
|
|
assert_eq!(next_resp.data, serde_json::json!({ "value": i + 1 }));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(stream.next().await.is_none());
|
|
|
|
}
|