Skip to content

Commit

Permalink
Allow for nullish values when defining enums
Browse files Browse the repository at this point in the history
Fixes #832
  • Loading branch information
leebyron committed Apr 29, 2017
1 parent d7fe661 commit b013e73
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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

0 comments on commit b013e73

Please sign in to comment.