Skip to content

Commit d40858d

Browse files
committed
Update buildEnum.js
1 parent 1b862c6 commit d40858d

File tree

2 files changed

+59
-43
lines changed

2 files changed

+59
-43
lines changed

src/build/__tests__/buildEnum-test.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,42 @@ import { GraphQLEnumType } from 'graphql/type';
22
import { parse } from 'graphql/language';
33
import buildEnum, { buildEnumValueConfigMap } from '../buildEnum';
44

5-
const generateEnumAST = ({ description, name = 'Enum', values = ['VALUE'] } = {}) => parse(`
5+
const generateEnumNode = ({ description, name = 'Enum', values = ['VALUE'] } = {}) => parse(`
66
# ${description === undefined ? '' : description}
77
enum ${name} {
88
${values.join('\n')}
99
}
1010
`).definitions[0];
1111
const generateEnumValueConfigMap = ({
1212
name = 'VALUE',
13-
value = 'VALUE',
14-
description,
13+
value,
1514
deprecationReason,
16-
} = {}) => ({ [name]: { value, description, deprecationReason } });
15+
description,
16+
} = {}) => (name ? ({ [name]: { value, deprecationReason, description } }) : {});
1717

1818
describe('buildEnumValueConfigMap()', () => {
19-
it('should work with minimal AST', () => {
20-
expect(buildEnumValueConfigMap(generateEnumAST())).toEqual(generateEnumValueConfigMap());
19+
it('builds with minimal AST', () => {
20+
expect(buildEnumValueConfigMap(generateEnumNode())).toEqual(generateEnumValueConfigMap());
2121
});
2222

23-
it('should work with description in AST', () => {
23+
it('builds with description in AST', () => {
2424
const description = 'Another description.';
25-
expect(buildEnumValueConfigMap(generateEnumAST({
25+
expect(buildEnumValueConfigMap(generateEnumNode({
2626
values: [`# ${description}\nVALUE`],
2727
}))).toEqual(generateEnumValueConfigMap({ description }));
2828
});
2929

30-
it('should work with `value` in config map', () => {
31-
const value = 'value';
32-
expect(buildEnumValueConfigMap(generateEnumAST(), { VALUE: { value } }))
33-
.toEqual(generateEnumValueConfigMap({ value }));
30+
it('builds with deprecation directive in AST', () => {
31+
const deprecationReason = 'A reason.';
32+
expect(buildEnumValueConfigMap(generateEnumNode({
33+
values: [`VALUE @deprecated(reason: "${deprecationReason}")`],
34+
}))).toEqual(generateEnumValueConfigMap({ deprecationReason }));
3435
});
3536

36-
it('should work with `deprecationReason` in config map', () => {
37-
const deprecationReason = 'A reason.';
38-
expect(buildEnumValueConfigMap(generateEnumAST(), { VALUE: { deprecationReason } }))
39-
.toEqual(generateEnumValueConfigMap({ deprecationReason }));
37+
it('builds with `value` in config map', () => {
38+
const value = 'value';
39+
expect(buildEnumValueConfigMap(generateEnumNode(), { VALUE: { value } }))
40+
.toEqual(generateEnumValueConfigMap({ value }));
4041
});
4142
});
4243

@@ -47,17 +48,17 @@ describe('buildEnum()', () => {
4748
description,
4849
} = {}) => new GraphQLEnumType({ name, values, description });
4950

50-
it('should work with minimal AST', () => {
51-
expect(buildEnum(generateEnumAST())).toEqual(generateEnumType());
51+
it('builds with minimal AST', () => {
52+
expect(buildEnum(generateEnumNode())).toEqual(generateEnumType());
5253
});
5354

54-
it('should work with description in AST', () => {
55+
it('builds with description in AST', () => {
5556
const config = { description: 'A description.' };
56-
expect(buildEnum(generateEnumAST(config))).toEqual(generateEnumType(config));
57+
expect(buildEnum(generateEnumNode(config))).toEqual(generateEnumType(config));
5758
});
5859

59-
it('should work with `values` in config', () => {
60+
it('builds with `values` in config', () => {
6061
const config = { values: { VALUE: { value: 'value' } } };
61-
expect(buildEnum(generateEnumAST(), config)).toEqual(generateEnumType(config));
62+
expect(buildEnum(generateEnumNode(), config)).toEqual(generateEnumType(config));
6263
});
6364
});

src/build/buildEnum.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
1-
import { GraphQLEnumType } from 'graphql/type';
2-
import { getDescription } from 'graphql/utilities/buildASTSchema';
1+
/* @flow */
32

4-
export const buildEnumValueConfigMap = (enumAST, configMap = {}) =>
5-
enumAST.values.reduce((map, enumValueAST) => {
6-
const name = enumValueAST.name.value;
7-
const config = configMap[name] || {};
8-
// set enum value to it's name by default
9-
const value = 'value' in config ? config.value : name;
10-
const description = getDescription(enumValueAST);
11-
const enumValueConfig = { value };
12-
if ('deprecationReason' in config) enumValueConfig.deprecationReason = config.deprecationReason;
13-
if (description) enumValueConfig.description = description;
14-
return { ...map, [name]: enumValueConfig };
15-
}, {});
3+
import type { EnumTypeDefinitionNode } from 'graphql/language';
4+
import { GraphQLEnumType } from 'graphql/type';
5+
import getDeprecationReason from './getDeprecationReason';
6+
import getDescription from './getDescription';
167

17-
export default function buildEnum(enumAST, { values } = {}) {
18-
const enumTypeConfig = {
19-
name: enumAST.name.value,
20-
values: buildEnumValueConfigMap(enumAST, values),
8+
type BuildEnumValueConfigMapConfigMap = {
9+
[valueName: string]: {
10+
value?: any;
2111
};
22-
const description = getDescription(enumAST);
23-
if (description) enumTypeConfig.description = description;
24-
return new GraphQLEnumType(enumTypeConfig);
12+
};
13+
14+
export function buildEnumValueConfigMap(
15+
node: EnumTypeDefinitionNode,
16+
configMap?: BuildEnumValueConfigMapConfigMap = {},
17+
) {
18+
return node.values.reduce((map, valueNode) => ({
19+
...map,
20+
[valueNode.name.value]: {
21+
...configMap[valueNode.name.value],
22+
deprecationReason: getDeprecationReason(valueNode.directives),
23+
description: getDescription(valueNode),
24+
},
25+
}), {});
26+
}
27+
28+
type BuildEnumConfig = {
29+
values?: BuildEnumValueConfigMapConfigMap;
30+
};
31+
32+
export default function buildEnum(node: EnumTypeDefinitionNode, config?: BuildEnumConfig = {}) {
33+
const { values, ...rest } = config;
34+
return new GraphQLEnumType({
35+
...rest,
36+
name: node.name.value,
37+
values: buildEnumValueConfigMap(node, values),
38+
description: getDescription(node),
39+
});
2540
}

0 commit comments

Comments
 (0)