diff --git a/src/type/__tests__/validation-test.ts b/src/type/__tests__/validation-test.ts index 5b9b1456904..1540b0f7209 100644 --- a/src/type/__tests__/validation-test.ts +++ b/src/type/__tests__/validation-test.ts @@ -1996,7 +1996,7 @@ describe('Type System: Argument default values must be valid', () => { ]); }); - it('Attempts to offer a suggested fix if possible', () => { + it('Attempts to offer a suggested fix if possible (programmatic)', () => { const Exotic = Symbol('Exotic'); const testEnum = new GraphQLEnumType({ @@ -2060,6 +2060,83 @@ describe('Type System: Argument default values must be valid', () => { }, ]); }); + + it('Attempts to offer a suggested fix if possible (SDL)', () => { + const originalSchema = buildSchema(` + enum TestEnum { + ONE + TWO + } + + input TestInput { + self: TestInput + string: [String]! + enum: [TestEnum] + } + + type Query { + field( + argWithPossibleFix: TestInput + argWithInvalidPossibleFix: TestInput + argWithoutPossibleFix: TestInput + ): Int + } + `); + + const Exotic = Symbol('Exotic'); + + // workaround as we cannot inject custom internal values into enums defined in SDL + const testEnum = new GraphQLEnumType({ + name: 'TestEnum', + values: { + ONE: { value: 1 }, + TWO: { value: Exotic }, + }, + }); + + const testInput = assertInputObjectType( + originalSchema.getType('TestInput'), + ); + testInput.getFields().enum.type = new GraphQLList(testEnum); + + // workaround as we cannot inject exotic default values into arguments defined in SDL + const QueryType = assertObjectType(originalSchema.getType('Query')); + for (const arg of QueryType.getFields().field.args) { + arg.type = testInput; + switch (arg.name) { + case 'argWithPossibleFix': + arg.defaultValue = { + value: { self: null, string: [1], enum: Exotic }, + }; + break; + case 'argWithInvalidPossibleFix': + arg.defaultValue = { value: { string: null } }; + break; + case 'argWithoutPossibleFix': + arg.defaultValue = { value: { enum: 'Exotic' } }; + break; + } + } + + expectJSON(validateSchema(originalSchema)).toDeepEqual([ + { + message: + 'Query.field(argWithPossibleFix:) has invalid default value: { self: null, string: [1], enum: Symbol(Exotic) }. Did you mean: { self: null, string: ["1"], enum: ["TWO"] }?', + }, + { + message: + 'Query.field(argWithInvalidPossibleFix:) has invalid default value at .string: Expected value of non-null type [String]! not to be null.', + }, + { + message: + 'Query.field(argWithoutPossibleFix:) has invalid default value: Expected value of type TestInput to include required field "string", found: { enum: "Exotic" }.', + }, + { + message: + 'Query.field(argWithoutPossibleFix:) has invalid default value at .enum: Value "Exotic" does not exist in "TestEnum" enum.', + }, + ]); + }); }); describe('Type System: Input Object fields must have input types', () => {