Skip to content

buildClientSchema: build enum type value maps lazily #4424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {

private _values:
| ReadonlyArray<GraphQLEnumValue /* <T> */>
| (() => GraphQLEnumValueConfigMap);
| (() => ReadonlyArray<GraphQLEnumValue>) /* <T> */;

private _valueLookup: ReadonlyMap<any /* T */, GraphQLEnumValue> | null;
private _nameLookup: ObjMap<GraphQLEnumValue> | null;
Expand All @@ -1534,13 +1534,7 @@ export class GraphQLEnumType /* <T> */ 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;
}
Expand All @@ -1551,10 +1545,7 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {

getValues(): ReadonlyArray<GraphQLEnumValue /* <T> */> {
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;
}
Expand Down Expand Up @@ -1684,6 +1675,18 @@ export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
}
}

function defineEnumValues(
parentEnum: GraphQLEnumType,
values: ThunkObjMap<GraphQLEnumValueConfig /* <T> */>,
): ReadonlyArray<GraphQLEnumValue> {
const valueMap = resolveObjMapThunk(values);

return Object.entries(valueMap).map(
([valueName, valueConfig]) =>
new GraphQLEnumValue(parentEnum, valueName, valueConfig),
);
}

function didYouMeanEnumValue(
enumType: GraphQLEnumType,
unknownValueStr: string,
Expand Down
Loading