diff --git a/src/validation/rules/default_values_of_correct_type.rs b/src/validation/rules/default_values_of_correct_type.rs index 1b14181e..0ae56a99 100644 --- a/src/validation/rules/default_values_of_correct_type.rs +++ b/src/validation/rules/default_values_of_correct_type.rs @@ -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, ) { + 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!( diff --git a/tests/variables.rs b/tests/variables.rs index 8cd8c376..70b0f8bb 100644 --- a/tests/variables.rs +++ b/tests/variables.rs @@ -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 { + 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 { + 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\"") + ); +}