@@ -1237,6 +1237,17 @@ export interface GraphQLEnumTypeExtensions {
1237
1237
[ attributeName : string ] : unknown ;
1238
1238
}
1239
1239
1240
+ function enumValuesFromConfig ( values : GraphQLEnumValueConfigMap ) {
1241
+ return Object . entries ( values ) . map ( ( [ valueName , valueConfig ] ) => ( {
1242
+ name : assertEnumValueName ( valueName ) ,
1243
+ description : valueConfig . description ,
1244
+ value : valueConfig . value !== undefined ? valueConfig . value : valueName ,
1245
+ deprecationReason : valueConfig . deprecationReason ,
1246
+ extensions : toObjMap ( valueConfig . extensions ) ,
1247
+ astNode : valueConfig . astNode ,
1248
+ } ) ) ;
1249
+ }
1250
+
1240
1251
/**
1241
1252
* Enum Type Definition
1242
1253
*
@@ -1267,9 +1278,12 @@ export class GraphQLEnumType /* <T> */ {
1267
1278
astNode : Maybe < EnumTypeDefinitionNode > ;
1268
1279
extensionASTNodes : ReadonlyArray < EnumTypeExtensionNode > ;
1269
1280
1270
- private _values : ReadonlyArray < GraphQLEnumValue /* <T> */ > ;
1271
- private _valueLookup : ReadonlyMap < any /* T */ , GraphQLEnumValue > ;
1272
- private _nameLookup : ObjMap < GraphQLEnumValue > ;
1281
+ private _values :
1282
+ | ReadonlyArray < GraphQLEnumValue /* <T> */ >
1283
+ | ( ( ) => GraphQLEnumValueConfigMap ) ;
1284
+
1285
+ private _valueLookup : ReadonlyMap < any /* T */ , GraphQLEnumValue > | null ;
1286
+ private _nameLookup : ObjMap < GraphQLEnumValue > | null ;
1273
1287
1274
1288
constructor ( config : Readonly < GraphQLEnumTypeConfig /* <T> */ > ) {
1275
1289
this . name = assertName ( config . name ) ;
@@ -1278,35 +1292,38 @@ export class GraphQLEnumType /* <T> */ {
1278
1292
this . astNode = config . astNode ;
1279
1293
this . extensionASTNodes = config . extensionASTNodes ?? [ ] ;
1280
1294
1281
- this . _values = Object . entries ( config . values ) . map (
1282
- ( [ valueName , valueConfig ] ) => ( {
1283
- name : assertEnumValueName ( valueName ) ,
1284
- description : valueConfig . description ,
1285
- value : valueConfig . value !== undefined ? valueConfig . value : valueName ,
1286
- deprecationReason : valueConfig . deprecationReason ,
1287
- extensions : toObjMap ( valueConfig . extensions ) ,
1288
- astNode : valueConfig . astNode ,
1289
- } ) ,
1290
- ) ;
1291
- this . _valueLookup = new Map (
1292
- this . _values . map ( ( enumValue ) => [ enumValue . value , enumValue ] ) ,
1293
- ) ;
1294
- this . _nameLookup = keyMap ( this . _values , ( value ) => value . name ) ;
1295
+ this . _values =
1296
+ typeof config . values === 'function'
1297
+ ? config . values
1298
+ : enumValuesFromConfig ( config . values ) ;
1299
+ this . _valueLookup = null ;
1300
+ this . _nameLookup = null ;
1295
1301
}
1296
1302
1297
1303
get [ Symbol . toStringTag ] ( ) {
1298
1304
return 'GraphQLEnumType' ;
1299
1305
}
1300
1306
1301
1307
getValues ( ) : ReadonlyArray < GraphQLEnumValue /* <T> */ > {
1308
+ if ( typeof this . _values === 'function' ) {
1309
+ this . _values = enumValuesFromConfig ( this . _values ( ) ) ;
1310
+ }
1302
1311
return this . _values ;
1303
1312
}
1304
1313
1305
1314
getValue ( name : string ) : Maybe < GraphQLEnumValue > {
1315
+ if ( this . _nameLookup === null ) {
1316
+ this . _nameLookup = keyMap ( this . getValues ( ) , ( value ) => value . name ) ;
1317
+ }
1306
1318
return this . _nameLookup [ name ] ;
1307
1319
}
1308
1320
1309
1321
serialize ( outputValue : unknown /* T */ ) : Maybe < string > {
1322
+ if ( this . _valueLookup === null ) {
1323
+ this . _valueLookup = new Map (
1324
+ this . getValues ( ) . map ( ( enumValue ) => [ enumValue . value , enumValue ] ) ,
1325
+ ) ;
1326
+ }
1310
1327
const enumValue = this . _valueLookup . get ( outputValue ) ;
1311
1328
if ( enumValue === undefined ) {
1312
1329
throw new GraphQLError (
@@ -1406,7 +1423,7 @@ function didYouMeanEnumValue(
1406
1423
export interface GraphQLEnumTypeConfig {
1407
1424
name : string ;
1408
1425
description ?: Maybe < string > ;
1409
- values : GraphQLEnumValueConfigMap /* <T> */ ;
1426
+ values : ThunkObjMap < GraphQLEnumValueConfig /* <T> */ > ;
1410
1427
extensions ?: Maybe < Readonly < GraphQLEnumTypeExtensions > > ;
1411
1428
astNode ?: Maybe < EnumTypeDefinitionNode > ;
1412
1429
extensionASTNodes ?: Maybe < ReadonlyArray < EnumTypeExtensionNode > > ;
0 commit comments