Skip to content

Fix execute deoptimisation caused by GraphQLEnumType #1288

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

Merged
merged 1 commit into from
Mar 29, 2018
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
24 changes: 12 additions & 12 deletions src/type/__tests__/definition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1048,47 +1048,47 @@ describe('Type System: Enum types must be well defined', () => {
});

it('rejects an Enum type with incorrectly typed values', () => {
const enumType = new GraphQLEnumType({
const config = {
name: 'SomeEnum',
values: [{ FOO: 10 }],
});
expect(() => enumType.getValue()).to.throw(
};
expect(() => new GraphQLEnumType(config)).to.throw(
'SomeEnum values must be an object with value names as keys.',
);
});

it('rejects an Enum type with missing value definition', () => {
const enumType = new GraphQLEnumType({
const config = {
name: 'SomeEnum',
values: { FOO: null },
});
expect(() => enumType.getValues()).to.throw(
};
expect(() => new GraphQLEnumType(config)).to.throw(
'SomeEnum.FOO must refer to an object with a "value" key representing ' +
'an internal value but got: null.',
);
});

it('rejects an Enum type with incorrectly typed value definition', () => {
const enumType = new GraphQLEnumType({
const config = {
name: 'SomeEnum',
values: { FOO: 10 },
});
expect(() => enumType.getValues()).to.throw(
};
expect(() => new GraphQLEnumType(config)).to.throw(
'SomeEnum.FOO must refer to an object with a "value" key representing ' +
'an internal value but got: 10.',
);
});

it('does not allow isDeprecated without deprecationReason on enum', () => {
const enumType = new GraphQLEnumType({
const config = {
name: 'SomeEnum',
values: {
FOO: {
isDeprecated: true,
},
},
});
expect(() => enumType.getValues()).to.throw(
};
expect(() => new GraphQLEnumType(config)).to.throw(
'SomeEnum.FOO should provide "deprecationReason" instead ' +
'of "isDeprecated".',
);
Expand Down
44 changes: 12 additions & 32 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import instanceOf from '../jsutils/instanceOf';
import invariant from '../jsutils/invariant';
import isInvalid from '../jsutils/isInvalid';
import keyMap from '../jsutils/keyMap';
import type { ObjMap } from '../jsutils/ObjMap';
import { Kind } from '../language/kinds';
import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped';
Expand Down Expand Up @@ -1071,7 +1072,6 @@ export class GraphQLEnumType /* <T> */ {
description: ?string;
astNode: ?EnumTypeDefinitionNode;

_enumConfig: GraphQLEnumTypeConfig /* <T> */;
_values: Array<GraphQLEnumValue /* <T> */>;
_valueLookup: Map<any /* T */, GraphQLEnumValue>;
_nameLookup: ObjMap<GraphQLEnumValue>;
Expand All @@ -1080,31 +1080,33 @@ export class GraphQLEnumType /* <T> */ {
this.name = config.name;
this.description = config.description;
this.astNode = config.astNode;
this._enumConfig = config;
this._values = defineEnumValues(this, config.values);
this._valueLookup = new Map(
this._values.map(enumValue => [enumValue.value, enumValue]),
);
this._nameLookup = keyMap(this._values, value => value.name);

invariant(typeof config.name === 'string', 'Must provide name.');
}

getValues(): Array<GraphQLEnumValue /* <T> */> {
return (
this._values ||
(this._values = defineEnumValues(this, this._enumConfig.values))
);
return this._values;
}

getValue(name: string): ?GraphQLEnumValue {
return this._getNameLookup()[name];
return this._nameLookup[name];
}

serialize(value: any /* T */): ?string {
const enumValue = this._getValueLookup().get(value);
const enumValue = this._valueLookup.get(value);
if (enumValue) {
return enumValue.name;
}
}

parseValue(value: mixed): ?any /* T */ {
if (typeof value === 'string') {
const enumValue = this._getNameLookup()[value];
const enumValue = this.getValue(value);
if (enumValue) {
return enumValue.value;
}
Expand All @@ -1114,35 +1116,13 @@ export class GraphQLEnumType /* <T> */ {
parseLiteral(valueNode: ValueNode, _variables: ?ObjMap<mixed>): ?any /* T */ {
// Note: variables will be resolved to a value before calling this function.
if (valueNode.kind === Kind.ENUM) {
const enumValue = this._getNameLookup()[valueNode.value];
const enumValue = this.getValue(valueNode.value);
if (enumValue) {
return enumValue.value;
}
}
}

_getValueLookup(): Map<any /* T */, GraphQLEnumValue> {
if (!this._valueLookup) {
const lookup = new Map();
this.getValues().forEach(value => {
lookup.set(value.value, value);
});
this._valueLookup = lookup;
}
return this._valueLookup;
}

_getNameLookup(): ObjMap<GraphQLEnumValue> {
if (!this._nameLookup) {
const lookup = Object.create(null);
this.getValues().forEach(value => {
lookup[value.name] = value;
});
this._nameLookup = lookup;
}
return this._nameLookup;
}

toString(): string {
return this.name;
}
Expand Down