@@ -14,6 +14,7 @@ import instanceOf from '../jsutils/instanceOf';
14
14
import inspect from '../jsutils/inspect' ;
15
15
import invariant from '../jsutils/invariant' ;
16
16
import keyMap from '../jsutils/keyMap' ;
17
+ import keyValMap from '../jsutils/keyValMap' ;
17
18
import mapValue from '../jsutils/mapValue' ;
18
19
import type { ObjMap } from '../jsutils/ObjMap' ;
19
20
import { Kind } from '../language/kinds' ;
@@ -510,6 +511,10 @@ function resolveThunk<+T>(thunk: Thunk<T>): T {
510
511
return typeof thunk = = = 'function' ? thunk ( ) : thunk ;
511
512
}
512
513
514
+ function undefineIfEmpty < T > ( arr : ?$ReadOnlyArray < T > ) : ?$ReadOnlyArray < T > {
515
+ return arr && arr . length > 0 ? arr : undefined ;
516
+ }
517
+
513
518
/**
514
519
* Scalar Type Definition
515
520
*
@@ -550,7 +555,7 @@ export class GraphQLScalarType {
550
555
this . parseValue = config . parseValue || ( value => value ) ;
551
556
this . parseLiteral = config . parseLiteral || valueFromASTUntyped ;
552
557
this . astNode = config . astNode ;
553
- this . extensionASTNodes = config . extensionASTNodes ;
558
+ this . extensionASTNodes = undefineIfEmpty ( config . extensionASTNodes ) ;
554
559
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
555
560
invariant (
556
561
typeof config . serialize === 'function' ,
@@ -568,6 +573,23 @@ export class GraphQLScalarType {
568
573
}
569
574
}
570
575
576
+ toConfig ( ) : { |
577
+ ...GraphQLScalarTypeConfig < * , * > ,
578
+ parseValue : GraphQLScalarValueParser < * > ,
579
+ parseLiteral : GraphQLScalarLiteralParser < * > ,
580
+ extensionASTNodes : $ReadOnlyArray < ScalarTypeExtensionNode > ,
581
+ | } {
582
+ return {
583
+ name : this . name ,
584
+ description : this . description ,
585
+ serialize : this . serialize ,
586
+ parseValue : this . parseValue ,
587
+ parseLiteral : this . parseLiteral ,
588
+ astNode : this . astNode ,
589
+ extensionASTNodes : this . extensionASTNodes || [ ] ,
590
+ } ;
591
+ }
592
+
571
593
toString ( ) : string {
572
594
return this . name ;
573
595
}
@@ -648,7 +670,7 @@ export class GraphQLObjectType {
648
670
this. name = config . name ;
649
671
this . description = config . description ;
650
672
this . astNode = config . astNode ;
651
- this . extensionASTNodes = config . extensionASTNodes ;
673
+ this . extensionASTNodes = undefineIfEmpty ( config . extensionASTNodes ) ;
652
674
this . isTypeOf = config . isTypeOf ;
653
675
this . _fields = defineFieldMap . bind ( undefined , config ) ;
654
676
this . _interfaces = defineInterfaces . bind ( undefined , config ) ;
@@ -674,6 +696,23 @@ export class GraphQLObjectType {
674
696
return this . _interfaces ;
675
697
}
676
698
699
+ toConfig ( ) : { |
700
+ ...GraphQLObjectTypeConfig < * , * > ,
701
+ interfaces : Array < GraphQLInterfaceType > ,
702
+ fields : GraphQLFieldConfigMap < * , * > ,
703
+ extensionASTNodes : $ReadOnlyArray < ObjectTypeExtensionNode > ,
704
+ | } {
705
+ return {
706
+ name : this . name ,
707
+ description : this . description ,
708
+ isTypeOf : this . isTypeOf ,
709
+ interfaces : this . getInterfaces ( ) ,
710
+ fields : fieldsToFieldsConfig ( this . getFields ( ) ) ,
711
+ astNode : this . astNode ,
712
+ extensionASTNodes : this . extensionASTNodes || [ ] ,
713
+ } ;
714
+ }
715
+
677
716
toString ( ) : string {
678
717
return this . name ;
679
718
}
@@ -751,6 +790,33 @@ function isPlainObj(obj) {
751
790
return obj && typeof obj === 'object ' && ! Array . isArray ( obj ) ;
752
791
}
753
792
793
+ function fieldsToFieldsConfig ( fields ) {
794
+ return mapValue ( fields , field => ( {
795
+ type : field . type ,
796
+ args : argsToArgsConfig ( field . args ) ,
797
+ resolve : field . resolve ,
798
+ subscribe : field . subscribe ,
799
+ deprecationReason : field . deprecationReason ,
800
+ description : field . description ,
801
+ astNode : field . astNode ,
802
+ } ) ) ;
803
+ }
804
+
805
+ export function argsToArgsConfig (
806
+ args : Array < GraphQLArgument > ,
807
+ ) : GraphQLFieldConfigArgumentMap {
808
+ return keyValMap (
809
+ args ,
810
+ arg => arg . name ,
811
+ arg => ( {
812
+ type : arg . type ,
813
+ defaultValue : arg . defaultValue ,
814
+ description : arg . description ,
815
+ astNode : arg . astNode ,
816
+ } ) ,
817
+ ) ;
818
+ }
819
+
754
820
export type GraphQLObjectTypeConfig < TSource , TContext > = { |
755
821
name : string ,
756
822
interfaces ?: Thunk < ?Array < GraphQLInterfaceType >> ,
@@ -892,7 +958,7 @@ export class GraphQLInterfaceType {
892
958
this . name = config . name ;
893
959
this . description = config . description ;
894
960
this . astNode = config . astNode ;
895
- this . extensionASTNodes = config . extensionASTNodes ;
961
+ this . extensionASTNodes = undefineIfEmpty ( config . extensionASTNodes ) ;
896
962
this . resolveType = config . resolveType ;
897
963
this . _fields = defineFieldMap . bind ( undefined , config ) ;
898
964
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
@@ -910,6 +976,21 @@ export class GraphQLInterfaceType {
910
976
return this . _fields ;
911
977
}
912
978
979
+ toConfig ( ) : { |
980
+ ...GraphQLInterfaceTypeConfig < * , * > ,
981
+ fields : GraphQLFieldConfigMap < * , * > ,
982
+ extensionASTNodes : $ReadOnlyArray < InterfaceTypeExtensionNode > ,
983
+ | } {
984
+ return {
985
+ name : this . name ,
986
+ description : this . description ,
987
+ resolveType : this . resolveType ,
988
+ fields : fieldsToFieldsConfig ( this . getFields ( ) ) ,
989
+ astNode : this . astNode ,
990
+ extensionASTNodes : this . extensionASTNodes || [ ] ,
991
+ } ;
992
+ }
993
+
913
994
toString ( ) : string {
914
995
return this . name ;
915
996
}
@@ -969,7 +1050,7 @@ export class GraphQLUnionType {
969
1050
this. name = config . name ;
970
1051
this . description = config . description ;
971
1052
this . astNode = config . astNode ;
972
- this . extensionASTNodes = config . extensionASTNodes ;
1053
+ this . extensionASTNodes = undefineIfEmpty ( config . extensionASTNodes ) ;
973
1054
this . resolveType = config . resolveType ;
974
1055
this . _types = defineTypes . bind ( undefined , config ) ;
975
1056
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
@@ -987,6 +1068,21 @@ export class GraphQLUnionType {
987
1068
return this . _types ;
988
1069
}
989
1070
1071
+ toConfig ( ) : { |
1072
+ ...GraphQLUnionTypeConfig < * , * > ,
1073
+ types : Array < GraphQLObjectType > ,
1074
+ extensionASTNodes : $ReadOnlyArray < UnionTypeExtensionNode > ,
1075
+ | } {
1076
+ return {
1077
+ name : this . name ,
1078
+ description : this . description ,
1079
+ resolveType : this . resolveType ,
1080
+ types : this . getTypes ( ) ,
1081
+ astNode : this . astNode ,
1082
+ extensionASTNodes : this . extensionASTNodes || [ ] ,
1083
+ } ;
1084
+ }
1085
+
990
1086
toString ( ) : string {
991
1087
return this . name ;
992
1088
}
@@ -1057,7 +1153,7 @@ export class GraphQLEnumType /* <T> */ {
1057
1153
this. name = config . name ;
1058
1154
this . description = config . description ;
1059
1155
this . astNode = config . astNode ;
1060
- this . extensionASTNodes = config . extensionASTNodes ;
1156
+ this . extensionASTNodes = undefineIfEmpty ( config . extensionASTNodes ) ;
1061
1157
this . _values = defineEnumValues ( this , config . values ) ;
1062
1158
this . _valueLookup = new Map (
1063
1159
this . _values . map ( enumValue => [ enumValue . value , enumValue ] ) ,
@@ -1101,6 +1197,30 @@ export class GraphQLEnumType /* <T> */ {
1101
1197
}
1102
1198
}
1103
1199
1200
+ toConfig ( ) : { |
1201
+ ...GraphQLEnumTypeConfig ,
1202
+ extensionASTNodes : $ReadOnlyArray < EnumTypeExtensionNode > ,
1203
+ | } {
1204
+ const values = keyValMap (
1205
+ this . getValues ( ) ,
1206
+ value => value . name ,
1207
+ value => ( {
1208
+ description : value . description ,
1209
+ value : value . value ,
1210
+ deprecationReason : value . deprecationReason ,
1211
+ astNode : value . astNode ,
1212
+ } ) ,
1213
+ ) ;
1214
+
1215
+ return {
1216
+ name : this . name ,
1217
+ description : this . description ,
1218
+ values,
1219
+ astNode : this . astNode ,
1220
+ extensionASTNodes : this . extensionASTNodes || [ ] ,
1221
+ } ;
1222
+ }
1223
+
1104
1224
toString ( ) : string {
1105
1225
return this . name ;
1106
1226
}
@@ -1198,7 +1318,7 @@ export class GraphQLInputObjectType {
1198
1318
this . name = config . name ;
1199
1319
this . description = config . description ;
1200
1320
this . astNode = config . astNode ;
1201
- this . extensionASTNodes = config . extensionASTNodes ;
1321
+ this . extensionASTNodes = undefineIfEmpty ( config . extensionASTNodes ) ;
1202
1322
this . _fields = defineInputFieldMap . bind ( undefined , config ) ;
1203
1323
invariant ( typeof config . name === 'string' , 'Must provide name.' ) ;
1204
1324
}
@@ -1210,6 +1330,27 @@ export class GraphQLInputObjectType {
1210
1330
return this . _fields ;
1211
1331
}
1212
1332
1333
+ toConfig ( ) : { |
1334
+ ...GraphQLInputObjectTypeConfig ,
1335
+ fields : GraphQLInputFieldConfigMap ,
1336
+ extensionASTNodes : $ReadOnlyArray < InputObjectTypeExtensionNode > ,
1337
+ | } {
1338
+ const fields = mapValue ( this . getFields ( ) , field => ( {
1339
+ description : field . description ,
1340
+ type : field . type ,
1341
+ defaultValue : field . defaultValue ,
1342
+ astNode : field . astNode ,
1343
+ } ) ) ;
1344
+
1345
+ return {
1346
+ name : this . name ,
1347
+ description : this . description ,
1348
+ fields,
1349
+ astNode : this . astNode ,
1350
+ extensionASTNodes : this . extensionASTNodes || [ ] ,
1351
+ } ;
1352
+ }
1353
+
1213
1354
toString ( ) : string {
1214
1355
return this . name ;
1215
1356
}
0 commit comments