Skip to content

Commit 0dc4c3f

Browse files
committed
Add 'toConfig' method
1 parent bc03768 commit 0dc4c3f

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
@@ -12,6 +12,8 @@ import inspect from '../jsutils/inspect';
1212
import invariant from '../jsutils/invariant';
1313
import isInvalid from '../jsutils/isInvalid';
1414
import keyMap from '../jsutils/keyMap';
15+
import keyValMap from '../jsutils/keyValMap';
16+
import objectValues from '../jsutils/objectValues';
1517
import type { ObjMap } from '../jsutils/ObjMap';
1618
import { Kind } from '../language/kinds';
1719
import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped';
@@ -579,6 +581,17 @@ export class GraphQLScalarType {
579581
: valueFromASTUntyped(valueNode, variables);
580582
}
581583

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

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

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

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

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

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

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

843941
export type GraphQLArgumentConfig = {
844942
type: GraphQLInputType,
@@ -847,9 +945,19 @@ export type GraphQLArgumentConfig = {
847945
astNode?: ?InputValueDefinitionNode,
848946
};
849947

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

854962
export type GraphQLField<
855963
TSource,
@@ -930,6 +1038,17 @@ export class GraphQLInterfaceType {
9301038
);
9311039
}
9321040

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

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

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

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

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

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

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

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

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

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

12881500
export type GraphQLInputFieldConfigMap = ObjMap<GraphQLInputFieldConfig>;
1501+
export type GraphQLInputFieldExactConfigMap = ObjMap<
1502+
GraphQLInputFieldExactConfig,
1503+
>;
12891504

12901505
export type GraphQLInputField = {
12911506
name: string,

0 commit comments

Comments
 (0)