@@ -12,7 +12,6 @@ import invariant from '../jsutils/invariant';
12
12
import isInvalid from '../jsutils/isInvalid' ;
13
13
import type { ObjMap } from '../jsutils/ObjMap' ;
14
14
import * as Kind from '../language/kinds' ;
15
- import { assertValidName } from '../utilities/assertValidName' ;
16
15
import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped' ;
17
16
import type {
18
17
ScalarTypeDefinitionNode ,
@@ -454,10 +453,11 @@ export class GraphQLScalarType {
454
453
_scalarConfig : GraphQLScalarTypeConfig < * , * > ;
455
454
456
455
constructor ( config : GraphQLScalarTypeConfig < * , * > ) : void {
457
- assertValidName ( config . name ) ;
458
456
this. name = config . name ;
459
457
this . description = config . description ;
460
458
this . astNode = config . astNode ;
459
+ this . _scalarConfig = config ;
460
+ invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
461
461
invariant (
462
462
typeof config . serialize === 'function' ,
463
463
`${ this . name } must provide "serialize" function. If this custom Scalar ` +
@@ -472,7 +472,6 @@ export class GraphQLScalarType {
472
472
'functions.' ,
473
473
) ;
474
474
}
475
- this._scalarConfig = config;
476
475
}
477
476
478
477
// Serializes an internal value to include in a response.
@@ -563,27 +562,27 @@ export class GraphQLObjectType {
563
562
name : string ;
564
563
description : ?string ;
565
564
astNode : ?ObjectTypeDefinitionNode ;
566
- extensionASTNodes : ?Array < ObjectTypeExtensionNode > ;
565
+ extensionASTNodes : ?$ReadOnlyArray < ObjectTypeExtensionNode > ;
567
566
isTypeOf : ?GraphQLIsTypeOfFn < * , * > ;
568
567
569
568
_typeConfig : GraphQLObjectTypeConfig < * , * > ;
570
569
_fields : GraphQLFieldMap < * , * > ;
571
570
_interfaces : Array < GraphQLInterfaceType > ;
572
571
573
572
constructor ( config : GraphQLObjectTypeConfig < * , * > ) : void {
574
- assertValidName ( config . name , config . isIntrospection ) ;
575
573
this. name = config . name ;
576
574
this . description = config . description ;
577
575
this . astNode = config . astNode ;
578
576
this . extensionASTNodes = config . extensionASTNodes ;
577
+ this . isTypeOf = config . isTypeOf ;
578
+ this . _typeConfig = config ;
579
+ invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
579
580
if ( config . isTypeOf ) {
580
581
invariant (
581
582
typeof config . isTypeOf === 'function' ,
582
583
`${ this . name } must provide "isTypeOf" as a function.` ,
583
584
) ;
584
585
}
585
- this . isTypeOf = config . isTypeOf ;
586
- this . _typeConfig = config ;
587
586
}
588
587
589
588
getFields ( ) : GraphQLFieldMap < * , * > {
@@ -616,10 +615,7 @@ function defineInterfaces(
616
615
type : GraphQLObjectType ,
617
616
interfacesThunk : Thunk < ?Array < GraphQLInterfaceType >> ,
618
617
) : Array < GraphQLInterfaceType > {
619
- const interfaces = resolveThunk ( interfacesThunk ) ;
620
- if ( ! interfaces ) {
621
- return [ ] ;
622
- }
618
+ const interfaces = resolveThunk ( interfacesThunk ) || [ ] ;
623
619
invariant (
624
620
Array . isArray ( interfaces ) ,
625
621
`${ type . name } interfaces must be an Array or a function which returns ` +
@@ -632,23 +628,15 @@ function defineFieldMap<TSource, TContext>(
632
628
type : GraphQLNamedType ,
633
629
fieldsThunk : Thunk < GraphQLFieldConfigMap < TSource , TContext >> ,
634
630
) : GraphQLFieldMap < TSource , TContext > {
635
- const fieldMap = resolveThunk ( fieldsThunk ) ;
631
+ const fieldMap = resolveThunk ( fieldsThunk ) || { } ;
636
632
invariant (
637
633
isPlainObj ( fieldMap ) ,
638
634
`${ type . name } fields must be an object with field names as keys or a ` +
639
635
'function which returns such an object.' ,
640
636
) ;
641
637
642
- const fieldNames = Object . keys ( fieldMap ) ;
643
- invariant (
644
- fieldNames . length > 0 ,
645
- `${ type . name } fields must be an object with field names as keys or a ` +
646
- 'function which returns such an object.' ,
647
- ) ;
648
-
649
638
const resultFieldMap = Object . create ( null ) ;
650
- fieldNames . forEach ( fieldName => {
651
- assertValidName ( fieldName ) ;
639
+ Object . keys ( fieldMap ) . forEach ( fieldName => {
652
640
const fieldConfig = fieldMap [ fieldName ] ;
653
641
invariant (
654
642
isPlainObj ( fieldConfig ) ,
@@ -664,11 +652,6 @@ function defineFieldMap<TSource, TContext>(
664
652
isDeprecated : Boolean ( fieldConfig . deprecationReason ) ,
665
653
name : fieldName ,
666
654
} ;
667
- invariant (
668
- isOutputType ( field . type ) ,
669
- `${ type . name } .${ fieldName } field type must be Output Type but ` +
670
- `got: ${ String ( field . type ) } .` ,
671
- ) ;
672
655
invariant (
673
656
isValidResolver ( field . resolve ) ,
674
657
`${ type . name } .${ fieldName } field resolver must be a function if ` +
@@ -684,13 +667,7 @@ function defineFieldMap<TSource, TContext>(
684
667
'names as keys.' ,
685
668
) ;
686
669
field . args = Object . keys ( argsConfig ) . map ( argName => {
687
- assertValidName ( argName ) ;
688
670
const arg = argsConfig [ argName ] ;
689
- invariant (
690
- isInputType ( arg . type ) ,
691
- `${ type . name } .${ fieldName } (${ argName } :) argument type must be ` +
692
- `Input Type but got: ${ String ( arg . type ) } .` ,
693
- ) ;
694
671
return {
695
672
name : argName ,
696
673
description : arg . description === undefined ? null : arg . description ,
@@ -720,9 +697,8 @@ export type GraphQLObjectTypeConfig<TSource, TContext> = {
720
697
fields : Thunk < GraphQLFieldConfigMap < TSource , TContext> > ,
721
698
isTypeOf ?: ?GraphQLIsTypeOfFn < TSource , TContext> ,
722
699
description ?: ?string ,
723
- isIntrospection ?: boolean ,
724
700
astNode ?: ?ObjectTypeDefinitionNode ,
725
- extensionASTNodes ?: ?Array < ObjectTypeExtensionNode > ,
701
+ extensionASTNodes ?: ?$ReadOnlyArray < ObjectTypeExtensionNode > ,
726
702
} ;
727
703
728
704
export type GraphQLTypeResolver < TSource , TContext > = (
@@ -831,26 +807,26 @@ export class GraphQLInterfaceType {
831
807
name : string ;
832
808
description : ?string ;
833
809
astNode : ?InterfaceTypeDefinitionNode ;
834
- extensionASTNodes : ?Array < InterfaceTypeExtensionNode > ;
810
+ extensionASTNodes : ?$ReadOnlyArray < InterfaceTypeExtensionNode > ;
835
811
resolveType : ?GraphQLTypeResolver < * , * > ;
836
812
837
813
_typeConfig : GraphQLInterfaceTypeConfig < * , * > ;
838
814
_fields : GraphQLFieldMap < * , * > ;
839
815
840
816
constructor ( config : GraphQLInterfaceTypeConfig < * , * > ) : void {
841
- assertValidName ( config . name ) ;
842
817
this . name = config . name ;
843
818
this . description = config . description ;
844
819
this . astNode = config . astNode ;
845
820
this . extensionASTNodes = config . extensionASTNodes ;
821
+ this . resolveType = config . resolveType ;
822
+ this . _typeConfig = config ;
823
+ invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
846
824
if ( config . resolveType ) {
847
825
invariant (
848
826
typeof config . resolveType === 'function' ,
849
827
`${ this . name } must provide "resolveType" as a function.` ,
850
828
) ;
851
829
}
852
- this . resolveType = config . resolveType ;
853
- this . _typeConfig = config ;
854
830
}
855
831
856
832
getFields ( ) : GraphQLFieldMap < * , * > {
@@ -883,7 +859,7 @@ export type GraphQLInterfaceTypeConfig<TSource, TContext> = {
883
859
resolveType ?: ?GraphQLTypeResolver < TSource , TContext> ,
884
860
description ?: ?string ,
885
861
astNode ?: ?InterfaceTypeDefinitionNode ,
886
- extensionASTNodes ?: ?Array < InterfaceTypeExtensionNode > ,
862
+ extensionASTNodes ?: ?$ReadOnlyArray < InterfaceTypeExtensionNode > ,
887
863
} ;
888
864
889
865
/**
@@ -919,18 +895,18 @@ export class GraphQLUnionType {
919
895
_types : Array < GraphQLObjectType > ;
920
896
921
897
constructor ( config : GraphQLUnionTypeConfig < * , * > ) : void {
922
- assertValidName ( config . name ) ;
923
898
this . name = config . name ;
924
899
this . description = config . description ;
925
900
this . astNode = config . astNode ;
901
+ this . resolveType = config . resolveType ;
902
+ this . _typeConfig = config ;
903
+ invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
926
904
if ( config . resolveType ) {
927
905
invariant (
928
906
typeof config . resolveType === 'function' ,
929
907
`${ this . name } must provide "resolveType" as a function.` ,
930
908
) ;
931
909
}
932
- this . resolveType = config . resolveType ;
933
- this . _typeConfig = config ;
934
910
}
935
911
936
912
getTypes ( ) : Array < GraphQLObjectType > {
@@ -955,27 +931,12 @@ function defineTypes(
955
931
unionType : GraphQLUnionType ,
956
932
typesThunk : Thunk < Array < GraphQLObjectType >> ,
957
933
) : Array < GraphQLObjectType > {
958
- const types = resolveThunk ( typesThunk ) ;
959
-
934
+ const types = resolveThunk ( typesThunk ) || [ ] ;
960
935
invariant (
961
- Array . isArray ( types ) && types . length > 0 ,
936
+ Array . isArray ( types ) ,
962
937
'Must provide Array of types or a function which returns ' +
963
938
`such an array for Union ${ unionType . name } .` ,
964
939
) ;
965
- const includedTypeNames = Object . create ( null ) ;
966
- types . forEach ( objType => {
967
- invariant (
968
- isObjectType ( objType ) ,
969
- `${ unionType . name } may only contain Object types, it cannot contain: ` +
970
- `${ String ( objType ) } .` ,
971
- ) ;
972
- invariant (
973
- ! includedTypeNames [ objType . name ] ,
974
- `${ unionType . name } can include ${ objType . name } type only once.` ,
975
- ) ;
976
- includedTypeNames [ objType . name ] = true ;
977
- } ) ;
978
-
979
940
return types ;
980
941
}
981
942
@@ -1025,15 +986,17 @@ export class GraphQLEnumType /* <T> */ {
1025
986
1026
987
constructor ( config : GraphQLEnumTypeConfig /* <T> */ ) : void {
1027
988
this . name = config . name ;
1028
- assertValidName ( config . name , config . isIntrospection ) ;
1029
989
this . description = config . description ;
1030
990
this . astNode = config . astNode ;
1031
- this . _values = defineEnumValues ( this , config . values ) ;
1032
991
this . _enumConfig = config ;
992
+ invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
1033
993
}
1034
994
1035
995
getValues ( ) : Array < GraphQLEnumValue /* <T> */ > {
1036
- return this . _values ;
996
+ return (
997
+ this . _values ||
998
+ ( this . _values = defineEnumValues ( this , this . _enumConfig . values ) )
999
+ ) ;
1037
1000
}
1038
1001
1039
1002
getValue ( name : string ) : ?GraphQLEnumValue {
@@ -1108,18 +1071,7 @@ function defineEnumValues(
1108
1071
isPlainObj ( valueMap ) ,
1109
1072
`${ type . name } values must be an object with value names as keys.` ,
1110
1073
) ;
1111
- const valueNames = Object . keys ( valueMap ) ;
1112
- invariant (
1113
- valueNames . length > 0 ,
1114
- `${ type . name } values must be an object with value names as keys.` ,
1115
- ) ;
1116
- return valueNames . map ( valueName => {
1117
- assertValidName ( valueName ) ;
1118
- invariant (
1119
- [ 'true' , 'false' , 'null' ] . indexOf ( valueName ) === - 1 ,
1120
- `Name "${ valueName } " can not be used as an Enum value.` ,
1121
- ) ;
1122
-
1074
+ return Object . keys ( valueMap ) . map ( valueName => {
1123
1075
const value = valueMap [ valueName ] ;
1124
1076
invariant (
1125
1077
isPlainObj ( value ) ,
@@ -1147,7 +1099,6 @@ export type GraphQLEnumTypeConfig /* <T> */ = {
1147
1099
values : GraphQLEnumValueConfigMap /* <T> */ ,
1148
1100
description ?: ?string ,
1149
1101
astNode ?: ?EnumTypeDefinitionNode ,
1150
- isIntrospection ?: boolean ,
1151
1102
} ;
1152
1103
1153
1104
export type GraphQLEnumValueConfigMap /* <T> */ = ObjMap <
@@ -1199,44 +1150,32 @@ export class GraphQLInputObjectType {
1199
1150
_fields : GraphQLInputFieldMap ;
1200
1151
1201
1152
constructor ( config : GraphQLInputObjectTypeConfig ) : void {
1202
- assertValidName ( config . name ) ;
1203
1153
this . name = config . name ;
1204
1154
this . description = config . description ;
1205
1155
this . astNode = config . astNode ;
1206
1156
this . _typeConfig = config ;
1157
+ invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
1207
1158
}
1208
1159
1209
1160
getFields ( ) : GraphQLInputFieldMap {
1210
1161
return this . _fields || ( this . _fields = this . _defineFieldMap ( ) ) ;
1211
1162
}
1212
1163
1213
1164
_defineFieldMap ( ) : GraphQLInputFieldMap {
1214
- const fieldMap : any = resolveThunk ( this . _typeConfig . fields ) ;
1165
+ const fieldMap : any = resolveThunk ( this . _typeConfig . fields ) || { } ;
1215
1166
invariant (
1216
1167
isPlainObj ( fieldMap ) ,
1217
1168
`${ this . name } fields must be an object with field names as keys or a ` +
1218
1169
'function which returns such an object.' ,
1219
1170
) ;
1220
- const fieldNames = Object . keys ( fieldMap ) ;
1221
- invariant (
1222
- fieldNames . length > 0 ,
1223
- `${ this . name } fields must be an object with field names as keys or a ` +
1224
- 'function which returns such an object.' ,
1225
- ) ;
1226
1171
const resultFieldMap = Object . create ( null ) ;
1227
- fieldNames . forEach ( fieldName => {
1228
- assertValidName ( fieldName ) ;
1172
+ Object . keys ( fieldMap ) . forEach ( fieldName => {
1229
1173
const field = {
1230
1174
...fieldMap [ fieldName ] ,
1231
1175
name : fieldName ,
1232
1176
} ;
1233
1177
invariant (
1234
- isInputType ( field . type ) ,
1235
- `${ this . name } .${ fieldName } field type must be Input Type but ` +
1236
- `got: ${ String ( field . type ) } .` ,
1237
- ) ;
1238
- invariant (
1239
- field . resolve == null ,
1178
+ ! field . hasOwnProperty ( 'resolve' ) ,
1240
1179
`${ this . name } .${ fieldName } field type has a resolve property, but ` +
1241
1180
'Input Types cannot define resolvers.' ,
1242
1181
) ;
0 commit comments