From 41c044c55b5da603328cbe2a2dae11a8ddb216ff Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Fri, 6 Jun 2025 09:46:28 +0300 Subject: [PATCH] perf: introduce defineEnumValues This makes definition of enum values lazy similar to how field/input field definition is lazy. It brings a small performance improvement when using enums in large schemas, as the enum values are only defined when they are actually used --- src/type/definition.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/type/definition.ts b/src/type/definition.ts index ea96be5153..1b0340baa4 100644 --- a/src/type/definition.ts +++ b/src/type/definition.ts @@ -1522,7 +1522,7 @@ export class GraphQLEnumType /* */ implements GraphQLSchemaElement { private _values: | ReadonlyArray */> - | (() => GraphQLEnumValueConfigMap); + | (() => ReadonlyArray) /* */; private _valueLookup: ReadonlyMap | null; private _nameLookup: ObjMap | null; @@ -1534,13 +1534,7 @@ export class GraphQLEnumType /* */ implements GraphQLSchemaElement { this.astNode = config.astNode; this.extensionASTNodes = config.extensionASTNodes ?? []; - this._values = - typeof config.values === 'function' - ? config.values - : Object.entries(config.values).map( - ([valueName, valueConfig]) => - new GraphQLEnumValue(this, valueName, valueConfig), - ); + this._values = defineEnumValues.bind(undefined, this, config.values); this._valueLookup = null; this._nameLookup = null; } @@ -1551,10 +1545,7 @@ export class GraphQLEnumType /* */ implements GraphQLSchemaElement { getValues(): ReadonlyArray */> { if (typeof this._values === 'function') { - this._values = Object.entries(this._values()).map( - ([valueName, valueConfig]) => - new GraphQLEnumValue(this, valueName, valueConfig), - ); + this._values = this._values(); } return this._values; } @@ -1684,6 +1675,18 @@ export class GraphQLEnumType /* */ implements GraphQLSchemaElement { } } +function defineEnumValues( + parentEnum: GraphQLEnumType, + values: ThunkObjMap */>, +): ReadonlyArray { + const valueMap = resolveObjMapThunk(values); + + return Object.entries(valueMap).map( + ([valueName, valueConfig]) => + new GraphQLEnumValue(parentEnum, valueName, valueConfig), + ); +} + function didYouMeanEnumValue( enumType: GraphQLEnumType, unknownValueStr: string,