From 2eeafec45cc1f5bc32933f31252644703659e5bc Mon Sep 17 00:00:00 2001 From: spawnia Date: Thu, 30 Sep 2021 14:02:46 +0200 Subject: [PATCH 1/2] Validate variables are known types Adds a check to the validation rule VariablesAreInputTypes to ensure that referenced types exist at all before checking they are input types. This check is not explicitly described in the specification for validating variables: http://spec.graphql.org/draft/#sec-Validation.Variables I am assuming this is simply an oversight and the rule is implicit. Still, it is valuable to check for it in order to provide a proper error to clients. --- .../VariablesAreInputTypesRule-test.ts | 17 +++++++++++++++++ .../rules/VariablesAreInputTypesRule.ts | 19 ++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/validation/__tests__/VariablesAreInputTypesRule-test.ts b/src/validation/__tests__/VariablesAreInputTypesRule-test.ts index 5a19fca650..9339aaa33c 100644 --- a/src/validation/__tests__/VariablesAreInputTypesRule-test.ts +++ b/src/validation/__tests__/VariablesAreInputTypesRule-test.ts @@ -21,6 +21,23 @@ describe('Validate: Variables are input types', () => { `); }); + it('unknown types are invalid', () => { + expectErrors(` + query Foo($a: Unknown, $b: [[Unknown!]]!) { + field(a: $a, b: $b) + } + `).to.deep.equal([ + { + locations: [{ line: 2, column: 21 }], + message: 'Variable "$a" references unknown type "Unknown".', + }, + { + locations: [{ line: 2, column: 34 }], + message: 'Variable "$b" references unknown type "[[Unknown!]]!".', + }, + ]); + }); + it('output types are invalid', () => { expectErrors(` query Foo($a: Dog, $b: [[CatOrDog!]]!, $c: Pet) { diff --git a/src/validation/rules/VariablesAreInputTypesRule.ts b/src/validation/rules/VariablesAreInputTypesRule.ts index 0dc9daa250..ad60f53d2f 100644 --- a/src/validation/rules/VariablesAreInputTypesRule.ts +++ b/src/validation/rules/VariablesAreInputTypesRule.ts @@ -23,13 +23,26 @@ export function VariablesAreInputTypesRule( VariableDefinition(node: VariableDefinitionNode) { const type = typeFromAST(context.getSchema(), node.type); - if (type && !isInputType(type)) { + if (!type) { const variableName = node.variable.name.value; - const typeName = print(node.type); + const typeReference = print(node.type); context.reportError( new GraphQLError( - `Variable "$${variableName}" cannot be non-input type "${typeName}".`, + `Variable "$${variableName}" references unknown type "${typeReference}".`, + node.type, + ), + ); + return; + } + + if (!isInputType(type)) { + const variableName = node.variable.name.value; + const typeReference = print(node.type); + + context.reportError( + new GraphQLError( + `Variable "$${variableName}" cannot be non-input type "${typeReference}".`, node.type, ), ); From d659b537b9fcb435d82142382a01e23b86c04c68 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Fri, 1 Oct 2021 11:39:35 +0300 Subject: [PATCH 2/2] review --- .../rules/VariablesAreInputTypesRule.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/validation/rules/VariablesAreInputTypesRule.ts b/src/validation/rules/VariablesAreInputTypesRule.ts index ad60f53d2f..38154e2fb5 100644 --- a/src/validation/rules/VariablesAreInputTypesRule.ts +++ b/src/validation/rules/VariablesAreInputTypesRule.ts @@ -22,27 +22,15 @@ export function VariablesAreInputTypesRule( return { VariableDefinition(node: VariableDefinitionNode) { const type = typeFromAST(context.getSchema(), node.type); - - if (!type) { - const variableName = node.variable.name.value; - const typeReference = print(node.type); - - context.reportError( - new GraphQLError( - `Variable "$${variableName}" references unknown type "${typeReference}".`, - node.type, - ), - ); - return; - } - if (!isInputType(type)) { const variableName = node.variable.name.value; const typeReference = print(node.type); context.reportError( new GraphQLError( - `Variable "$${variableName}" cannot be non-input type "${typeReference}".`, + type === undefined + ? `Variable "$${variableName}" references unknown type "${typeReference}".` + : `Variable "$${variableName}" cannot be non-input type "${typeReference}".`, node.type, ), );