Skip to content

Commit ae4e0d9

Browse files
committed
Clearer error messages
This adds clearer errors for incorrect variables. Does not wrap printed values in quotes, to avoid ambiguity with strings.
1 parent c3e94a3 commit ae4e0d9

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

src/execution/__tests__/variables.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ describe('Execute: Handles inputs', () => {
231231
expect(caughtError).to.containSubset({
232232
locations: [ { line: 2, column: 17 } ],
233233
message:
234-
'Variable $input expected value of type TestInputObject but ' +
234+
'Variable "$input" expected value of type "TestInputObject" but ' +
235235
'got: {"a":"foo","b":"bar","c":null}.'
236236
});
237237
});
@@ -249,7 +249,7 @@ describe('Execute: Handles inputs', () => {
249249
expect(caughtError).to.containSubset({
250250
locations: [ { line: 2, column: 17 } ],
251251
message:
252-
'Variable $input expected value of type TestInputObject but ' +
252+
'Variable "$input" expected value of type "TestInputObject" but ' +
253253
'got: "foo bar".'
254254
});
255255
});
@@ -267,7 +267,7 @@ describe('Execute: Handles inputs', () => {
267267
expect(caughtError).to.containSubset({
268268
locations: [ { line: 2, column: 17 } ],
269269
message:
270-
'Variable $input expected value of type TestInputObject but ' +
270+
'Variable "$input" expected value of type "TestInputObject" but ' +
271271
'got: {"a":"foo","b":"bar"}.'
272272
});
273273
});
@@ -285,7 +285,7 @@ describe('Execute: Handles inputs', () => {
285285
expect(caughtError).to.containSubset({
286286
locations: [ { line: 2, column: 17 } ],
287287
message:
288-
'Variable $input expected value of type TestInputObject but ' +
288+
'Variable "$input" expected value of type "TestInputObject" but ' +
289289
'got: {"a":"foo","b":"bar","c":"baz","d":"dog"}.'
290290
});
291291
});
@@ -407,7 +407,7 @@ describe('Execute: Handles inputs', () => {
407407
expect(caughtError).to.containSubset({
408408
locations: [ { line: 2, column: 31 } ],
409409
message:
410-
'Variable $value expected value of type String! but got: undefined.'
410+
'Variable "$value" of required type "String!" was not provided.'
411411
});
412412
});
413413

@@ -429,7 +429,7 @@ describe('Execute: Handles inputs', () => {
429429
expect(caughtError).to.containSubset({
430430
locations: [ { line: 2, column: 31 } ],
431431
message:
432-
'Variable $value expected value of type String! but got: null.'
432+
'Variable "$value" of required type "String!" was not provided.'
433433
});
434434
});
435435

@@ -551,7 +551,7 @@ describe('Execute: Handles inputs', () => {
551551
expect(caughtError).to.containSubset({
552552
locations: [ { line: 2, column: 17 } ],
553553
message:
554-
'Variable $input expected value of type [String]! but got: null.'
554+
'Variable "$input" of required type "[String]!" was not provided.'
555555
});
556556
});
557557

@@ -642,7 +642,7 @@ describe('Execute: Handles inputs', () => {
642642
expect(caughtError).to.containSubset({
643643
locations: [ { line: 2, column: 17 } ],
644644
message:
645-
'Variable $input expected value of type [String!] but got: ' +
645+
'Variable "$input" expected value of type "[String!]" but got: ' +
646646
'["A",null,"B"].'
647647
});
648648
});
@@ -665,7 +665,7 @@ describe('Execute: Handles inputs', () => {
665665
expect(caughtError).to.containSubset({
666666
locations: [ { line: 2, column: 17 } ],
667667
message:
668-
'Variable $input expected value of type [String!]! but got: null.'
668+
'Variable "$input" of required type "[String!]!" was not provided.'
669669
});
670670
});
671671

@@ -705,7 +705,7 @@ describe('Execute: Handles inputs', () => {
705705
expect(caughtError).to.containSubset({
706706
locations: [ { line: 2, column: 17 } ],
707707
message:
708-
'Variable $input expected value of type [String!]! but got: ' +
708+
'Variable "$input" expected value of type "[String!]!" but got: ' +
709709
'["A",null,"B"].'
710710
});
711711
});
@@ -729,7 +729,7 @@ describe('Execute: Handles inputs', () => {
729729
expect(caughtError).to.containSubset({
730730
locations: [ { line: 2, column: 17 } ],
731731
message:
732-
'Variable $input expected value of type TestType! which cannot ' +
732+
'Variable "$input" expected value of type "TestType!" which cannot ' +
733733
'be used as an input type.'
734734
});
735735
});
@@ -753,8 +753,8 @@ describe('Execute: Handles inputs', () => {
753753
expect(caughtError).to.containSubset({
754754
locations: [ { line: 2, column: 17 } ],
755755
message:
756-
'Variable $input expected value of type UnknownType! which cannot ' +
757-
'be used as an input type.'
756+
'Variable "$input" expected value of type "UnknownType!" which ' +
757+
'cannot be used as an input type.'
758758
});
759759
});
760760

src/execution/values.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ function getVariableValue(
8585
input: ?any
8686
): any {
8787
var type = typeFromAST(schema, definitionAST.type);
88+
var variable = definitionAST.variable;
8889
if (!type || !isInputType(type)) {
8990
throw new GraphQLError(
90-
`Variable $${definitionAST.variable.name.value} expected value of type ` +
91-
`${print(definitionAST.type)} which cannot be used as an input type.`,
91+
`Variable "$${variable.name.value}" expected value of type ` +
92+
`"${print(definitionAST.type)}" which cannot be used as an input type.`,
9293
[ definitionAST ]
9394
);
9495
}
@@ -101,9 +102,16 @@ function getVariableValue(
101102
}
102103
return coerceValue(type, input);
103104
}
105+
if (isNullish(input)) {
106+
throw new GraphQLError(
107+
`Variable "$${variable.name.value}" of required type ` +
108+
`"${print(definitionAST.type)}" was not provided.`,
109+
[ definitionAST ]
110+
);
111+
}
104112
throw new GraphQLError(
105-
`Variable $${definitionAST.variable.name.value} expected value of type ` +
106-
`${print(definitionAST.type)} but got: ${JSON.stringify(input)}.`,
113+
`Variable "$${variable.name.value}" expected value of type ` +
114+
`"${print(definitionAST.type)}" but got: ${JSON.stringify(input)}.`,
107115
[ definitionAST ]
108116
);
109117
}

src/validation/rules/ArgumentsOfCorrectType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { isValidLiteralValue } from '../../utilities/isValidLiteralValue';
1515

1616

1717
export function badValueMessage(argName: any, type: any, value: any): string {
18-
return `Argument "${argName}" expected type "${type}" but got: "${value}".`;
18+
return `Argument "${argName}" expected type "${type}" but got: ${value}.`;
1919
}
2020

2121
/**

src/validation/rules/DefaultValuesOfCorrectType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function badValueForDefaultArgMessage(
3030
value: any
3131
): string {
3232
return `Variable "$${varName}" of type "${type}" has invalid default ` +
33-
`value: "${value}".`;
33+
`value: ${value}.`;
3434
}
3535

3636
/**

0 commit comments

Comments
 (0)