async-graphql/tests/variables.rs
Koxiaet ed5acdcf18 Support service parsing in async-graphql-parser
- Instead of adding a separate module `schema` like there was before,
since service parsing and executable parsing have a fair amount of
overlap I put them as two submodules `executable` and `service` in both
`parse` and `types`. Also, the grammar is unified under one `.pest`
file.
- Added const equivalents to `Value`, `Directive` etc
- Change the reexport `async_graphql::Value` from
`async_graphql_parser::types::Value` to
`async_graphql_parser::types::ConstValue` since in 99% of cases in this library
a const value is wanted instead of a value.
- Added consistent usage of executable/service instead of the ambiguous
query/schema.
- Some of the tests actually had invalid GraphQL so the new more correct
grammar made them fail, that was fixed.
- Added a `Name` newtype to refer to GraphQL names
(`[A-Za-z_][A-Za-z_0-9]*`) since they are used so frequently.
2020-09-08 09:21:27 +01:00

235 lines
5.3 KiB
Rust

use async_graphql::*;
#[async_std::test]
pub async fn test_variables() {
struct QueryRoot;
#[Object]
impl QueryRoot {
pub async fn int_val(&self, value: i32) -> i32 {
value
}
pub async fn int_list_val(&self, value: Vec<i32>) -> Vec<i32> {
value
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let query = QueryBuilder::new(
r#"
query QueryWithVariables($intVal: Int!, $intListVal: [Int!]!) {
intVal(value: $intVal)
intListVal(value: $intListVal)
}
"#,
)
.variables(
Variables::parse_from_json(serde_json::json!({
"intVal": 10,
"intListVal": [1, 2, 3, 4, 5],
}))
);
let resp = query.execute(&schema).await.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"intVal": 10,
"intListVal": [1, 2, 3, 4, 5],
})
);
}
#[async_std::test]
pub async fn test_variable_default_value() {
struct QueryRoot;
#[Object]
impl QueryRoot {
pub async fn int_val(&self, value: i32) -> i32 {
value
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let resp = schema
.execute(
r#"
query QueryWithVariables($intVal: Int = 10) {
intVal(value: $intVal)
}
"#,
)
.await
.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"intVal": 10,
})
);
}
#[async_std::test]
pub async fn test_variable_no_value() {
struct QueryRoot;
#[Object]
impl QueryRoot {
pub async fn int_val(&self, value: Option<i32>) -> i32 {
value.unwrap_or(10)
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let query = QueryBuilder::new(
r#"
query QueryWithVariables($intVal: Int) {
intVal(value: $intVal)
}
"#,
)
.variables(Variables::parse_from_json(serde_json::json!({})));
let resp = query.execute(&schema).await.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"intVal": 10,
})
);
}
#[async_std::test]
pub async fn test_variable_null() {
struct QueryRoot;
#[Object]
impl QueryRoot {
pub async fn int_val(&self, value: Option<i32>) -> i32 {
value.unwrap_or(10)
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let query = QueryBuilder::new(
r#"
query QueryWithVariables($intVal: Int) {
intVal(value: $intVal)
}
"#,
)
.variables(
Variables::parse_from_json(serde_json::json!({
"intVal": null,
}))
);
let resp = query.execute(&schema).await.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"intVal": 10,
})
);
}
#[async_std::test]
pub async fn test_variable_in_input_object() {
#[InputObject]
struct MyInput {
value: i32,
}
struct QueryRoot;
#[Object]
impl QueryRoot {
async fn test(&self, input: MyInput) -> i32 {
input.value
}
async fn test2(&self, input: Vec<MyInput>) -> i32 {
input.iter().map(|item| item.value).sum()
}
}
struct MutationRoot;
#[Object]
impl MutationRoot {
async fn test(&self, input: MyInput) -> i32 {
input.value
}
}
let schema = Schema::new(QueryRoot, MutationRoot, EmptySubscription);
// test query
{
let query = r#"
query TestQuery($value: Int!) {
test(input: {value: $value })
}"#;
let resp = QueryBuilder::new(query)
.variables(
Variables::parse_from_json(serde_json::json!({
"value": 10,
}))
)
.execute(&schema)
.await
.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"test": 10,
})
);
}
// test query2
{
let query = r#"
query TestQuery($value: Int!) {
test2(input: [{value: $value }, {value: $value }])
}"#;
let resp = QueryBuilder::new(query)
.variables(
Variables::parse_from_json(serde_json::json!({
"value": 3,
}))
)
.execute(&schema)
.await
.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"test2": 6,
})
);
}
// test mutation
{
let query = r#"
mutation TestMutation($value: Int!) {
test(input: {value: $value })
}"#;
let resp = QueryBuilder::new(query)
.variables(
Variables::parse_from_json(serde_json::json!({
"value": 10,
}))
)
.execute(&schema)
.await
.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"test": 10,
})
);
}
}