Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 9 additions & 9 deletions src/type/__tests__/definition-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,15 +794,15 @@ describe('Type System: Enums', () => {
});

it('rejects an Enum type with incorrectly named values', () => {
expect(
() =>
new GraphQLEnumType({
name: 'SomeEnum',
values: {
'bad-name': {},
},
}),
).to.throw('Names must only contain [_a-zA-Z0-9] but "bad-name" does not.');
const enumType = new GraphQLEnumType({
name: 'SomeEnum',
values: {
'bad-name': {},
},
});
expect(() => enumType.getValues()).to.throw(
'Names must only contain [_a-zA-Z0-9] but "bad-name" does not.',
);
});
});

Expand Down
27 changes: 15 additions & 12 deletions src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,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 @@ -1576,13 +1576,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 @@ -1593,10 +1587,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 @@ -1722,6 +1713,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