10
10
import instanceOf from '../jsutils/instanceOf' ;
11
11
import invariant from '../jsutils/invariant' ;
12
12
import isInvalid from '../jsutils/isInvalid' ;
13
+ import keyMap from '../jsutils/keyMap' ;
13
14
import type { ObjMap } from '../jsutils/ObjMap' ;
14
15
import { Kind } from '../language/kinds' ;
15
16
import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped' ;
@@ -1071,7 +1072,6 @@ export class GraphQLEnumType /* <T> */ {
1071
1072
description : ?string ;
1072
1073
astNode : ?EnumTypeDefinitionNode ;
1073
1074
1074
- _enumConfig : GraphQLEnumTypeConfig /* <T> */ ;
1075
1075
_values : Array < GraphQLEnumValue /* <T> */ > ;
1076
1076
_valueLookup : Map < any /* T */ , GraphQLEnumValue > ;
1077
1077
_nameLookup : ObjMap < GraphQLEnumValue > ;
@@ -1080,31 +1080,33 @@ export class GraphQLEnumType /* <T> */ {
1080
1080
this . name = config . name ;
1081
1081
this . description = config . description ;
1082
1082
this . astNode = config . astNode ;
1083
- this . _enumConfig = config ;
1083
+ this . _values = defineEnumValues ( this , config . values ) ;
1084
+ this . _valueLookup = new Map (
1085
+ this . _values . map ( enumValue => [ enumValue . value , enumValue ] ) ,
1086
+ ) ;
1087
+ this . _nameLookup = keyMap ( this . _values , value => value . name ) ;
1088
+
1084
1089
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
1085
1090
}
1086
1091
1087
1092
getValues ( ) : Array < GraphQLEnumValue /* <T> */ > {
1088
- return (
1089
- this . _values ||
1090
- ( this . _values = defineEnumValues ( this , this . _enumConfig . values ) )
1091
- ) ;
1093
+ return this . _values ;
1092
1094
}
1093
1095
1094
1096
getValue ( name : string ) : ?GraphQLEnumValue {
1095
- return this . _getNameLookup ( ) [ name ] ;
1097
+ return this . _nameLookup [ name ] ;
1096
1098
}
1097
1099
1098
1100
serialize ( value : any /* T */ ) : ?string {
1099
- const enumValue = this . _getValueLookup ( ) . get ( value ) ;
1101
+ const enumValue = this . _valueLookup . get ( value ) ;
1100
1102
if ( enumValue ) {
1101
1103
return enumValue . name ;
1102
1104
}
1103
1105
}
1104
1106
1105
1107
parseValue ( value : mixed ) : ?any /* T */ {
1106
1108
if ( typeof value === 'string' ) {
1107
- const enumValue = this . _getNameLookup ( ) [ value ] ;
1109
+ const enumValue = this . getValue ( value ) ;
1108
1110
if ( enumValue ) {
1109
1111
return enumValue . value ;
1110
1112
}
@@ -1114,35 +1116,13 @@ export class GraphQLEnumType /* <T> */ {
1114
1116
parseLiteral ( valueNode : ValueNode , _variables : ?ObjMap < mixed > ) : ?any /* T */ {
1115
1117
// Note: variables will be resolved to a value before calling this function.
1116
1118
if ( valueNode . kind === Kind . ENUM ) {
1117
- const enumValue = this . _getNameLookup ( ) [ valueNode . value ] ;
1119
+ const enumValue = this . getValue ( valueNode . value ) ;
1118
1120
if ( enumValue ) {
1119
1121
return enumValue . value ;
1120
1122
}
1121
1123
}
1122
1124
}
1123
1125
1124
- _getValueLookup ( ) : Map < any /* T */ , GraphQLEnumValue > {
1125
- if ( ! this . _valueLookup ) {
1126
- const lookup = new Map ( ) ;
1127
- this . getValues ( ) . forEach ( value => {
1128
- lookup . set ( value . value , value ) ;
1129
- } ) ;
1130
- this . _valueLookup = lookup ;
1131
- }
1132
- return this . _valueLookup ;
1133
- }
1134
-
1135
- _getNameLookup ( ) : ObjMap < GraphQLEnumValue > {
1136
- if ( ! this . _nameLookup ) {
1137
- const lookup = Object . create ( null ) ;
1138
- this . getValues ( ) . forEach ( value => {
1139
- lookup [ value . name ] = value ;
1140
- } ) ;
1141
- this . _nameLookup = lookup ;
1142
- }
1143
- return this . _nameLookup ;
1144
- }
1145
-
1146
1126
toString ( ) : string {
1147
1127
return this . name ;
1148
1128
}
0 commit comments