Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/validation/__tests__/VariablesAreInputTypesRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions src/validation/rules/VariablesAreInputTypesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
);
Expand Down