@@ -649,17 +649,17 @@ export class GraphQLObjectType {
649
649
extensionASTNodes : ?$ReadOnlyArray < ObjectTypeExtensionNode > ;
650
650
isTypeOf : ?GraphQLIsTypeOfFn < * , * > ;
651
651
652
- _typeConfig : GraphQLObjectTypeConfig < * , * > ;
653
- _fields : GraphQLFieldMap < * , * > ;
654
- _interfaces : Array < GraphQLInterfaceType > ;
652
+ _fields : Thunk < GraphQLFieldMap < * , * > > ;
653
+ _interfaces : Thunk < Array < GraphQLInterfaceType > > ;
655
654
656
655
constructor ( config : GraphQLObjectTypeConfig < * , * > ) : void {
657
656
this . name = config . name ;
658
657
this . description = config . description ;
659
658
this . astNode = config . astNode ;
660
659
this . extensionASTNodes = config . extensionASTNodes ;
661
660
this . isTypeOf = config . isTypeOf ;
662
- this . _typeConfig = config ;
661
+ this . _fields = defineFieldMap . bind ( undefined , config ) ;
662
+ this . _interfaces = defineInterfaces . bind ( undefined , config ) ;
663
663
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
664
664
if ( config . isTypeOf ) {
665
665
invariant (
@@ -670,17 +670,17 @@ export class GraphQLObjectType {
670
670
}
671
671
672
672
getFields ( ) : GraphQLFieldMap < * , * > {
673
- return (
674
- this . _fields ||
675
- ( this . _fields = defineFieldMap ( this , this . _typeConfig . fields ) )
676
- ) ;
673
+ if ( typeof this . _fields === 'function' ) {
674
+ this . _fields = this . _fields ( ) ;
675
+ }
676
+ return this . _fields ;
677
677
}
678
678
679
679
getInterfaces ( ) : Array < GraphQLInterfaceType > {
680
- return (
681
- this . _interfaces ||
682
- ( this . _interfaces = defineInterfaces ( this , this . _typeConfig . interfaces ) )
683
- ) ;
680
+ if ( typeof this . _interfaces === 'function' ) {
681
+ this . _interfaces = this . _interfaces ( ) ;
682
+ }
683
+ return this . _interfaces ;
684
684
}
685
685
686
686
toString ( ) : string {
@@ -693,26 +693,26 @@ defineToStringTag(GraphQLObjectType);
693
693
defineToJSON ( GraphQLObjectType ) ;
694
694
695
695
function defineInterfaces (
696
- type : GraphQLObjectType ,
697
- interfacesThunk : Thunk < ?Array < GraphQLInterfaceType >> ,
696
+ config : GraphQLObjectTypeConfig < * , * > ,
698
697
) : Array < GraphQLInterfaceType > {
699
- const interfaces = resolveThunk ( interfacesThunk ) || [ ] ;
698
+ const interfaces = resolveThunk ( config . interfaces ) || [ ] ;
700
699
invariant (
701
700
Array . isArray ( interfaces ) ,
702
- `${ type . name } interfaces must be an Array or a function which returns ` +
701
+ `${ config . name } interfaces must be an Array or a function which returns ` +
703
702
'an Array.' ,
704
703
) ;
705
704
return interfaces ;
706
705
}
707
706
708
707
function defineFieldMap < TSource , TContext > (
709
- type : GraphQLNamedType ,
710
- fieldsThunk : Thunk < GraphQLFieldConfigMap < TSource , TContext >> ,
708
+ config :
709
+ | GraphQLObjectTypeConfig < TSource , TContext >
710
+ | GraphQLInterfaceTypeConfig < TSource , TContext > ,
711
711
) : GraphQLFieldMap < TSource , TContext > {
712
- const fieldMap = resolveThunk ( fieldsThunk ) || { } ;
712
+ const fieldMap = resolveThunk ( config . fields ) || { } ;
713
713
invariant (
714
714
isPlainObj ( fieldMap ) ,
715
- `${ type . name } fields must be an object with field names as keys or a ` +
715
+ `${ config . name } fields must be an object with field names as keys or a ` +
716
716
'function which returns such an object.' ,
717
717
) ;
718
718
@@ -721,12 +721,12 @@ function defineFieldMap<TSource, TContext>(
721
721
const fieldConfig = fieldMap [ fieldName ] ;
722
722
invariant (
723
723
isPlainObj ( fieldConfig ) ,
724
- `${ type . name } .${ fieldName } field config must be an object` ,
724
+ `${ config . name } .${ fieldName } field config must be an object` ,
725
725
) ;
726
726
invariant (
727
727
! fieldConfig . hasOwnProperty ( 'isDeprecated' ) ,
728
- `${ type . name } .${ fieldName } should provide "deprecationReason" instead ` +
729
- 'of "isDeprecated".' ,
728
+ `${ config . name } .${ fieldName } should provide "deprecationReason" ` +
729
+ 'instead of "isDeprecated".' ,
730
730
) ;
731
731
const field = {
732
732
...fieldConfig ,
@@ -735,7 +735,7 @@ function defineFieldMap<TSource, TContext>(
735
735
} ;
736
736
invariant (
737
737
isValidResolver ( field . resolve ) ,
738
- `${ type . name } .${ fieldName } field resolver must be a function if ` +
738
+ `${ config . name } .${ fieldName } field resolver must be a function if ` +
739
739
`provided, but got: ${ inspect ( field . resolve ) } .` ,
740
740
) ;
741
741
const argsConfig = fieldConfig . args ;
@@ -744,7 +744,7 @@ function defineFieldMap<TSource, TContext>(
744
744
} else {
745
745
invariant (
746
746
isPlainObj ( argsConfig ) ,
747
- `${ type . name } .${ fieldName } args must be an object with argument ` +
747
+ `${ config . name } .${ fieldName } args must be an object with argument ` +
748
748
'names as keys.' ,
749
749
) ;
750
750
field . args = Object . keys ( argsConfig ) . map ( argName => {
@@ -903,16 +903,15 @@ export class GraphQLInterfaceType {
903
903
extensionASTNodes : ?$ReadOnlyArray < InterfaceTypeExtensionNode > ;
904
904
resolveType : ?GraphQLTypeResolver < * , * > ;
905
905
906
- _typeConfig : GraphQLInterfaceTypeConfig < * , * > ;
907
- _fields : GraphQLFieldMap < * , * > ;
906
+ _fields : Thunk < GraphQLFieldMap < * , * > > ;
908
907
909
908
constructor ( config : GraphQLInterfaceTypeConfig < * , * > ) : void {
910
909
this . name = config . name ;
911
910
this . description = config . description ;
912
911
this . astNode = config . astNode ;
913
912
this . extensionASTNodes = config . extensionASTNodes ;
914
913
this . resolveType = config . resolveType ;
915
- this . _typeConfig = config ;
914
+ this . _fields = defineFieldMap . bind ( undefined , config ) ;
916
915
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
917
916
if ( config . resolveType ) {
918
917
invariant (
@@ -923,10 +922,10 @@ export class GraphQLInterfaceType {
923
922
}
924
923
925
924
getFields ( ) : GraphQLFieldMap < * , * > {
926
- return (
927
- this . _fields ||
928
- ( this . _fields = defineFieldMap ( this , this . _typeConfig . fields ) )
929
- ) ;
925
+ if ( typeof this . _fields === 'function' ) {
926
+ this . _fields = this . _fields ( ) ;
927
+ }
928
+ return this . _fields ;
930
929
}
931
930
932
931
toString ( ) : string {
@@ -982,16 +981,15 @@ export class GraphQLUnionType {
982
981
extensionASTNodes : ?$ReadOnlyArray < UnionTypeExtensionNode > ;
983
982
resolveType : ?GraphQLTypeResolver < * , * > ;
984
983
985
- _typeConfig : GraphQLUnionTypeConfig < * , * > ;
986
- _types : Array < GraphQLObjectType > ;
984
+ _types : Thunk < Array < GraphQLObjectType > > ;
987
985
988
986
constructor ( config : GraphQLUnionTypeConfig < * , * > ) : void {
989
987
this . name = config . name ;
990
988
this . description = config . description ;
991
989
this . astNode = config . astNode ;
992
990
this . extensionASTNodes = config . extensionASTNodes ;
993
991
this . resolveType = config . resolveType ;
994
- this . _typeConfig = config ;
992
+ this . _types = defineTypes . bind ( undefined , config ) ;
995
993
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
996
994
if ( config . resolveType ) {
997
995
invariant (
@@ -1002,9 +1000,10 @@ export class GraphQLUnionType {
1002
1000
}
1003
1001
1004
1002
getTypes ( ) : Array < GraphQLObjectType > {
1005
- return (
1006
- this . _types || ( this . _types = defineTypes ( this , this . _typeConfig . types ) )
1007
- ) ;
1003
+ if ( typeof this . _types === 'function' ) {
1004
+ this . _types = this . _types ( ) ;
1005
+ }
1006
+ return this . _types ;
1008
1007
}
1009
1008
1010
1009
toString ( ) : string {
@@ -1017,14 +1016,13 @@ defineToStringTag(GraphQLUnionType);
1017
1016
defineToJSON ( GraphQLUnionType ) ;
1018
1017
1019
1018
function defineTypes (
1020
- unionType : GraphQLUnionType ,
1021
- typesThunk : Thunk < Array < GraphQLObjectType >> ,
1019
+ config : GraphQLUnionTypeConfig < * , * > ,
1022
1020
) : Array < GraphQLObjectType > {
1023
- const types = resolveThunk ( typesThunk ) || [ ] ;
1021
+ const types = resolveThunk ( config . types ) || [ ] ;
1024
1022
invariant (
1025
1023
Array . isArray ( types ) ,
1026
1024
'Must provide Array of types or a function which returns ' +
1027
- `such an array for Union ${ unionType . name } .` ,
1025
+ `such an array for Union ${ config . name } .` ,
1028
1026
) ;
1029
1027
return types ;
1030
1028
}
@@ -1216,43 +1214,22 @@ export class GraphQLInputObjectType {
1216
1214
astNode : ?InputObjectTypeDefinitionNode ;
1217
1215
extensionASTNodes : ?$ReadOnlyArray < InputObjectTypeExtensionNode > ;
1218
1216
1219
- _typeConfig : GraphQLInputObjectTypeConfig ;
1220
- _fields : GraphQLInputFieldMap ;
1217
+ _fields : Thunk < GraphQLInputFieldMap > ;
1221
1218
1222
1219
constructor ( config : GraphQLInputObjectTypeConfig ) : void {
1223
1220
this . name = config . name ;
1224
1221
this . description = config . description ;
1225
1222
this . astNode = config . astNode ;
1226
1223
this . extensionASTNodes = config . extensionASTNodes ;
1227
- this . _typeConfig = config ;
1224
+ this . _fields = defineInputFieldMap . bind ( undefined , config ) ;
1228
1225
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
1229
1226
}
1230
1227
1231
1228
getFields ( ) : GraphQLInputFieldMap {
1232
- return this . _fields || ( this . _fields = this . _defineFieldMap ( ) ) ;
1233
- }
1234
-
1235
- _defineFieldMap ( ) : GraphQLInputFieldMap {
1236
- const fieldMap : any = resolveThunk ( this . _typeConfig . fields ) || { } ;
1237
- invariant (
1238
- isPlainObj ( fieldMap ) ,
1239
- `${ this . name } fields must be an object with field names as keys or a ` +
1240
- 'function which returns such an object.' ,
1241
- ) ;
1242
- const resultFieldMap = Object . create ( null ) ;
1243
- Object . keys ( fieldMap ) . forEach ( fieldName => {
1244
- const field = {
1245
- ...fieldMap [ fieldName ] ,
1246
- name : fieldName ,
1247
- } ;
1248
- invariant (
1249
- ! field . hasOwnProperty ( 'resolve' ) ,
1250
- `${ this . name } .${ fieldName } field type has a resolve property, but ` +
1251
- 'Input Types cannot define resolvers.' ,
1252
- ) ;
1253
- resultFieldMap [ fieldName ] = field ;
1254
- } ) ;
1255
- return resultFieldMap ;
1229
+ if ( typeof this . _fields === 'function' ) {
1230
+ this . _fields = this . _fields ( ) ;
1231
+ }
1232
+ return this . _fields ;
1256
1233
}
1257
1234
1258
1235
toString ( ) : string {
@@ -1264,6 +1241,31 @@ export class GraphQLInputObjectType {
1264
1241
defineToStringTag ( GraphQLInputObjectType ) ;
1265
1242
defineToJSON ( GraphQLInputObjectType ) ;
1266
1243
1244
+ function defineInputFieldMap (
1245
+ config : GraphQLInputObjectTypeConfig ,
1246
+ ) : GraphQLInputFieldMap {
1247
+ const fieldMap : any = resolveThunk ( config . fields ) || { } ;
1248
+ invariant (
1249
+ isPlainObj ( fieldMap ) ,
1250
+ `${ config . name } fields must be an object with field names as keys or a ` +
1251
+ 'function which returns such an object.' ,
1252
+ ) ;
1253
+ const resultFieldMap = Object . create ( null ) ;
1254
+ Object . keys ( fieldMap ) . forEach ( fieldName => {
1255
+ const field = {
1256
+ ...fieldMap [ fieldName ] ,
1257
+ name : fieldName ,
1258
+ } ;
1259
+ invariant (
1260
+ ! field . hasOwnProperty ( 'resolve' ) ,
1261
+ `${ config . name } .${ fieldName } field type has a resolve property, but ` +
1262
+ 'Input Types cannot define resolvers.' ,
1263
+ ) ;
1264
+ resultFieldMap [ fieldName ] = field ;
1265
+ } ) ;
1266
+ return resultFieldMap ;
1267
+ }
1268
+
1267
1269
export type GraphQLInputObjectTypeConfig = { |
1268
1270
name : string ,
1269
1271
fields : Thunk < GraphQLInputFieldConfigMap > ,
0 commit comments