Skip to content

Commit 41c044c

Browse files
committed
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
1 parent ef042cf commit 41c044c

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/type/definition.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
15221522

15231523
private _values:
15241524
| ReadonlyArray<GraphQLEnumValue /* <T> */>
1525-
| (() => GraphQLEnumValueConfigMap);
1525+
| (() => ReadonlyArray<GraphQLEnumValue>) /* <T> */;
15261526

15271527
private _valueLookup: ReadonlyMap<any /* T */, GraphQLEnumValue> | null;
15281528
private _nameLookup: ObjMap<GraphQLEnumValue> | null;
@@ -1534,13 +1534,7 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
15341534
this.astNode = config.astNode;
15351535
this.extensionASTNodes = config.extensionASTNodes ?? [];
15361536

1537-
this._values =
1538-
typeof config.values === 'function'
1539-
? config.values
1540-
: Object.entries(config.values).map(
1541-
([valueName, valueConfig]) =>
1542-
new GraphQLEnumValue(this, valueName, valueConfig),
1543-
);
1537+
this._values = defineEnumValues.bind(undefined, this, config.values);
15441538
this._valueLookup = null;
15451539
this._nameLookup = null;
15461540
}
@@ -1551,10 +1545,7 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
15511545

15521546
getValues(): ReadonlyArray<GraphQLEnumValue /* <T> */> {
15531547
if (typeof this._values === 'function') {
1554-
this._values = Object.entries(this._values()).map(
1555-
([valueName, valueConfig]) =>
1556-
new GraphQLEnumValue(this, valueName, valueConfig),
1557-
);
1548+
this._values = this._values();
15581549
}
15591550
return this._values;
15601551
}
@@ -1684,6 +1675,18 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
16841675
}
16851676
}
16861677

1678+
function defineEnumValues(
1679+
parentEnum: GraphQLEnumType,
1680+
values: ThunkObjMap<GraphQLEnumValueConfig /* <T> */>,
1681+
): ReadonlyArray<GraphQLEnumValue> {
1682+
const valueMap = resolveObjMapThunk(values);
1683+
1684+
return Object.entries(valueMap).map(
1685+
([valueName, valueConfig]) =>
1686+
new GraphQLEnumValue(parentEnum, valueName, valueConfig),
1687+
);
1688+
}
1689+
16871690
function didYouMeanEnumValue(
16881691
enumType: GraphQLEnumType,
16891692
unknownValueStr: string,

0 commit comments

Comments
 (0)