Skip to content

Commit 1b0f68a

Browse files
committed
buildSchema lookup for enum value in resolvers
1 parent 9157529 commit 1b0f68a

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/utilities/buildASTSchema.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ import {
7474
} from '../type/schema';
7575

7676
export type TypeFieldResolverMap = ObjMap<
77-
ObjMap<GraphQLFieldResolver<mixed, mixed, mixed>>,
77+
| ObjMap<GraphQLFieldResolver<mixed, mixed, mixed>> /* type and interface */
78+
| ObjMap<any> /* enum */,
7879
>;
7980

8081
export type BuildSchemaOptions = {
@@ -259,22 +260,14 @@ export class ASTDefinitionBuilder {
259260
field: FieldDefinitionNode,
260261
typeName?: string,
261262
): GraphQLFieldConfig<mixed, mixed> {
262-
const resolve =
263-
(typeName &&
264-
this._options &&
265-
this._options.resolvers &&
266-
this._options.resolvers[typeName] &&
267-
this._options.resolvers[typeName][field.name.value]) ||
268-
undefined;
269-
270263
return {
271264
// Note: While this could make assertions to get the correctly typed
272265
// value, that would throw immediately while type system validation
273266
// with validateSchema() will produce more actionable results.
274267
type: (this.getWrappedType(field.type): any),
275268
description: getDescription(field, this._options),
276269
args: keyByNameNode(field.arguments || [], arg => this.buildArg(arg)),
277-
resolve,
270+
resolve: this._lookupResolver(typeName, field.name.value),
278271
deprecationReason: getDeprecationReason(field),
279272
astNode: field,
280273
};
@@ -306,8 +299,12 @@ export class ASTDefinitionBuilder {
306299
};
307300
}
308301

309-
buildEnumValue(value: EnumValueDefinitionNode): GraphQLEnumValueConfig {
302+
buildEnumValue(
303+
value: EnumValueDefinitionNode,
304+
typeName?: string,
305+
): GraphQLEnumValueConfig {
310306
return {
307+
value: this._lookupResolver(typeName, value.name.value),
311308
description: getDescription(value, this._options),
312309
deprecationReason: getDeprecationReason(value),
313310
astNode: value,
@@ -389,11 +386,14 @@ export class ASTDefinitionBuilder {
389386

390387
_makeEnumDef(astNode: EnumTypeDefinitionNode) {
391388
const valueNodes = astNode.values || [];
389+
const name = astNode.name.value;
392390

393391
return new GraphQLEnumType({
394-
name: astNode.name.value,
392+
name,
395393
description: getDescription(astNode, this._options),
396-
values: keyByNameNode(valueNodes, value => this.buildEnumValue(value)),
394+
values: keyByNameNode(valueNodes, value =>
395+
this.buildEnumValue(value, name),
396+
),
397397
astNode,
398398
});
399399
}
@@ -437,6 +437,17 @@ export class ASTDefinitionBuilder {
437437
astNode: def,
438438
});
439439
}
440+
441+
_lookupResolver(typeName: ?string, key: string) {
442+
return (
443+
(typeName &&
444+
this._options &&
445+
this._options.resolvers &&
446+
this._options.resolvers[typeName] &&
447+
this._options.resolvers[typeName][key]) ||
448+
undefined
449+
);
450+
}
440451
}
441452

442453
function keyByNameNode<T: { +name: NameNode, ... }, V>(

src/utilities/extendSchema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export function extendSchema(
314314
...keyValMap(
315315
valueNodes,
316316
value => value.name.value,
317-
value => astBuilder.buildEnumValue(value),
317+
value => astBuilder.buildEnumValue(value, config.name),
318318
),
319319
},
320320
extensionASTNodes: config.extensionASTNodes.concat(extensions),

0 commit comments

Comments
 (0)