Skip to content

Commit 1f93cf4

Browse files
authored
Merge pull request #674 from graphql/enum-value
TypeInfo.getEnumValue() and EnumType.getValue(name)
2 parents 769be7c + 728f4d6 commit 1f93cf4

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/type/__tests__/enumType-test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ describe('Type System: Enum Values', () => {
354354
});
355355
});
356356

357-
it('may present a values API for complex enums ', () => {
357+
it('presents a getValues() API for complex enums', () => {
358358
const values = ComplexEnum.getValues();
359359
expect(values.length).to.equal(2);
360360
expect(values[0].name).to.equal('ONE');
@@ -363,6 +363,15 @@ describe('Type System: Enum Values', () => {
363363
expect(values[1].value).to.equal(Complex2);
364364
});
365365

366+
it('presents a getValue() API for complex enums', () => {
367+
const oneValue = ComplexEnum.getValue('ONE');
368+
expect(oneValue.name).to.equal('ONE');
369+
expect(oneValue.value).to.equal(Complex1);
370+
371+
const badUsage = ComplexEnum.getValue(Complex1);
372+
expect(badUsage).to.equal(undefined);
373+
});
374+
366375
it('may be internally represented with complex values', async () => {
367376
expect(
368377
await graphql(schema, `{

src/type/definition.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,10 @@ export class GraphQLEnumType/* <T> */ {
852852
return this._values;
853853
}
854854

855+
getValue(name: string): ?GraphQLEnumValue {
856+
return this._getNameLookup()[name];
857+
}
858+
855859
serialize(value: any/* T */): ?string {
856860
const enumValue = this._getValueLookup().get(value);
857861
return enumValue ? enumValue.name : null;

src/utilities/TypeInfo.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
GraphQLInterfaceType,
1818
GraphQLUnionType,
1919
GraphQLInputObjectType,
20+
GraphQLEnumType,
2021
GraphQLList,
2122
} from '../type/definition';
2223
import type {
@@ -25,7 +26,8 @@ import type {
2526
GraphQLOutputType,
2627
GraphQLCompositeType,
2728
GraphQLField,
28-
GraphQLArgument
29+
GraphQLArgument,
30+
GraphQLEnumValue,
2931
} from '../type/definition';
3032
import type { GraphQLDirective } from '../type/directives';
3133
import {
@@ -52,6 +54,7 @@ export class TypeInfo {
5254
_fieldDefStack: Array<?GraphQLField<*, *>>;
5355
_directive: ?GraphQLDirective;
5456
_argument: ?GraphQLArgument;
57+
_enumValue: ?GraphQLEnumValue;
5558
_getFieldDef: typeof getFieldDef;
5659

5760
constructor(
@@ -68,6 +71,7 @@ export class TypeInfo {
6871
this._fieldDefStack = [];
6972
this._directive = null;
7073
this._argument = null;
74+
this._enumValue = null;
7175
this._getFieldDef = getFieldDefFn || getFieldDef;
7276
}
7377

@@ -103,6 +107,10 @@ export class TypeInfo {
103107
return this._argument;
104108
}
105109

110+
getEnumValue(): ?GraphQLEnumValue {
111+
return this._enumValue;
112+
}
113+
106114
// Flow does not yet handle this case.
107115
enter(node: any/* ASTNode */) {
108116
const schema = this._schema;
@@ -182,6 +190,14 @@ export class TypeInfo {
182190
}
183191
this._inputTypeStack.push(fieldType);
184192
break;
193+
case Kind.ENUM:
194+
const enumType = getNamedType(this.getInputType());
195+
let enumValue;
196+
if (enumType instanceof GraphQLEnumType) {
197+
enumValue = enumType.getValue(node.value);
198+
}
199+
this._enumValue = enumValue;
200+
break;
185201
}
186202
}
187203

@@ -213,6 +229,9 @@ export class TypeInfo {
213229
case Kind.OBJECT_FIELD:
214230
this._inputTypeStack.pop();
215231
break;
232+
case Kind.ENUM:
233+
this._enumValue = null;
234+
break;
216235
}
217236
}
218237
}

0 commit comments

Comments
 (0)