Skip to content

Fix input coercion to allow null-valued Enums as arguments #848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/utilities/__tests__/valueFromAST-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ describe('valueFromAST', () => {

const testEnum = new GraphQLEnumType({
name: 'TestColor',
values: { RED: { value: 1 }, GREEN: { value: 2 }, BLUE: { value: 3 } }
values: {
RED: { value: 1 },
GREEN: { value: 2 },
BLUE: { value: 3 },
NULL: { value: null },
UNDEFINED: { value: undefined }
}
});

it('converts enum values according to input coercion rules', () => {
Expand All @@ -75,6 +81,8 @@ describe('valueFromAST', () => {
testCase(testEnum, '3', undefined);
testCase(testEnum, '"BLUE"', undefined);
testCase(testEnum, 'null', null);
testCase(testEnum, 'NULL', null);
testCase(testEnum, 'UNDEFINED', undefined);
});

// Boolean!
Expand Down
7 changes: 4 additions & 3 deletions src/utilities/isValidLiteralValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ export function isValidLiteralValue(
'Must be input type'
);

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

Expand Down
8 changes: 5 additions & 3 deletions src/utilities/valueFromAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ export function valueFromAST(
);

const parsed = type.parseLiteral(valueNode);
if (isNullish(parsed)) {
// null or invalid values represent a failure to parse correctly,
// in which case no value is returned.
if (type instanceof GraphQLEnumType ?
!type.serialize(parsed) : isNullish(parsed)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this condition is a little gross now. I wonder if we should instead have something like type.isValidValue() or something along those lines to simplify this?

// null or invalid values represent a failure to parse correctly (unless
// we have a legitimately null-valued Enum), in which case no value is
// returned.
return;
}

Expand Down
20 changes: 20 additions & 0 deletions src/validation/__tests__/ArgumentsOfCorrectType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ describe('Validate: Argument values of correct type', () => {
`);
});

it('Enum with undefined value', () => {
expectPassesRule(ArgumentsOfCorrectType, `
{
complicatedArgs {
enumArgField(enumArg: UNKNOWN)
}
}
`);
});

it('Enum with null value', () => {
expectPassesRule(ArgumentsOfCorrectType, `
{
complicatedArgs {
enumArgField(enumArg: NO_FUR)
}
}
`);
});

it('null into nullable type', () => {
expectPassesRule(ArgumentsOfCorrectType, `
{
Expand Down
2 changes: 2 additions & 0 deletions src/validation/__tests__/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ const FurColor = new GraphQLEnumType({
BLACK: { value: 1 },
TAN: { value: 2 },
SPOTTED: { value: 3 },
NO_FUR: { value: null },
UNKNOWN: { value: undefined }
},
});

Expand Down