Skip to content

Commit fcecd91

Browse files
committed
standardize error messages prior to introducing schema coordinates
extracted from graphql#3808 PR graphql#3808 uses schema coordinates to improve GraphQL-JS error messages. To reduce the size of the PR, this commit standardizes error messages according to the general pattern that will be introduced with schema coordinates without introducing the coordinates themselves, in the hopes of aiding review of the later PR. Parenthetically, extracting these changes out of graphql#3808 demonstrates how the use of schema coordinates improves our error messages. For example, it is difficult when printing an error message regarding an argument to easily look up the field of that argument, and, if it a nested input field, the full field path. It is similarly sometimes difficult to know the type of the field or argument triggering the error. Embedding schema coordinates within GraphQL-JS makes this trivial. This PR simply omits the changes from graphql#3808 that are directly enabled by the use of schema coordinates. graphql#3808 can then be rebased on top of this PR to demonstrate the code changes and the direct improvement in the various error messages.
1 parent 5c7d4d1 commit fcecd91

19 files changed

+148
-131
lines changed

src/execution/__tests__/variables-test.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ describe('Execute: Handles inputs', () => {
226226
errors: [
227227
{
228228
message:
229-
'Argument "input" has invalid value ["foo", "bar", "baz"].',
229+
'Argument "input" of type "TestInputObject" has invalid value ["foo", "bar", "baz"].',
230230
path: ['fieldWithObjectInput'],
231231
locations: [{ line: 3, column: 41 }],
232232
},
@@ -262,7 +262,7 @@ describe('Execute: Handles inputs', () => {
262262
errors: [
263263
{
264264
message:
265-
'Argument "input" has invalid value { c: "foo", e: "bar" }.',
265+
'Argument "input" of type "TestInputObject" has invalid value { c: "foo", e: "bar" }.',
266266
path: ['fieldWithObjectInput'],
267267
locations: [{ line: 3, column: 41 }],
268268
},
@@ -462,7 +462,7 @@ describe('Execute: Handles inputs', () => {
462462
errors: [
463463
{
464464
message:
465-
'Variable "$input" got invalid value { a: "foo", b: "bar" }; Field "c" of required type "String!" was not provided.',
465+
'Variable "$input" got invalid value { a: "foo", b: "bar" }; Field "TestInputObject.c" of required type "String!" was not provided.',
466466
locations: [{ line: 2, column: 16 }],
467467
},
468468
],
@@ -481,12 +481,12 @@ describe('Execute: Handles inputs', () => {
481481
errors: [
482482
{
483483
message:
484-
'Variable "$input" got invalid value { a: "foo" } at "input.na"; Field "c" of required type "String!" was not provided.',
484+
'Variable "$input" got invalid value { a: "foo" } at "input.na"; Field "TestInputObject.c" of required type "String!" was not provided.',
485485
locations: [{ line: 2, column: 18 }],
486486
},
487487
{
488488
message:
489-
'Variable "$input" got invalid value { na: { a: "foo" } }; Field "nb" of required type "String!" was not provided.',
489+
'Variable "$input" got invalid value { na: { a: "foo" } }; Field "TestNestedInputObject.nb" of required type "String!" was not provided.',
490490
locations: [{ line: 2, column: 18 }],
491491
},
492492
],
@@ -1042,7 +1042,8 @@ describe('Execute: Handles inputs', () => {
10421042
},
10431043
errors: [
10441044
{
1045-
message: 'Argument "input" has invalid value WRONG_TYPE.',
1045+
message:
1046+
'Argument "input" of type "String" has invalid value WRONG_TYPE.',
10461047
locations: [{ line: 3, column: 48 }],
10471048
path: ['fieldWithDefaultArgumentValue'],
10481049
},

src/execution/values.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ export function getArgumentValues(
215215
// execution. This is a runtime check to ensure execution does not
216216
// continue with an invalid argument value.
217217
throw new GraphQLError(
218-
`Argument "${name}" has invalid value ${print(valueNode)}.`,
218+
`Argument "${name}" of type "${inspect(
219+
argType,
220+
)}" has invalid value ${print(valueNode)}.`,
219221
{ nodes: valueNode },
220222
);
221223
}

src/type/__tests__/introspection-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,7 @@ describe('Introspection', () => {
16441644
errors: [
16451645
{
16461646
message:
1647-
'Field "__type" argument "name" of type "String!" is required, but it was not provided.',
1647+
'Argument "__type(name:)" of type "String!" is required, but it was not provided.',
16481648
locations: [{ line: 3, column: 9 }],
16491649
},
16501650
],

src/type/__tests__/validation-test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,7 @@ describe('Objects must adhere to Interface they implement', () => {
20892089
expectJSON(validateSchema(schema)).toDeepEqual([
20902090
{
20912091
message:
2092-
'Object field AnotherObject.field includes required argument requiredArg that is missing from the Interface field AnotherInterface.field.',
2092+
'Argument "AnotherObject.field(requiredArg:)" must not be required type "String!" if not provided by the Interface field "AnotherInterface.field".',
20932093
locations: [
20942094
{ line: 13, column: 11 },
20952095
{ line: 7, column: 9 },
@@ -2546,7 +2546,7 @@ describe('Interfaces must adhere to Interface they implement', () => {
25462546
expectJSON(validateSchema(schema)).toDeepEqual([
25472547
{
25482548
message:
2549-
'Object field ChildInterface.field includes required argument requiredArg that is missing from the Interface field ParentInterface.field.',
2549+
'Argument "ChildInterface.field(requiredArg:)" must not be required type "String!" if not provided by the Interface field "ParentInterface.field".',
25502550
locations: [
25512551
{ line: 13, column: 11 },
25522552
{ line: 7, column: 9 },

src/type/validate.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,13 @@ function validateTypeImplementsInterface(
433433
const ifaceArg = ifaceField.args.find((arg) => arg.name === argName);
434434
if (!ifaceArg && isRequiredArgument(typeArg)) {
435435
context.reportError(
436-
`Object field ${type.name}.${fieldName} includes required argument ${argName} that is missing from the Interface field ${iface.name}.${fieldName}.`,
436+
`Argument "${
437+
type.name
438+
}.${fieldName}(${argName}:)" must not be required type "${inspect(
439+
typeArg.type,
440+
)}" if not provided by the Interface field "${
441+
iface.name
442+
}.${fieldName}".`,
437443
[typeArg.astNode, ifaceField.astNode],
438444
);
439445
}

src/utilities/__tests__/coerceInputValue-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ describe('coerceInputValue', () => {
238238
const result = coerceValue({ bar: 123 }, TestInputObject);
239239
expectErrors(result).to.deep.equal([
240240
{
241-
error: 'Field "foo" of required type "Int!" was not provided.',
241+
error: 'Field "TestInputObject.foo" of required type "Int!" was not provided.',
242242
path: [],
243243
value: { bar: 123 },
244244
},

0 commit comments

Comments
 (0)