Skip to content

Commit

Permalink
add additional test case
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jan 2, 2023
1 parent 1022441 commit 8ea5217
Showing 1 changed file with 78 additions and 1 deletion.
79 changes: 78 additions & 1 deletion src/type/__tests__/validation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 8ea5217

Please sign in to comment.