Skip to content
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

[Bug Reproduce] Can't use undefined and NaN as enum values #1267

Merged
merged 1 commit into from
Jun 5, 2018
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
55 changes: 55 additions & 0 deletions src/execution/__tests__/variables-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
GraphQLString,
GraphQLNonNull,
GraphQLScalarType,
GraphQLEnumType,
} from '../../type';

const TestComplexScalar = new GraphQLScalarType({
Expand Down Expand Up @@ -60,6 +61,18 @@ const TestNestedInputObject = new GraphQLInputObjectType({
},
});

const TestEnum = new GraphQLEnumType({
name: 'TestEnum',
values: {
NULL: { value: null },
UNDEFINED: { value: undefined },
NAN: { value: NaN },
FALSE: { value: false },
CUSTOM: { value: 'custom value' },
DEFAULT_VALUE: {},
},
});

function fieldWithInputArg(inputArg) {
return {
type: GraphQLString,
Expand All @@ -75,6 +88,10 @@ function fieldWithInputArg(inputArg) {
const TestType = new GraphQLObjectType({
name: 'TestType',
fields: {
fieldWithEnumInput: fieldWithInputArg({ type: TestEnum }),
fieldWithNonNullableEnumInput: fieldWithInputArg({
type: GraphQLNonNull(TestEnum),
}),
fieldWithObjectInput: fieldWithInputArg({ type: TestInputObject }),
fieldWithNullableStringInput: fieldWithInputArg({ type: GraphQLString }),
fieldWithNonNullableStringInput: fieldWithInputArg({
Expand Down Expand Up @@ -439,6 +456,44 @@ describe('Execute: Handles inputs', () => {
});
});

describe('Handles custom enum values', () => {
it('allows custom enum values as inputs', () => {
const result = executeQuery(`
{
null: fieldWithEnumInput(input: NULL)
NaN: fieldWithEnumInput(input: NAN)
false: fieldWithEnumInput(input: FALSE)
customValue: fieldWithEnumInput(input: CUSTOM)
defaultValue: fieldWithEnumInput(input: DEFAULT_VALUE)
}
`);

expect(result).to.deep.equal({
data: {
null: 'null',
NaN: 'NaN',
false: 'false',
customValue: "'custom value'",
defaultValue: "'DEFAULT_VALUE'",
},
});
});

it('allows non-nullable inputs to have null as enum custom value', () => {
const result = executeQuery(`
{
fieldWithNonNullableEnumInput(input: NULL)
}
`);

expect(result).to.deep.equal({
data: {
fieldWithNonNullableEnumInput: 'null',
},
});
});
});

describe('Handles nullable scalars', () => {
it('allows nullable inputs to be omitted', () => {
const result = executeQuery(`
Expand Down
2 changes: 2 additions & 0 deletions src/utilities/__tests__/valueFromAST-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('valueFromAST', () => {
BLUE: { value: 3 },
NULL: { value: null },
UNDEFINED: { value: undefined },
NAN: { value: NaN },
},
});

Expand All @@ -77,6 +78,7 @@ describe('valueFromAST', () => {
testCase(testEnum, 'null', null);
testCase(testEnum, 'NULL', null);
testCase(testEnum, 'UNDEFINED', undefined);
testCase(testEnum, 'NAN', NaN);
});

// Boolean!
Expand Down