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

Allow for nullish values when defining enums #836

Merged
merged 1 commit into from
Apr 29, 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
27 changes: 27 additions & 0 deletions src/type/__tests__/definition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,33 @@ describe('Type System: Example', () => {
});
});

it('defines an enum type with a value of `null` and `undefined`', () => {
const EnumTypeWithNullishValue = new GraphQLEnumType({
name: 'EnumWithNullishValue',
values: {
NULL: { value: null },
UNDEFINED: { value: undefined },
}
});

expect(EnumTypeWithNullishValue.getValues()).to.deep.equal([
{
name: 'NULL',
description: undefined,
isDeprecated: false,
deprecationReason: undefined,
value: null,
},
{
name: 'UNDEFINED',
description: undefined,
isDeprecated: false,
deprecationReason: undefined,
value: undefined,
},
]);
});

it('defines an object type with deprecated field', () => {
const TypeWithDeprecatedField = new GraphQLObjectType({
name: 'foo',
Expand Down
3 changes: 1 addition & 2 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/

import invariant from '../jsutils/invariant';
import isNullish from '../jsutils/isNullish';
import { ENUM } from '../language/kinds';
import { assertValidName } from '../utilities/assertValidName';
import type {
Expand Down Expand Up @@ -984,7 +983,7 @@ function defineEnumValues(
description: value.description,
isDeprecated: Boolean(value.deprecationReason),
deprecationReason: value.deprecationReason,
value: isNullish(value.value) ? valueName : value.value,
value: value.hasOwnProperty('value') ? value.value : valueName,
};
});
}
Expand Down