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]
|
2020-04-22 13:31:44 +00:00
|
|
|
pub async fn test_mutation_execution_order() {
|
2020-04-03 14:19:15 +00:00
|
|
|
type List = Arc<Mutex<Vec<i32>>>;
|
|
|
|
|
2020-04-22 13:31:44 +00:00
|
|
|
struct QueryRoot;
|
2020-04-03 14:19:15 +00:00
|
|
|
|
2020-10-20 03:49:31 +00:00
|
|
|
#[Object]
|
|
|
|
impl QueryRoot {
|
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:19:15 +00:00
|
|
|
struct MutationRoot;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-04-03 14:19:15 +00:00
|
|
|
impl MutationRoot {
|
|
|
|
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();
|
|
|
|
let schema = Schema::build(QueryRoot, MutationRoot, EmptySubscription)
|
|
|
|
.data(list.clone())
|
|
|
|
.finish();
|
2020-09-10 11:35:48 +00:00
|
|
|
schema.execute("mutation { append1 append2 }").await;
|
2020-04-03 14:19:15 +00:00
|
|
|
assert_eq!(list.lock().await[0], 1);
|
|
|
|
assert_eq!(list.lock().await[1], 2);
|
|
|
|
}
|
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() {
|
|
|
|
struct QueryRoot;
|
|
|
|
|
2020-10-20 03:49:31 +00:00
|
|
|
#[Object]
|
|
|
|
impl QueryRoot {
|
|
|
|
async fn value(&self) -> i32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 13:31:44 +00:00
|
|
|
struct MutationRoot;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-04-22 13:31:44 +00:00
|
|
|
impl MutationRoot {
|
|
|
|
async fn action(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema = Schema::new(QueryRoot, MutationRoot, EmptySubscription);
|
|
|
|
let resp = schema
|
|
|
|
.execute(
|
|
|
|
r#"
|
|
|
|
mutation {
|
|
|
|
... {
|
|
|
|
actionInUnnamedFragment: action
|
|
|
|
}
|
|
|
|
... on MutationRoot {
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|