2020-04-03 14:19:15 +00:00
|
|
|
use async_graphql::*;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
2021-03-12 04:47:24 +00:00
|
|
|
use tokio::sync::Mutex;
|
2020-04-03 14:19:15 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2021-06-16 07:08:33 +00:00
|
|
|
pub async fn test_root_mutation_execution_order() {
|
2020-04-03 14:19:15 +00:00
|
|
|
type List = Arc<Mutex<Vec<i32>>>;
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-04-03 14:19:15 +00:00
|
|
|
|
2020-10-20 03:49:31 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-10-20 03:49:31 +00:00
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Mutation;
|
2020-04-03 14:19:15 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Mutation {
|
2020-04-03 14:19:15 +00:00
|
|
|
async fn append1(&self, ctx: &Context<'_>) -> bool {
|
2021-03-12 04:47:24 +00:00
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
2020-07-06 23:54:57 +00:00
|
|
|
ctx.data_unchecked::<List>().lock().await.push(1);
|
2020-04-03 14:19:15 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn append2(&self, ctx: &Context<'_>) -> bool {
|
2021-03-12 04:47:24 +00:00
|
|
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
2020-07-06 23:54:57 +00:00
|
|
|
ctx.data_unchecked::<List>().lock().await.push(2);
|
2020-04-03 14:19:15 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = List::default();
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::build(Query, Mutation, EmptySubscription)
|
2020-04-03 14:19:15 +00:00
|
|
|
.data(list.clone())
|
|
|
|
.finish();
|
2020-09-10 11:35:48 +00:00
|
|
|
schema.execute("mutation { append1 append2 }").await;
|
2021-06-16 07:08:33 +00:00
|
|
|
assert_eq!(&*list.lock().await, &[1, 2]);
|
2020-04-03 14:19:15 +00:00
|
|
|
}
|
2020-04-22 13:31:44 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-04-22 13:31:44 +00:00
|
|
|
pub async fn test_mutation_fragment() {
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2020-04-22 13:31:44 +00:00
|
|
|
|
2020-10-20 03:49:31 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2020-10-20 03:49:31 +00:00
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Mutation;
|
2020-04-22 13:31:44 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Mutation {
|
2020-04-22 13:31:44 +00:00
|
|
|
async fn action(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::new(Query, Mutation, EmptySubscription);
|
2020-04-22 13:31:44 +00:00
|
|
|
let resp = schema
|
|
|
|
.execute(
|
|
|
|
r#"
|
|
|
|
mutation {
|
|
|
|
... {
|
|
|
|
actionInUnnamedFragment: action
|
|
|
|
}
|
2021-11-20 03:16:48 +00:00
|
|
|
... on Mutation {
|
2020-04-22 13:31:44 +00:00
|
|
|
actionInNamedFragment: action
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
2020-09-10 11:35:48 +00:00
|
|
|
.await;
|
2020-04-22 13:31:44 +00:00
|
|
|
assert_eq!(
|
|
|
|
resp.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-04-22 13:31:44 +00:00
|
|
|
"actionInUnnamedFragment": true,
|
|
|
|
"actionInNamedFragment": true,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2021-06-15 00:53:26 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
2021-06-16 07:08:33 +00:00
|
|
|
pub async fn test_serial_object() {
|
2021-06-15 00:53:26 +00:00
|
|
|
type List = Arc<Mutex<Vec<i32>>>;
|
|
|
|
|
|
|
|
struct MyObj;
|
|
|
|
|
|
|
|
#[Object(serial)]
|
|
|
|
impl MyObj {
|
|
|
|
async fn append1(&self, ctx: &Context<'_>) -> bool {
|
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
|
|
ctx.data_unchecked::<List>().lock().await.push(1);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn append2(&self, ctx: &Context<'_>) -> bool {
|
|
|
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
|
|
|
ctx.data_unchecked::<List>().lock().await.push(2);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2021-06-15 00:53:26 +00:00
|
|
|
|
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2021-06-15 00:53:26 +00:00
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Mutation;
|
2021-06-15 00:53:26 +00:00
|
|
|
|
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Mutation {
|
2021-06-15 00:53:26 +00:00
|
|
|
async fn obj(&self) -> MyObj {
|
|
|
|
MyObj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = List::default();
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::build(Query, Mutation, EmptySubscription)
|
2021-06-15 00:53:26 +00:00
|
|
|
.data(list.clone())
|
|
|
|
.finish();
|
|
|
|
schema.execute("mutation { obj { append1 append2 } }").await;
|
2021-06-16 07:08:33 +00:00
|
|
|
assert_eq!(&*list.lock().await, &[1, 2]);
|
2021-06-15 00:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
pub async fn test_serial_simple_object() {
|
|
|
|
type List = Arc<Mutex<Vec<i32>>>;
|
|
|
|
|
|
|
|
#[derive(SimpleObject)]
|
|
|
|
#[graphql(complex, serial)]
|
|
|
|
struct MyObj {
|
|
|
|
value: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ComplexObject]
|
|
|
|
impl MyObj {
|
|
|
|
async fn append1(&self, ctx: &Context<'_>) -> bool {
|
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
|
|
ctx.data_unchecked::<List>().lock().await.push(1);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn append2(&self, ctx: &Context<'_>) -> bool {
|
|
|
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
|
|
|
ctx.data_unchecked::<List>().lock().await.push(2);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2021-06-15 00:53:26 +00:00
|
|
|
|
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2021-06-15 00:53:26 +00:00
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Mutation;
|
2021-06-15 00:53:26 +00:00
|
|
|
|
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Mutation {
|
2021-06-15 00:53:26 +00:00
|
|
|
async fn obj(&self) -> MyObj {
|
|
|
|
MyObj { value: 10 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = List::default();
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::build(Query, Mutation, EmptySubscription)
|
2021-06-15 00:53:26 +00:00
|
|
|
.data(list.clone())
|
|
|
|
.finish();
|
|
|
|
schema.execute("mutation { obj { append1 append2 } }").await;
|
2021-06-16 07:08:33 +00:00
|
|
|
assert_eq!(&*list.lock().await, &[1, 2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
pub async fn test_serial_merged_object() {
|
|
|
|
type List = Arc<Mutex<Vec<i32>>>;
|
|
|
|
|
|
|
|
#[derive(MergedObject)]
|
|
|
|
#[graphql(serial)]
|
|
|
|
struct MyObj(MyObj1, MyObj2);
|
|
|
|
|
|
|
|
struct MyObj1;
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
impl MyObj1 {
|
|
|
|
async fn append1(&self, ctx: &Context<'_>) -> bool {
|
|
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
|
|
ctx.data_unchecked::<List>().lock().await.push(1);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn append2(&self, ctx: &Context<'_>) -> bool {
|
|
|
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
|
|
|
ctx.data_unchecked::<List>().lock().await.push(2);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MyObj2;
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
impl MyObj2 {
|
|
|
|
async fn append3(&self, ctx: &Context<'_>) -> bool {
|
|
|
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
|
|
|
ctx.data_unchecked::<List>().lock().await.push(3);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn append4(&self, ctx: &Context<'_>) -> bool {
|
|
|
|
tokio::time::sleep(Duration::from_millis(700)).await;
|
|
|
|
ctx.data_unchecked::<List>().lock().await.push(4);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Query;
|
2021-06-16 07:08:33 +00:00
|
|
|
|
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Query {
|
2021-06-16 07:08:33 +00:00
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 03:16:48 +00:00
|
|
|
struct Mutation;
|
2021-06-16 07:08:33 +00:00
|
|
|
|
|
|
|
#[Object]
|
2021-11-20 03:16:48 +00:00
|
|
|
impl Mutation {
|
2021-06-16 07:08:33 +00:00
|
|
|
async fn obj(&self) -> MyObj {
|
|
|
|
MyObj(MyObj1, MyObj2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = List::default();
|
2021-11-20 03:16:48 +00:00
|
|
|
let schema = Schema::build(Query, Mutation, EmptySubscription)
|
2021-06-16 07:08:33 +00:00
|
|
|
.data(list.clone())
|
|
|
|
.finish();
|
|
|
|
schema
|
|
|
|
.execute("mutation { obj { append1 append2 append3 append4 } }")
|
|
|
|
.await;
|
|
|
|
assert_eq!(&*list.lock().await, &[1, 2, 3, 4]);
|
2021-06-15 00:53:26 +00:00
|
|
|
}
|