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