Skip to content

Commit ff845f7

Browse files
exogenleebyron
authored andcommitted
Fix input coercion to allow null-valued Enums as arguments (#848)
* Fix input coercion to allow null-valued Enums as arguments * Add tests for ArgumentsOfCorrectType with null and undefined Enum values
1 parent 387b41a commit ff845f7

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

src/utilities/__tests__/valueFromAST-test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ describe('valueFromAST', () => {
6666

6767
const testEnum = new GraphQLEnumType({
6868
name: 'TestColor',
69-
values: { RED: { value: 1 }, GREEN: { value: 2 }, BLUE: { value: 3 } }
69+
values: {
70+
RED: { value: 1 },
71+
GREEN: { value: 2 },
72+
BLUE: { value: 3 },
73+
NULL: { value: null },
74+
UNDEFINED: { value: undefined }
75+
}
7076
});
7177

7278
it('converts enum values according to input coercion rules', () => {
@@ -75,6 +81,8 @@ describe('valueFromAST', () => {
7581
testCase(testEnum, '3', undefined);
7682
testCase(testEnum, '"BLUE"', undefined);
7783
testCase(testEnum, 'null', null);
84+
testCase(testEnum, 'NULL', null);
85+
testCase(testEnum, 'UNDEFINED', undefined);
7886
});
7987

8088
// Boolean!

src/utilities/isValidLiteralValue.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ export function isValidLiteralValue(
115115
'Must be input type'
116116
);
117117

118-
// Scalar/Enum input checks to ensure the type can parse the value to
119-
// a non-null value.
118+
// Scalars must parse to a non-null value, Enums may be null but must
119+
// serialize back to a named value.
120120
const parseResult = type.parseLiteral(valueNode);
121-
if (isNullish(parseResult)) {
121+
if (type instanceof GraphQLEnumType ?
122+
!type.serialize(parseResult) : isNullish(parseResult)) {
122123
return [ `Expected type "${type.name}", found ${print(valueNode)}.` ];
123124
}
124125

src/utilities/valueFromAST.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ export function valueFromAST(
152152
);
153153

154154
const parsed = type.parseLiteral(valueNode);
155-
if (isNullish(parsed)) {
156-
// null or invalid values represent a failure to parse correctly,
157-
// in which case no value is returned.
155+
if (type instanceof GraphQLEnumType ?
156+
!type.serialize(parsed) : isNullish(parsed)) {
157+
// null or invalid values represent a failure to parse correctly (unless
158+
// we have a legitimately null-valued Enum), in which case no value is
159+
// returned.
158160
return;
159161
}
160162

src/validation/__tests__/ArgumentsOfCorrectType-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ describe('Validate: Argument values of correct type', () => {
116116
`);
117117
});
118118

119+
it('Enum with undefined value', () => {
120+
expectPassesRule(ArgumentsOfCorrectType, `
121+
{
122+
complicatedArgs {
123+
enumArgField(enumArg: UNKNOWN)
124+
}
125+
}
126+
`);
127+
});
128+
129+
it('Enum with null value', () => {
130+
expectPassesRule(ArgumentsOfCorrectType, `
131+
{
132+
complicatedArgs {
133+
enumArgField(enumArg: NO_FUR)
134+
}
135+
}
136+
`);
137+
});
138+
119139
it('null into nullable type', () => {
120140
expectPassesRule(ArgumentsOfCorrectType, `
121141
{

src/validation/__tests__/harness.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ const FurColor = new GraphQLEnumType({
189189
BLACK: { value: 1 },
190190
TAN: { value: 2 },
191191
SPOTTED: { value: 3 },
192+
NO_FUR: { value: null },
193+
UNKNOWN: { value: undefined }
192194
},
193195
});
194196

0 commit comments

Comments
 (0)