Fixed variable type not checked when given a default value
This commit is contained in:
parent
c2feefdf09
commit
962e1b1f80
|
@ -1,3 +1,5 @@
|
|||
use async_graphql_parser::types::BaseType;
|
||||
|
||||
use crate::context::QueryPathNode;
|
||||
use crate::parser::types::VariableDefinition;
|
||||
use crate::validation::utils::is_valid_input_value;
|
||||
|
@ -12,6 +14,13 @@ impl<'a> Visitor<'a> for DefaultValuesOfCorrectType {
|
|||
ctx: &mut VisitorContext<'a>,
|
||||
variable_definition: &'a Positioned<VariableDefinition>,
|
||||
) {
|
||||
if let BaseType::Named(vtype_name) = &variable_definition.node.var_type.node.base {
|
||||
if !ctx.registry.types.contains_key(vtype_name.as_str()) {
|
||||
ctx.report_error(vec![variable_definition.pos], format!(r#"Unknown type "{}""#, vtype_name));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(value) = &variable_definition.node.default_value {
|
||||
if !variable_definition.node.var_type.node.nullable {
|
||||
ctx.report_error(vec![variable_definition.pos],format!(
|
||||
|
|
|
@ -297,3 +297,61 @@ pub async fn test_variables_json() {
|
|||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
pub async fn test_variables_invalid_type() {
|
||||
struct Query;
|
||||
|
||||
#[Object]
|
||||
impl Query {
|
||||
pub async fn int_val(&self, value: Option<i32>) -> i32 {
|
||||
value.unwrap_or(10)
|
||||
}
|
||||
}
|
||||
|
||||
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
||||
let query = Request::new(
|
||||
r#"
|
||||
query QueryWithVariables($intVal: invalid) {
|
||||
intVal(value: $intVal)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.variables(Variables::from_value(value!({
|
||||
"intVal": null,
|
||||
})));
|
||||
let resp = schema.execute(query).await;
|
||||
assert_eq!(
|
||||
resp.errors.first().map(|v| v.message.as_str()),
|
||||
Some("Unknown type \"invalid\"")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
pub async fn test_variables_invalid_type_with_value() {
|
||||
struct Query;
|
||||
|
||||
#[Object]
|
||||
impl Query {
|
||||
pub async fn int_val(&self, value: Option<i32>) -> i32 {
|
||||
value.unwrap_or(10)
|
||||
}
|
||||
}
|
||||
|
||||
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
|
||||
let query = Request::new(
|
||||
r#"
|
||||
query QueryWithVariables($intVal: invalid = 2) {
|
||||
intVal(value: $intVal)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.variables(Variables::from_value(value!({
|
||||
"intVal": null,
|
||||
})));
|
||||
let resp = schema.execute(query).await;
|
||||
assert_eq!(
|
||||
resp.errors.first().map(|v| v.message.as_str()),
|
||||
Some("Unknown type \"invalid\"")
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue