Skip to content

Commit 97b4b9b

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

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/utilities/buildASTSchema.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -259,22 +259,14 @@ export class ASTDefinitionBuilder {
259259
field: FieldDefinitionNode,
260260
typeName?: string,
261261
): 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-
270262
return {
271263
// Note: While this could make assertions to get the correctly typed
272264
// value, that would throw immediately while type system validation
273265
// with validateSchema() will produce more actionable results.
274266
type: (this.getWrappedType(field.type): any),
275267
description: getDescription(field, this._options),
276268
args: keyByNameNode(field.arguments || [], arg => this.buildArg(arg)),
277-
resolve,
269+
resolve: this._lookupResolver(typeName, field.name.value),
278270
deprecationReason: getDeprecationReason(field),
279271
astNode: field,
280272
};
@@ -306,8 +298,12 @@ export class ASTDefinitionBuilder {
306298
};
307299
}
308300

309-
buildEnumValue(value: EnumValueDefinitionNode): GraphQLEnumValueConfig {
301+
buildEnumValue(
302+
value: EnumValueDefinitionNode,
303+
typeName?: string,
304+
): GraphQLEnumValueConfig {
310305
return {
306+
value: this._lookupResolver(typeName, value.name.value),
311307
description: getDescription(value, this._options),
312308
deprecationReason: getDeprecationReason(value),
313309
astNode: value,
@@ -389,11 +385,14 @@ export class ASTDefinitionBuilder {
389385

390386
_makeEnumDef(astNode: EnumTypeDefinitionNode) {
391387
const valueNodes = astNode.values || [];
388+
const name = astNode.name.value;
392389

393390
return new GraphQLEnumType({
394-
name: astNode.name.value,
391+
name,
395392
description: getDescription(astNode, this._options),
396-
values: keyByNameNode(valueNodes, value => this.buildEnumValue(value)),
393+
values: keyByNameNode(valueNodes, value =>
394+
this.buildEnumValue(value, name),
395+
),
397396
astNode,
398397
});
399398
}
@@ -437,6 +436,17 @@ export class ASTDefinitionBuilder {
437436
astNode: def,
438437
});
439438
}
439+
440+
_lookupResolver(typeName: ?string, key: string) {
441+
return (
442+
(typeName &&
443+
this._options &&
444+
this._options.resolvers &&
445+
this._options.resolvers[typeName] &&
446+
this._options.resolvers[typeName][key]) ||
447+
undefined
448+
);
449+
}
440450
}
441451

442452
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)