|
| 1 | +use super::ValidationRule; |
| 2 | +use crate::ast::{AstTypeRef, TypeDefinitionExtension, TypeInfoQueryVisitor}; |
| 3 | +use crate::validation::utils::ValidationContext; |
| 4 | +use crate::validation::utils::{ValidationError, ValidationErrorContext}; |
| 5 | + |
| 6 | +/// Variables are input types |
| 7 | +/// |
| 8 | +/// A GraphQL operation is only valid if all the variables it defines are of |
| 9 | +/// input types (scalar, enum, or input object). |
| 10 | +/// |
| 11 | +/// See https://spec.graphql.org/draft/#sec-Variables-Are-Input-Types |
| 12 | +pub struct VariablesAreInputTypes; |
| 13 | + |
| 14 | +impl<'a> TypeInfoQueryVisitor<ValidationErrorContext<'a>> for VariablesAreInputTypes { |
| 15 | + fn enter_variable_definition( |
| 16 | + &self, |
| 17 | + _node: &crate::static_graphql::query::VariableDefinition, |
| 18 | + _parent_operation: &crate::static_graphql::query::OperationDefinition, |
| 19 | + _visitor_context: &mut ValidationErrorContext<'a>, |
| 20 | + _type_info: &crate::ast::TypeInfo, |
| 21 | + ) { |
| 22 | + if let Some(schema_type) = _visitor_context |
| 23 | + .ctx |
| 24 | + .find_schema_definition_by_name(_node.var_type.named_type()) |
| 25 | + { |
| 26 | + if !schema_type.is_input_type() { |
| 27 | + _visitor_context.report_error(ValidationError { |
| 28 | + message: format!( |
| 29 | + "Variable \"${}\" cannot be non-input type \"{}\".", |
| 30 | + _node.name, _node.var_type |
| 31 | + ), |
| 32 | + locations: vec![_node.position], |
| 33 | + }) |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl ValidationRule for VariablesAreInputTypes { |
| 40 | + fn validate<'a>(&self, ctx: &ValidationContext) -> Vec<ValidationError> { |
| 41 | + let mut error_context = ValidationErrorContext::new(ctx); |
| 42 | + |
| 43 | + if let Some(type_info_registry) = &ctx.type_info_registry { |
| 44 | + self.visit_document( |
| 45 | + &ctx.operation.clone(), |
| 46 | + &mut error_context, |
| 47 | + &type_info_registry, |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + error_context.errors |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[test] |
| 56 | +fn unknown_types_are_ignored() { |
| 57 | + use crate::validation::test_utils::*; |
| 58 | + |
| 59 | + let mut plan = create_plan_from_rule(Box::new(VariablesAreInputTypes {})); |
| 60 | + let errors = test_operation_with_schema( |
| 61 | + " |
| 62 | + query Foo($a: Unknown, $b: [[Unknown!]]!) { |
| 63 | + field(a: $a, b: $b) |
| 64 | + }", |
| 65 | + &TEST_SCHEMA, |
| 66 | + &mut plan, |
| 67 | + ); |
| 68 | + |
| 69 | + let messages = get_messages(&errors); |
| 70 | + assert_eq!(messages.len(), 0); |
| 71 | +} |
| 72 | + |
| 73 | +#[test] |
| 74 | +fn input_types_are_valid() { |
| 75 | + use crate::validation::test_utils::*; |
| 76 | + |
| 77 | + let mut plan = create_plan_from_rule(Box::new(VariablesAreInputTypes {})); |
| 78 | + let errors = test_operation_with_schema( |
| 79 | + " |
| 80 | + query Foo($a: String, $b: [Boolean!]!, $c: ComplexInput) { |
| 81 | + field(a: $a, b: $b, c: $c) |
| 82 | + }", |
| 83 | + &TEST_SCHEMA, |
| 84 | + &mut plan, |
| 85 | + ); |
| 86 | + |
| 87 | + let messages = get_messages(&errors); |
| 88 | + assert_eq!(messages.len(), 0); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn output_types_are_invalid() { |
| 93 | + use crate::validation::test_utils::*; |
| 94 | + |
| 95 | + let mut plan = create_plan_from_rule(Box::new(VariablesAreInputTypes {})); |
| 96 | + let errors = test_operation_with_schema( |
| 97 | + " |
| 98 | + query Foo($a: Dog, $b: [[CatOrDog!]]!, $c: Pet) { |
| 99 | + field(a: $a, b: $b, c: $c) |
| 100 | + }", |
| 101 | + &TEST_SCHEMA, |
| 102 | + &mut plan, |
| 103 | + ); |
| 104 | + |
| 105 | + let messages = get_messages(&errors); |
| 106 | + assert_eq!(messages.len(), 3); |
| 107 | + assert_eq!( |
| 108 | + messages, |
| 109 | + vec![ |
| 110 | + "Variable \"$a\" cannot be non-input type \"Dog\".", |
| 111 | + "Variable \"$b\" cannot be non-input type \"[[CatOrDog!]]!\".", |
| 112 | + "Variable \"$c\" cannot be non-input type \"Pet\".", |
| 113 | + ] |
| 114 | + ); |
| 115 | +} |
0 commit comments