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..38154e2fb5 100644 --- a/src/validation/rules/VariablesAreInputTypesRule.ts +++ b/src/validation/rules/VariablesAreInputTypesRule.ts @@ -22,14 +22,15 @@ export function VariablesAreInputTypesRule( return { VariableDefinition(node: VariableDefinitionNode) { const type = typeFromAST(context.getSchema(), node.type); - - if (type && !isInputType(type)) { + if (!isInputType(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}".`, + type === undefined + ? `Variable "$${variableName}" references unknown type "${typeReference}".` + : `Variable "$${variableName}" cannot be non-input type "${typeReference}".`, node.type, ), );