@@ -639,17 +639,17 @@ export class GraphQLObjectType {
639
639
extensionASTNodes : ?$ReadOnlyArray < ObjectTypeExtensionNode > ;
640
640
isTypeOf : ?GraphQLIsTypeOfFn < * , * > ;
641
641
642
- _typeConfig : GraphQLObjectTypeConfig < * , * > ;
643
- _fields : GraphQLFieldMap < * , * > ;
644
- _interfaces : Array < GraphQLInterfaceType > ;
642
+ _fields : Thunk < GraphQLFieldMap < * , * > > ;
643
+ _interfaces : Thunk < Array < GraphQLInterfaceType > > ;
645
644
646
645
constructor ( config : GraphQLObjectTypeConfig < * , * > ) : void {
647
646
this . name = config . name ;
648
647
this . description = config . description ;
649
648
this . astNode = config . astNode ;
650
649
this . extensionASTNodes = config . extensionASTNodes ;
651
650
this . isTypeOf = config . isTypeOf ;
652
- this . _typeConfig = config ;
651
+ this . _fields = defineFieldMap . bind ( undefined , config ) ;
652
+ this . _interfaces = defineInterfaces . bind ( undefined , config ) ;
653
653
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
654
654
if ( config . isTypeOf ) {
655
655
invariant (
@@ -660,17 +660,17 @@ export class GraphQLObjectType {
660
660
}
661
661
662
662
getFields ( ) : GraphQLFieldMap < * , * > {
663
- return (
664
- this . _fields ||
665
- ( this . _fields = defineFieldMap ( this , this . _typeConfig . fields ) )
666
- ) ;
663
+ if ( typeof this . _fields === 'function' ) {
664
+ this . _fields = this . _fields ( ) ;
665
+ }
666
+ return this . _fields ;
667
667
}
668
668
669
669
getInterfaces ( ) : Array < GraphQLInterfaceType > {
670
- return (
671
- this . _interfaces ||
672
- ( this . _interfaces = defineInterfaces ( this , this . _typeConfig . interfaces ) )
673
- ) ;
670
+ if ( typeof this . _interfaces === 'function' ) {
671
+ this . _interfaces = this . _interfaces ( ) ;
672
+ }
673
+ return this . _interfaces ;
674
674
}
675
675
676
676
toString ( ) : string {
@@ -683,26 +683,26 @@ defineToStringTag(GraphQLObjectType);
683
683
defineToJSON ( GraphQLObjectType ) ;
684
684
685
685
function defineInterfaces (
686
- type : GraphQLObjectType ,
687
- interfacesThunk : Thunk < ?Array < GraphQLInterfaceType >> ,
686
+ config : GraphQLObjectTypeConfig < * , * > ,
688
687
) : Array < GraphQLInterfaceType > {
689
- const interfaces = resolveThunk ( interfacesThunk ) || [ ] ;
688
+ const interfaces = resolveThunk ( config . interfaces ) || [ ] ;
690
689
invariant (
691
690
Array . isArray ( interfaces ) ,
692
- `${ type . name } interfaces must be an Array or a function which returns ` +
691
+ `${ config . name } interfaces must be an Array or a function which returns ` +
693
692
'an Array.' ,
694
693
) ;
695
694
return interfaces ;
696
695
}
697
696
698
697
function defineFieldMap < TSource , TContext > (
699
- type : GraphQLNamedType ,
700
- fieldsThunk : Thunk < GraphQLFieldConfigMap < TSource , TContext> > ,
698
+ config :
699
+ | GraphQLObjectTypeConfig < TSource , TContext >
700
+ | GraphQLInterfaceTypeConfig < TSource , TContext > ,
701
701
) : GraphQLFieldMap < TSource , TContext > {
702
- const fieldMap = resolveThunk ( fieldsThunk ) || { } ;
702
+ const fieldMap = resolveThunk ( config . fields ) || { } ;
703
703
invariant (
704
704
isPlainObj ( fieldMap ) ,
705
- `${ type . name } fields must be an object with field names as keys or a ` +
705
+ `${ config . name } fields must be an object with field names as keys or a ` +
706
706
'function which returns such an object.' ,
707
707
) ;
708
708
@@ -711,12 +711,12 @@ function defineFieldMap<TSource, TContext>(
711
711
const fieldConfig = fieldMap [ fieldName ] ;
712
712
invariant (
713
713
isPlainObj ( fieldConfig ) ,
714
- `${ type . name } .${ fieldName } field config must be an object` ,
714
+ `${ config . name } .${ fieldName } field config must be an object` ,
715
715
) ;
716
716
invariant (
717
717
! fieldConfig . hasOwnProperty ( 'isDeprecated' ) ,
718
- `${ type . name } .${ fieldName } should provide "deprecationReason" instead ` +
719
- 'of "isDeprecated".' ,
718
+ `${ config . name } .${ fieldName } should provide "deprecationReason" ` +
719
+ 'instead of "isDeprecated".' ,
720
720
) ;
721
721
const field = {
722
722
...fieldConfig ,
@@ -725,7 +725,7 @@ function defineFieldMap<TSource, TContext>(
725
725
} ;
726
726
invariant (
727
727
isValidResolver ( field . resolve ) ,
728
- `${ type . name } .${ fieldName } field resolver must be a function if ` +
728
+ `${ config . name } .${ fieldName } field resolver must be a function if ` +
729
729
`provided, but got: ${ inspect ( field . resolve ) } .` ,
730
730
) ;
731
731
const argsConfig = fieldConfig . args ;
@@ -734,7 +734,7 @@ function defineFieldMap<TSource, TContext>(
734
734
} else {
735
735
invariant (
736
736
isPlainObj ( argsConfig ) ,
737
- `${ type . name } .${ fieldName } args must be an object with argument ` +
737
+ `${ config . name } .${ fieldName } args must be an object with argument ` +
738
738
'names as keys.' ,
739
739
) ;
740
740
field . args = Object . keys ( argsConfig ) . map ( argName => {
@@ -893,16 +893,15 @@ export class GraphQLInterfaceType {
893
893
extensionASTNodes : ?$ReadOnlyArray < InterfaceTypeExtensionNode > ;
894
894
resolveType : ?GraphQLTypeResolver < * , * > ;
895
895
896
- _typeConfig : GraphQLInterfaceTypeConfig < * , * > ;
897
- _fields : GraphQLFieldMap < * , * > ;
896
+ _fields : Thunk < GraphQLFieldMap < * , * > > ;
898
897
899
898
constructor ( config : GraphQLInterfaceTypeConfig < * , * > ) : void {
900
899
this . name = config . name ;
901
900
this . description = config . description ;
902
901
this . astNode = config . astNode ;
903
902
this . extensionASTNodes = config . extensionASTNodes ;
904
903
this . resolveType = config . resolveType ;
905
- this . _typeConfig = config ;
904
+ this . _fields = defineFieldMap . bind ( undefined , config ) ;
906
905
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
907
906
if ( config . resolveType ) {
908
907
invariant (
@@ -913,10 +912,10 @@ export class GraphQLInterfaceType {
913
912
}
914
913
915
914
getFields ( ) : GraphQLFieldMap < * , * > {
916
- return (
917
- this . _fields ||
918
- ( this . _fields = defineFieldMap ( this , this . _typeConfig . fields ) )
919
- ) ;
915
+ if ( typeof this . _fields === 'function' ) {
916
+ this . _fields = this . _fields ( ) ;
917
+ }
918
+ return this . _fields ;
920
919
}
921
920
922
921
toString ( ) : string {
@@ -972,16 +971,15 @@ export class GraphQLUnionType {
972
971
extensionASTNodes : ?$ReadOnlyArray < UnionTypeExtensionNode > ;
973
972
resolveType : ?GraphQLTypeResolver < * , * > ;
974
973
975
- _typeConfig : GraphQLUnionTypeConfig < * , * > ;
976
- _types : Array < GraphQLObjectType > ;
974
+ _types : Thunk < Array < GraphQLObjectType > > ;
977
975
978
976
constructor ( config : GraphQLUnionTypeConfig < * , * > ) : void {
979
977
this . name = config . name ;
980
978
this . description = config . description ;
981
979
this . astNode = config . astNode ;
982
980
this . extensionASTNodes = config . extensionASTNodes ;
983
981
this . resolveType = config . resolveType ;
984
- this . _typeConfig = config ;
982
+ this . _types = defineTypes . bind ( undefined , config ) ;
985
983
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
986
984
if ( config . resolveType ) {
987
985
invariant (
@@ -992,9 +990,10 @@ export class GraphQLUnionType {
992
990
}
993
991
994
992
getTypes ( ) : Array < GraphQLObjectType > {
995
- return (
996
- this . _types || ( this . _types = defineTypes ( this , this . _typeConfig . types ) )
997
- ) ;
993
+ if ( typeof this . _types === 'function' ) {
994
+ this . _types = this . _types ( ) ;
995
+ }
996
+ return this . _types ;
998
997
}
999
998
1000
999
toString ( ) : string {
@@ -1007,14 +1006,13 @@ defineToStringTag(GraphQLUnionType);
1007
1006
defineToJSON ( GraphQLUnionType ) ;
1008
1007
1009
1008
function defineTypes (
1010
- unionType : GraphQLUnionType ,
1011
- typesThunk : Thunk < Array < GraphQLObjectType >> ,
1009
+ config : GraphQLUnionTypeConfig < * , * > ,
1012
1010
) : Array < GraphQLObjectType > {
1013
- const types = resolveThunk ( typesThunk ) || [ ] ;
1011
+ const types = resolveThunk ( config . types ) || [ ] ;
1014
1012
invariant (
1015
1013
Array . isArray ( types ) ,
1016
1014
'Must provide Array of types or a function which returns ' +
1017
- `such an array for Union ${ unionType . name } .` ,
1015
+ `such an array for Union ${ config . name } .` ,
1018
1016
) ;
1019
1017
return types ;
1020
1018
}
@@ -1206,43 +1204,22 @@ export class GraphQLInputObjectType {
1206
1204
astNode : ?InputObjectTypeDefinitionNode ;
1207
1205
extensionASTNodes : ?$ReadOnlyArray < InputObjectTypeExtensionNode > ;
1208
1206
1209
- _typeConfig : GraphQLInputObjectTypeConfig ;
1210
- _fields : GraphQLInputFieldMap ;
1207
+ _fields : Thunk < GraphQLInputFieldMap > ;
1211
1208
1212
1209
constructor ( config : GraphQLInputObjectTypeConfig ) : void {
1213
1210
this . name = config . name ;
1214
1211
this . description = config . description ;
1215
1212
this . astNode = config . astNode ;
1216
1213
this . extensionASTNodes = config . extensionASTNodes ;
1217
- this . _typeConfig = config ;
1214
+ this . _fields = defineInputFieldMap . bind ( undefined , config ) ;
1218
1215
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
1219
1216
}
1220
1217
1221
1218
getFields ( ) : GraphQLInputFieldMap {
1222
- return this . _fields || ( this . _fields = this . _defineFieldMap ( ) ) ;
1223
- }
1224
-
1225
- _defineFieldMap ( ) : GraphQLInputFieldMap {
1226
- const fieldMap : any = resolveThunk ( this . _typeConfig . fields ) || { } ;
1227
- invariant (
1228
- isPlainObj ( fieldMap ) ,
1229
- `${ this . name } fields must be an object with field names as keys or a ` +
1230
- 'function which returns such an object.' ,
1231
- ) ;
1232
- const resultFieldMap = Object . create ( null ) ;
1233
- Object . keys ( fieldMap ) . forEach ( fieldName => {
1234
- const field = {
1235
- ...fieldMap [ fieldName ] ,
1236
- name : fieldName ,
1237
- } ;
1238
- invariant (
1239
- ! field . hasOwnProperty ( 'resolve' ) ,
1240
- `${ this . name } .${ fieldName } field type has a resolve property, but ` +
1241
- 'Input Types cannot define resolvers.' ,
1242
- ) ;
1243
- resultFieldMap [ fieldName ] = field ;
1244
- } ) ;
1245
- return resultFieldMap ;
1219
+ if ( typeof this . _fields === 'function' ) {
1220
+ this . _fields = this . _fields ( ) ;
1221
+ }
1222
+ return this . _fields ;
1246
1223
}
1247
1224
1248
1225
toString ( ) : string {
@@ -1254,6 +1231,31 @@ export class GraphQLInputObjectType {
1254
1231
defineToStringTag ( GraphQLInputObjectType ) ;
1255
1232
defineToJSON ( GraphQLInputObjectType ) ;
1256
1233
1234
+ function defineInputFieldMap (
1235
+ config : GraphQLInputObjectTypeConfig ,
1236
+ ) : GraphQLInputFieldMap {
1237
+ const fieldMap : any = resolveThunk ( config . fields ) || { } ;
1238
+ invariant (
1239
+ isPlainObj ( fieldMap ) ,
1240
+ `${ config . name } fields must be an object with field names as keys or a ` +
1241
+ 'function which returns such an object.' ,
1242
+ ) ;
1243
+ const resultFieldMap = Object . create ( null ) ;
1244
+ Object . keys ( fieldMap ) . forEach ( fieldName => {
1245
+ const field = {
1246
+ ...fieldMap [ fieldName ] ,
1247
+ name : fieldName ,
1248
+ } ;
1249
+ invariant (
1250
+ ! field . hasOwnProperty ( 'resolve' ) ,
1251
+ `${ config . name } .${ fieldName } field type has a resolve property, but ` +
1252
+ 'Input Types cannot define resolvers.' ,
1253
+ ) ;
1254
+ resultFieldMap [ fieldName ] = field ;
1255
+ } ) ;
1256
+ return resultFieldMap ;
1257
+ }
1258
+
1257
1259
export type GraphQLInputObjectTypeConfig = { |
1258
1260
name : string ,
1259
1261
fields : Thunk < GraphQLInputFieldConfigMap > ,
0 commit comments