async-graphql/tests/default_value.rs

110 lines
2.6 KiB
Rust
Raw Permalink Normal View History

2020-05-04 02:49:35 +00:00
use async_graphql::*;
#[tokio::test]
2020-05-04 05:26:13 +00:00
pub async fn test_default_value_arg() {
2020-05-04 02:49:35 +00:00
struct Query;
#[Object]
2020-05-04 02:49:35 +00:00
impl Query {
2020-09-28 09:44:00 +00:00
async fn value1(&self, #[graphql(default = 100)] input: i32) -> i32 {
2020-05-27 01:27:59 +00:00
input
}
2020-09-28 09:44:00 +00:00
async fn value2(&self, #[graphql(default)] input: i32) -> i32 {
2020-05-27 01:27:59 +00:00
input
}
2020-09-28 09:44:00 +00:00
async fn value3(&self, #[graphql(default_with = "1 + 2 + 3")] input: i32) -> i32 {
2020-05-04 02:49:35 +00:00
input
}
async fn value4(&self, #[graphql(default = 100)] input: u32) -> u32 {
input
}
2020-05-04 02:49:35 +00:00
}
let query = "{ value1 value2 value3 value4 }";
2020-05-04 02:49:35 +00:00
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
2020-09-10 11:35:48 +00:00
schema.execute(query).await.data,
value!({
2020-05-27 01:27:59 +00:00
"value1": 100,
"value2": 0,
"value3": 6,
"value4": 100,
2020-05-04 02:49:35 +00:00
})
);
2020-05-27 01:27:59 +00:00
let query = "{ value1(input: 1) value2(input: 2) value3(input: 3) }";
2020-05-04 02:49:35 +00:00
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
2020-09-10 11:35:48 +00:00
schema.execute(query).await.data,
value!({
2020-05-27 01:27:59 +00:00
"value1": 1,
"value2": 2,
"value3": 3,
2020-05-04 02:49:35 +00:00
})
);
}
2020-05-04 05:26:13 +00:00
#[tokio::test]
2020-05-04 05:26:13 +00:00
pub async fn test_default_value_inputobject() {
#[derive(InputObject)]
2020-05-04 05:26:13 +00:00
struct MyInput {
2020-09-28 09:44:00 +00:00
#[graphql(default = 100)]
2020-05-27 01:27:59 +00:00
value1: i32,
2020-09-28 09:44:00 +00:00
#[graphql(default)]
2020-05-27 01:27:59 +00:00
value2: i32,
2020-09-28 09:44:00 +00:00
#[graphql(default_with = "1 + 2 + 3")]
2020-05-27 01:27:59 +00:00
value3: i32,
}
#[derive(SimpleObject)]
2020-05-27 01:27:59 +00:00
struct MyOutput {
value1: i32,
value2: i32,
value3: i32,
2020-05-04 05:26:13 +00:00
}
struct Query;
#[Object]
2020-05-04 05:26:13 +00:00
impl Query {
2020-05-27 01:27:59 +00:00
async fn value(&self, input: MyInput) -> MyOutput {
MyOutput {
value1: input.value1,
value2: input.value2,
value3: input.value3,
}
2020-05-04 05:26:13 +00:00
}
}
2020-05-27 01:27:59 +00:00
let query = "{ value(input: {}) { value1 value2 value3 } }";
2020-05-04 05:26:13 +00:00
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
2020-09-10 11:35:48 +00:00
schema.execute(query).await.data,
value!({
2020-05-27 01:27:59 +00:00
"value": {
"value1": 100,
"value2": 0,
"value3": 6,
}
2020-05-04 05:26:13 +00:00
})
);
2020-05-27 01:27:59 +00:00
let query = "{ value(input: { value1: 1, value2: 2, value3: 3 }) { value1 value2 value3 } }";
2020-05-04 05:26:13 +00:00
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
2020-09-10 11:35:48 +00:00
schema.execute(query).await.data,
value!({
2020-05-27 01:27:59 +00:00
"value": {
"value1": 1,
"value2": 2,
"value3": 3,
}
2020-05-04 05:26:13 +00:00
})
);
}