Skip to content

Commit 6d433a0

Browse files
committed
Add 'toConfig' method
1 parent 2eccaad commit 6d433a0

File tree

5 files changed

+343
-132
lines changed

5 files changed

+343
-132
lines changed

src/type/definition.js

Lines changed: 221 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import instanceOf from '../jsutils/instanceOf';
1111
import invariant from '../jsutils/invariant';
1212
import isInvalid from '../jsutils/isInvalid';
1313
import keyMap from '../jsutils/keyMap';
14+
import keyValMap from '../jsutils/keyValMap';
15+
import objectValues from '../jsutils/objectValues';
1416
import type { ObjMap } from '../jsutils/ObjMap';
1517
import { Kind } from '../language/kinds';
1618
import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped';
@@ -578,6 +580,17 @@ export class GraphQLScalarType {
578580
: valueFromASTUntyped(valueNode, variables);
579581
}
580582

583+
toConfig(): GraphQLScalarTypeExactConfig<*, *> {
584+
return {
585+
name: this.name,
586+
description: this.description,
587+
serialize: this._scalarConfig.serialize,
588+
parseValue: this._scalarConfig.parseValue,
589+
parseLiteral: this._scalarConfig.parseLiteral,
590+
astNode: this.astNode,
591+
};
592+
}
593+
581594
toString(): string {
582595
return this.name;
583596
}
@@ -590,18 +603,31 @@ export class GraphQLScalarType {
590603
GraphQLScalarType.prototype.toJSON = GraphQLScalarType.prototype.inspect =
591604
GraphQLScalarType.prototype.toString;
592605

606+
type GraphQLScalarSerializeFn<TExternal> = (value: mixed) => ?TExternal;
607+
type GraphQLScalarParseValueFn<TInternal> = (value: mixed) => ?TInternal;
608+
type GraphQLScalarParseLiteralFn<TInternal> = (
609+
valueNode: ValueNode,
610+
variables: ?ObjMap<mixed>,
611+
) => ?TInternal;
612+
593613
export type GraphQLScalarTypeConfig<TInternal, TExternal> = {
594614
name: string,
595615
description?: ?string,
596616
astNode?: ?ScalarTypeDefinitionNode,
597-
serialize: (value: mixed) => ?TExternal,
598-
parseValue?: (value: mixed) => ?TInternal,
599-
parseLiteral?: (
600-
valueNode: ValueNode,
601-
variables: ?ObjMap<mixed>,
602-
) => ?TInternal,
617+
serialize: GraphQLScalarSerializeFn<TExternal>,
618+
parseValue?: GraphQLScalarParseValueFn<TInternal>,
619+
parseLiteral?: GraphQLScalarParseLiteralFn<TInternal>,
603620
};
604621

622+
export type GraphQLScalarTypeExactConfig<TInternal, TExternal> = {|
623+
name: string,
624+
description: ?string,
625+
astNode: ?ScalarTypeDefinitionNode,
626+
serialize: GraphQLScalarSerializeFn<TExternal>,
627+
parseValue?: GraphQLScalarParseValueFn<TInternal>,
628+
parseLiteral?: GraphQLScalarParseLiteralFn<TInternal>,
629+
|};
630+
605631
/**
606632
* Object Type Definition
607633
*
@@ -680,6 +706,18 @@ export class GraphQLObjectType {
680706
);
681707
}
682708

709+
toConfig(): GraphQLObjectTypeExactConfig<*, *> {
710+
return {
711+
name: this.name,
712+
interfaces: this.getInterfaces(),
713+
fields: fieldsToFieldsConfig(this.getFields()),
714+
isTypeOf: this.isTypeOf,
715+
description: this.description,
716+
astNode: this.astNode,
717+
extensionASTNodes: this.extensionASTNodes || [],
718+
};
719+
}
720+
683721
toString(): string {
684722
return this.name;
685723
}
@@ -772,6 +810,39 @@ function isValidResolver(resolver: mixed): boolean {
772810
return resolver == null || typeof resolver === 'function';
773811
}
774812

813+
function fieldsToFieldsConfig<TSource, TContext>(
814+
fields: GraphQLFieldMap<TSource, TContext>,
815+
): GraphQLFieldExactConfigMap<TSource, TContext> {
816+
return keyValMap(
817+
objectValues(fields),
818+
field => field.name,
819+
field => ({
820+
type: field.type,
821+
args: argsToArgsConfig(field.args),
822+
resolve: field.resolve,
823+
subscribe: field.subscribe,
824+
deprecationReason: field.deprecationReason,
825+
description: field.description,
826+
astNode: field.astNode,
827+
}),
828+
);
829+
}
830+
831+
export function argsToArgsConfig(
832+
args: Array<GraphQLArgument>,
833+
): GraphQLFieldExactConfigArgumentMap {
834+
return keyValMap(
835+
args,
836+
arg => arg.name,
837+
arg => ({
838+
type: arg.type,
839+
defaultValue: arg.defaultValue,
840+
description: arg.description,
841+
astNode: arg.astNode,
842+
}),
843+
);
844+
}
845+
775846
export type GraphQLObjectTypeConfig<TSource, TContext> = {
776847
name: string,
777848
interfaces?: Thunk<?Array<GraphQLInterfaceType>>,
@@ -782,6 +853,16 @@ export type GraphQLObjectTypeConfig<TSource, TContext> = {
782853
extensionASTNodes?: ?$ReadOnlyArray<ObjectTypeExtensionNode>,
783854
};
784855

856+
export type GraphQLObjectTypeExactConfig<TSource, TContext> = {|
857+
name: string,
858+
interfaces: Array<GraphQLInterfaceType>,
859+
fields: GraphQLFieldExactConfigMap<TSource, TContext>,
860+
isTypeOf: ?GraphQLIsTypeOfFn<TSource, TContext>,
861+
description: ?string,
862+
astNode?: ?ObjectTypeDefinitionNode,
863+
extensionASTNodes: $ReadOnlyArray<ObjectTypeExtensionNode>,
864+
|};
865+
785866
export type GraphQLTypeResolver<TSource, TContext> = (
786867
value: TSource,
787868
context: TContext,
@@ -837,7 +918,24 @@ export type GraphQLFieldConfig<
837918
astNode?: ?FieldDefinitionNode,
838919
};
839920

921+
export type GraphQLFieldExactConfig<
922+
TSource,
923+
TContext,
924+
TArgs = { [argument: string]: any },
925+
> = {|
926+
type: GraphQLOutputType,
927+
args: GraphQLFieldExactConfigArgumentMap,
928+
resolve: ?GraphQLFieldResolver<TSource, TContext, TArgs>,
929+
subscribe: ?GraphQLFieldResolver<TSource, TContext, TArgs>,
930+
deprecationReason: ?string,
931+
description: ?string,
932+
astNode: ?FieldDefinitionNode,
933+
|};
934+
840935
export type GraphQLFieldConfigArgumentMap = ObjMap<GraphQLArgumentConfig>;
936+
export type GraphQLFieldExactConfigArgumentMap = ObjMap<
937+
GraphQLArgumentExactConfig,
938+
>;
841939

842940
export type GraphQLArgumentConfig = {
843941
type: GraphQLInputType,
@@ -846,9 +944,19 @@ export type GraphQLArgumentConfig = {
846944
astNode?: ?InputValueDefinitionNode,
847945
};
848946

947+
export type GraphQLArgumentExactConfig = {|
948+
type: GraphQLInputType,
949+
defaultValue: mixed,
950+
description: ?string,
951+
astNode: ?InputValueDefinitionNode,
952+
|};
953+
849954
export type GraphQLFieldConfigMap<TSource, TContext> = ObjMap<
850955
GraphQLFieldConfig<TSource, TContext>,
851956
>;
957+
export type GraphQLFieldExactConfigMap<TSource, TContext> = ObjMap<
958+
GraphQLFieldExactConfig<TSource, TContext>,
959+
>;
852960

853961
export type GraphQLField<
854962
TSource,
@@ -929,6 +1037,17 @@ export class GraphQLInterfaceType {
9291037
);
9301038
}
9311039

1040+
toConfig(): GraphQLInterfaceTypeExactConfig<*, *> {
1041+
return {
1042+
name: this.name,
1043+
fields: fieldsToFieldsConfig(this.getFields()),
1044+
resolveType: this.resolveType,
1045+
description: this.description,
1046+
astNode: this.astNode,
1047+
extensionASTNodes: this.extensionASTNodes || [],
1048+
};
1049+
}
1050+
9321051
toString(): string {
9331052
return this.name;
9341053
}
@@ -955,6 +1074,15 @@ export type GraphQLInterfaceTypeConfig<TSource, TContext> = {
9551074
extensionASTNodes?: ?$ReadOnlyArray<InterfaceTypeExtensionNode>,
9561075
};
9571076

1077+
export type GraphQLInterfaceTypeExactConfig<TSource, TContext> = {|
1078+
name: string,
1079+
fields: GraphQLFieldExactConfigMap<TSource, TContext>,
1080+
resolveType: ?GraphQLTypeResolver<TSource, TContext>,
1081+
description: ?string,
1082+
astNode: ?InterfaceTypeDefinitionNode,
1083+
extensionASTNodes: $ReadOnlyArray<InterfaceTypeExtensionNode>,
1084+
|};
1085+
9581086
/**
9591087
* Union Type Definition
9601088
*
@@ -1008,6 +1136,16 @@ export class GraphQLUnionType {
10081136
);
10091137
}
10101138

1139+
toConfig(): GraphQLUnionTypeExactConfig<*, *> {
1140+
return {
1141+
name: this.name,
1142+
types: this.getTypes(),
1143+
resolveType: this.resolveType,
1144+
description: this.description,
1145+
astNode: this.astNode,
1146+
};
1147+
}
1148+
10111149
toString(): string {
10121150
return this.name;
10131151
}
@@ -1046,6 +1184,14 @@ export type GraphQLUnionTypeConfig<TSource, TContext> = {
10461184
astNode?: ?UnionTypeDefinitionNode,
10471185
};
10481186

1187+
export type GraphQLUnionTypeExactConfig<TSource, TContext> = {
1188+
name: string,
1189+
types: Array<GraphQLObjectType>,
1190+
resolveType: ?GraphQLTypeResolver<TSource, TContext>,
1191+
description: ?string,
1192+
astNode: ?UnionTypeDefinitionNode,
1193+
};
1194+
10491195
/**
10501196
* Enum Type Definition
10511197
*
@@ -1123,6 +1269,24 @@ export class GraphQLEnumType /* <T> */ {
11231269
}
11241270
}
11251271

1272+
toConfig(): GraphQLEnumTypeConfig {
1273+
return {
1274+
name: this.name,
1275+
values: keyValMap(
1276+
this.getValues(),
1277+
value => value.name,
1278+
value => ({
1279+
value: value.value,
1280+
deprecationReason: value.deprecationReason,
1281+
description: value.description,
1282+
astNode: value.astNode,
1283+
}),
1284+
),
1285+
description: this.description,
1286+
astNode: this.astNode,
1287+
};
1288+
}
1289+
11261290
toString(): string {
11271291
return this.name;
11281292
}
@@ -1173,17 +1337,35 @@ export type GraphQLEnumTypeConfig /* <T> */ = {
11731337
astNode?: ?EnumTypeDefinitionNode,
11741338
};
11751339

1340+
export type GraphQLEnumTypeExactConfig /* <T> */ = {|
1341+
name: string,
1342+
values: GraphQLEnumValueExactConfigMap /* <T> */,
1343+
description: ?string,
1344+
astNode: ?EnumTypeDefinitionNode,
1345+
|};
1346+
11761347
export type GraphQLEnumValueConfigMap /* <T> */ = ObjMap<
11771348
GraphQLEnumValueConfig /* <T> */,
11781349
>;
11791350

1351+
export type GraphQLEnumValueExactConfigMap /* <T> */ = ObjMap<
1352+
GraphQLEnumValueExactConfig /* <T> */,
1353+
>;
1354+
11801355
export type GraphQLEnumValueConfig /* <T> */ = {
11811356
value?: any /* T */,
11821357
deprecationReason?: ?string,
11831358
description?: ?string,
11841359
astNode?: ?EnumValueDefinitionNode,
11851360
};
11861361

1362+
export type GraphQLEnumValueExactConfig /* <T> */ = {|
1363+
value?: any /* T */,
1364+
deprecationReason: ?string,
1365+
description: ?string,
1366+
astNode: ?EnumValueDefinitionNode,
1367+
|};
1368+
11871369
export type GraphQLEnumValue /* <T> */ = {
11881370
name: string,
11891371
description: ?string,
@@ -1256,6 +1438,24 @@ export class GraphQLInputObjectType {
12561438
return resultFieldMap;
12571439
}
12581440

1441+
toConfig(): GraphQLInputObjectTypeExactConfig {
1442+
return {
1443+
name: this.name,
1444+
fields: keyValMap(
1445+
objectValues(this.getFields()),
1446+
field => field.name,
1447+
field => ({
1448+
type: field.type,
1449+
defaultValue: field.defaultValue,
1450+
description: field.description,
1451+
astNode: field.astNode,
1452+
}),
1453+
),
1454+
description: this.description,
1455+
astNode: this.astNode,
1456+
};
1457+
}
1458+
12591459
toString(): string {
12601460
return this.name;
12611461
}
@@ -1276,15 +1476,30 @@ export type GraphQLInputObjectTypeConfig = {
12761476
description?: ?string,
12771477
astNode?: ?InputObjectTypeDefinitionNode,
12781478
};
1479+
export type GraphQLInputObjectTypeExactConfig = {|
1480+
name: string,
1481+
fields: GraphQLInputFieldExactConfigMap,
1482+
description: ?string,
1483+
astNode: ?InputObjectTypeDefinitionNode,
1484+
|};
12791485

12801486
export type GraphQLInputFieldConfig = {
12811487
type: GraphQLInputType,
12821488
defaultValue?: mixed,
12831489
description?: ?string,
12841490
astNode?: ?InputValueDefinitionNode,
12851491
};
1492+
export type GraphQLInputFieldExactConfig = {|
1493+
type: GraphQLInputType,
1494+
defaultValue: mixed,
1495+
description: ?string,
1496+
astNode: ?InputValueDefinitionNode,
1497+
|};
12861498

12871499
export type GraphQLInputFieldConfigMap = ObjMap<GraphQLInputFieldConfig>;
1500+
export type GraphQLInputFieldExactConfigMap = ObjMap<
1501+
GraphQLInputFieldExactConfig,
1502+
>;
12881503

12891504
export type GraphQLInputField = {
12901505
name: string,

0 commit comments

Comments
 (0)