diff --git a/src/execution/__tests__/oneof-test.ts b/src/execution/__tests__/oneof-test.ts index 0416cccd2d..85153c244a 100644 --- a/src/execution/__tests__/oneof-test.ts +++ b/src/execution/__tests__/oneof-test.ts @@ -83,7 +83,7 @@ describe('Execute: Handles OneOf Input Objects', () => { { locations: [{ column: 16, line: 2 }], message: - 'Variable "$input" has invalid default value: OneOf Input Object "TestInputObject" must specify exactly one key.', + 'Variable "$input" has invalid default value: Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', }, ], }); @@ -148,7 +148,7 @@ describe('Execute: Handles OneOf Input Objects', () => { errors: [ { message: - 'Variable "$input" has invalid value: Field "a" for OneOf type "TestInputObject" must be non-null.', + 'Variable "$input" has invalid value at .a: Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', locations: [{ line: 2, column: 16 }], }, ], @@ -173,7 +173,7 @@ describe('Execute: Handles OneOf Input Objects', () => { { locations: [{ column: 16, line: 2 }], message: - 'Variable "$input" has invalid value: Exactly one key must be specified for OneOf type "TestInputObject".', + 'Variable "$input" has invalid value: Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', }, ], }); @@ -197,7 +197,7 @@ describe('Execute: Handles OneOf Input Objects', () => { { locations: [{ column: 16, line: 2 }], message: - 'Variable "$input" has invalid value: Exactly one key must be specified for OneOf type "TestInputObject".', + 'Variable "$input" has invalid value: Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', }, ], }); diff --git a/src/utilities/__tests__/validateInputValue-test.ts b/src/utilities/__tests__/validateInputValue-test.ts index 6aaae7188a..82c6814b56 100644 --- a/src/utilities/__tests__/validateInputValue-test.ts +++ b/src/utilities/__tests__/validateInputValue-test.ts @@ -395,7 +395,7 @@ describe('validateInputValue', () => { test({ foo: 123, bar: null }, TestInputObject, [ { error: - 'Exactly one key must be specified for OneOf type "TestInputObject".', + 'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', path: [], }, ]); @@ -409,8 +409,8 @@ describe('validateInputValue', () => { test({ bar: null }, TestInputObject, [ { error: - 'Field "bar" for OneOf type "TestInputObject" must be non-null.', - path: [], + 'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', + path: ['bar'], }, ]); }); @@ -436,7 +436,7 @@ describe('validateInputValue', () => { }, { error: - 'Exactly one key must be specified for OneOf type "TestInputObject".', + 'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', path: [], }, ]); @@ -452,7 +452,7 @@ describe('validateInputValue', () => { }, { error: - 'Exactly one key must be specified for OneOf type "TestInputObject".', + 'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', path: [], }, ]); @@ -980,7 +980,7 @@ describe('validateInputLiteral', () => { test('{ foo: 123, bar: null }', TestInputObject, [ { error: - 'OneOf Input Object "TestInputObject" must specify exactly one key.', + 'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', path: [], }, ]); @@ -990,7 +990,7 @@ describe('validateInputLiteral', () => { test('{ bar: null }', TestInputObject, [ { error: - 'Field "TestInputObject.bar" used for OneOf Input Object must be non-null.', + 'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', path: ['bar'], }, ]); @@ -1025,7 +1025,7 @@ describe('validateInputLiteral', () => { }, { error: - 'OneOf Input Object "TestInputObject" must specify exactly one key.', + 'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', path: [], }, ]); @@ -1041,7 +1041,7 @@ describe('validateInputLiteral', () => { }, { error: - 'OneOf Input Object "TestInputObject" must specify exactly one key.', + 'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', path: [], }, ]); @@ -1087,7 +1087,7 @@ describe('validateInputLiteral', () => { }, { error: - 'OneOf Input Object "TestInputObject" must specify exactly one key.', + 'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', path: [], }, ]); @@ -1105,7 +1105,7 @@ describe('validateInputLiteral', () => { }, { error: - 'OneOf Input Object "TestInputObject" must specify exactly one key.', + 'Within OneOf Input Object type "TestInputObject", exactly one field must be specified, and the value for that field must be non-null.', path: [], }, ], diff --git a/src/utilities/validateInputValue.ts b/src/utilities/validateInputValue.ts index abd45fb41e..865a7b81a1 100644 --- a/src/utilities/validateInputValue.ts +++ b/src/utilities/validateInputValue.ts @@ -169,7 +169,7 @@ function validateInputValueImpl( if (fields.length !== 1) { reportInvalidValue( onError, - `Exactly one key must be specified for OneOf type "${type}".`, + getOneOfInputObjectErrorMessage(type), path, ); } @@ -179,8 +179,8 @@ function validateInputValueImpl( if (value === null) { reportInvalidValue( onError, - `Field "${field}" for OneOf type "${type}" must be non-null.`, - path, + getOneOfInputObjectErrorMessage(type), + addPath(path, field, type.name), ); } } @@ -437,7 +437,7 @@ function validateInputLiteralImpl( if (isNotExactlyOneField) { reportInvalidLiteral( context.onError, - `OneOf Input Object "${type}" must specify exactly one key.`, + getOneOfInputObjectErrorMessage(type), valueNode, path, ); @@ -449,7 +449,7 @@ function validateInputLiteralImpl( const fieldName = fields[0].name.value; reportInvalidLiteral( context.onError, - `Field "${type}.${fieldName}" used for OneOf Input Object must be non-null.`, + getOneOfInputObjectErrorMessage(type), valueNode, addPath(path, fieldName, undefined), ); @@ -532,3 +532,7 @@ function getCaughtErrorMessage(caughtError: unknown): string { return String(caughtError); } + +function getOneOfInputObjectErrorMessage(type: GraphQLInputType): string { + return `Within OneOf Input Object type "${type}", exactly one field must be specified, and the value for that field must be non-null.`; +} diff --git a/src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts b/src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts index 9631c1ae05..38dbcdcfd5 100644 --- a/src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts +++ b/src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts @@ -1147,7 +1147,7 @@ describe('Validate: Values of correct type', () => { `).toDeepEqual([ { message: - 'Field "OneOfInput.stringField" used for OneOf Input Object must be non-null.', + 'Within OneOf Input Object type "OneOfInput", exactly one field must be specified, and the value for that field must be non-null.', locations: [{ line: 4, column: 37 }], }, ]); @@ -1163,7 +1163,7 @@ describe('Validate: Values of correct type', () => { `).toDeepEqual([ { message: - 'OneOf Input Object "OneOfInput" must specify exactly one key.', + 'Within OneOf Input Object type "OneOfInput", exactly one field must be specified, and the value for that field must be non-null.', locations: [{ line: 4, column: 37 }], }, ]);