Skip to content

Commit 8ea5217

Browse files
committed
add additional test case
1 parent 1022441 commit 8ea5217

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

src/type/__tests__/validation-test.ts

+78-1
Original file line numberDiff line numberDiff line change
@@ -1996,7 +1996,7 @@ describe('Type System: Argument default values must be valid', () => {
19961996
]);
19971997
});
19981998

1999-
it('Attempts to offer a suggested fix if possible', () => {
1999+
it('Attempts to offer a suggested fix if possible (programmatic)', () => {
20002000
const Exotic = Symbol('Exotic');
20012001

20022002
const testEnum = new GraphQLEnumType({
@@ -2060,6 +2060,83 @@ describe('Type System: Argument default values must be valid', () => {
20602060
},
20612061
]);
20622062
});
2063+
2064+
it('Attempts to offer a suggested fix if possible (SDL)', () => {
2065+
const originalSchema = buildSchema(`
2066+
enum TestEnum {
2067+
ONE
2068+
TWO
2069+
}
2070+
2071+
input TestInput {
2072+
self: TestInput
2073+
string: [String]!
2074+
enum: [TestEnum]
2075+
}
2076+
2077+
type Query {
2078+
field(
2079+
argWithPossibleFix: TestInput
2080+
argWithInvalidPossibleFix: TestInput
2081+
argWithoutPossibleFix: TestInput
2082+
): Int
2083+
}
2084+
`);
2085+
2086+
const Exotic = Symbol('Exotic');
2087+
2088+
// workaround as we cannot inject custom internal values into enums defined in SDL
2089+
const testEnum = new GraphQLEnumType({
2090+
name: 'TestEnum',
2091+
values: {
2092+
ONE: { value: 1 },
2093+
TWO: { value: Exotic },
2094+
},
2095+
});
2096+
2097+
const testInput = assertInputObjectType(
2098+
originalSchema.getType('TestInput'),
2099+
);
2100+
testInput.getFields().enum.type = new GraphQLList(testEnum);
2101+
2102+
// workaround as we cannot inject exotic default values into arguments defined in SDL
2103+
const QueryType = assertObjectType(originalSchema.getType('Query'));
2104+
for (const arg of QueryType.getFields().field.args) {
2105+
arg.type = testInput;
2106+
switch (arg.name) {
2107+
case 'argWithPossibleFix':
2108+
arg.defaultValue = {
2109+
value: { self: null, string: [1], enum: Exotic },
2110+
};
2111+
break;
2112+
case 'argWithInvalidPossibleFix':
2113+
arg.defaultValue = { value: { string: null } };
2114+
break;
2115+
case 'argWithoutPossibleFix':
2116+
arg.defaultValue = { value: { enum: 'Exotic' } };
2117+
break;
2118+
}
2119+
}
2120+
2121+
expectJSON(validateSchema(originalSchema)).toDeepEqual([
2122+
{
2123+
message:
2124+
'Query.field(argWithPossibleFix:) has invalid default value: { self: null, string: [1], enum: Symbol(Exotic) }. Did you mean: { self: null, string: ["1"], enum: ["TWO"] }?',
2125+
},
2126+
{
2127+
message:
2128+
'Query.field(argWithInvalidPossibleFix:) has invalid default value at .string: Expected value of non-null type [String]! not to be null.',
2129+
},
2130+
{
2131+
message:
2132+
'Query.field(argWithoutPossibleFix:) has invalid default value: Expected value of type TestInput to include required field "string", found: { enum: "Exotic" }.',
2133+
},
2134+
{
2135+
message:
2136+
'Query.field(argWithoutPossibleFix:) has invalid default value at .enum: Value "Exotic" does not exist in "TestEnum" enum.',
2137+
},
2138+
]);
2139+
});
20632140
});
20642141

20652142
describe('Type System: Input Object fields must have input types', () => {

0 commit comments

Comments
 (0)