@@ -1372,9 +1372,12 @@ export class GraphQLEnumType /* <T> */ {
1372
1372
astNode : Maybe < EnumTypeDefinitionNode > ;
1373
1373
extensionASTNodes : ReadonlyArray < EnumTypeExtensionNode > ;
1374
1374
1375
- private _values : ReadonlyArray < GraphQLEnumValue /* <T> */ > ;
1376
- private _valueLookup : ReadonlyMap < any /* T */ , GraphQLEnumValue > ;
1377
- private _nameLookup : ObjMap < GraphQLEnumValue > ;
1375
+ private _values :
1376
+ | ReadonlyArray < GraphQLEnumValue /* <T> */ >
1377
+ | ( ( ) => GraphQLEnumValueConfigMap ) ;
1378
+
1379
+ private _valueLookup : ReadonlyMap < any /* T */ , GraphQLEnumValue > | null ;
1380
+ private _nameLookup : ObjMap < GraphQLEnumValue > | null ;
1378
1381
1379
1382
constructor ( config : Readonly < GraphQLEnumTypeConfig /* <T> */ > ) {
1380
1383
this . name = assertName ( config . name ) ;
@@ -1383,26 +1386,38 @@ export class GraphQLEnumType /* <T> */ {
1383
1386
this . astNode = config . astNode ;
1384
1387
this . extensionASTNodes = config . extensionASTNodes ?? [ ] ;
1385
1388
1386
- this . _values = defineEnumValues ( this . name , config . values ) ;
1387
- this . _valueLookup = new Map (
1388
- this . _values . map ( ( enumValue ) => [ enumValue . value , enumValue ] ) ,
1389
- ) ;
1390
- this . _nameLookup = keyMap ( this . _values , ( value ) => value . name ) ;
1389
+ this . _values =
1390
+ typeof config . values === 'function'
1391
+ ? config . values
1392
+ : defineEnumValues ( this . name , config . values ) ;
1393
+ this . _valueLookup = null ;
1394
+ this . _nameLookup = null ;
1391
1395
}
1392
1396
1393
1397
get [ Symbol . toStringTag ] ( ) {
1394
1398
return 'GraphQLEnumType' ;
1395
1399
}
1396
1400
1397
1401
getValues ( ) : ReadonlyArray < GraphQLEnumValue /* <T> */ > {
1402
+ if ( typeof this . _values === 'function' ) {
1403
+ this . _values = defineEnumValues ( this . name , this . _values ( ) ) ;
1404
+ }
1398
1405
return this . _values ;
1399
1406
}
1400
1407
1401
1408
getValue ( name : string ) : Maybe < GraphQLEnumValue > {
1409
+ if ( this . _nameLookup === null ) {
1410
+ this . _nameLookup = keyMap ( this . getValues ( ) , ( value ) => value . name ) ;
1411
+ }
1402
1412
return this . _nameLookup [ name ] ;
1403
1413
}
1404
1414
1405
1415
serialize ( outputValue : unknown /* T */ ) : Maybe < string > {
1416
+ if ( this . _valueLookup === null ) {
1417
+ this . _valueLookup = new Map (
1418
+ this . getValues ( ) . map ( ( enumValue ) => [ enumValue . value , enumValue ] ) ,
1419
+ ) ;
1420
+ }
1406
1421
const enumValue = this . _valueLookup . get ( outputValue ) ;
1407
1422
if ( enumValue === undefined ) {
1408
1423
throw new GraphQLError (
@@ -1527,13 +1542,14 @@ function defineEnumValues(
1527
1542
export interface GraphQLEnumTypeConfig {
1528
1543
name : string ;
1529
1544
description ?: Maybe < string > ;
1530
- values : GraphQLEnumValueConfigMap /* <T> */ ;
1545
+ values : ThunkObjMap < GraphQLEnumValueConfig /* <T> */ > ;
1531
1546
extensions ?: Maybe < Readonly < GraphQLEnumTypeExtensions > > ;
1532
1547
astNode ?: Maybe < EnumTypeDefinitionNode > ;
1533
1548
extensionASTNodes ?: Maybe < ReadonlyArray < EnumTypeExtensionNode > > ;
1534
1549
}
1535
1550
1536
1551
interface GraphQLEnumTypeNormalizedConfig extends GraphQLEnumTypeConfig {
1552
+ values : ObjMap < GraphQLEnumValueConfig /* <T> */ > ;
1537
1553
extensions : Readonly < GraphQLEnumTypeExtensions > ;
1538
1554
extensionASTNodes : ReadonlyArray < EnumTypeExtensionNode > ;
1539
1555
}
0 commit comments