Skip to content

Commit c27a2f4

Browse files
committed
Add 'toConfig' method
1 parent 9a43d47 commit c27a2f4

File tree

5 files changed

+403
-264
lines changed

5 files changed

+403
-264
lines changed

src/type/definition.js

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import instanceOf from '../jsutils/instanceOf';
1313
import inspect from '../jsutils/inspect';
1414
import invariant from '../jsutils/invariant';
1515
import keyMap from '../jsutils/keyMap';
16+
import keyValMap from '../jsutils/keyValMap';
17+
import objectValues from '../jsutils/objectValues';
1618
import type { ObjMap } from '../jsutils/ObjMap';
1719
import objectEntries from '../jsutils/objectEntries';
1820
import { Kind } from '../language/kinds';
@@ -567,6 +569,18 @@ export class GraphQLScalarType {
567569
}
568570
}
569571

572+
toConfig(): GraphQLScalarTypeExactConfig<*, *> {
573+
return {
574+
name: this.name,
575+
description: this.description,
576+
serialize: this.serialize,
577+
parseValue: this.parseValue,
578+
parseLiteral: this.parseLiteral,
579+
astNode: this.astNode,
580+
extensionASTNodes: this.extensionASTNodes || [],
581+
};
582+
}
583+
570584
toString(): string {
571585
return this.name;
572586
}
@@ -596,6 +610,19 @@ export type GraphQLScalarTypeConfig<TInternal, TExternal> = {|
596610
extensionASTNodes?: ?$ReadOnlyArray<ScalarTypeExtensionNode>,
597611
|};
598612

613+
export type GraphQLScalarTypeExactConfig<TInternal, TExternal> = {|
614+
name: string,
615+
description: ?string,
616+
// Serializes an internal value to include in a response.
617+
serialize: GraphQLScalarSerializer<TExternal>,
618+
// Parses an externally provided value to use as an input.
619+
parseValue: GraphQLScalarValueParser<TInternal>,
620+
// Parses an externally provided literal value to use as an input.
621+
parseLiteral: GraphQLScalarLiteralParser<TInternal>,
622+
astNode: ?ScalarTypeDefinitionNode,
623+
extensionASTNodes: $ReadOnlyArray<ScalarTypeExtensionNode>,
624+
|};
625+
599626
/**
600627
* Object Type Definition
601628
*
@@ -673,6 +700,18 @@ export class GraphQLObjectType {
673700
return this._interfaces;
674701
}
675702

703+
toConfig(): GraphQLObjectTypeExactConfig<*, *> {
704+
return {
705+
name: this.name,
706+
interfaces: this.getInterfaces(),
707+
fields: fieldsToFieldsConfig(this.getFields()),
708+
isTypeOf: this.isTypeOf,
709+
description: this.description,
710+
astNode: this.astNode,
711+
extensionASTNodes: this.extensionASTNodes || [],
712+
};
713+
}
714+
676715
toString(): string {
677716
return this.name;
678717
}
@@ -751,6 +790,39 @@ function isPlainObj(obj) {
751790
return obj && typeof obj === 'object' && !Array.isArray(obj);
752791
}
753792
793+
function fieldsToFieldsConfig<TSource, TContext>(
794+
fields: GraphQLFieldMap<TSource, TContext>,
795+
): GraphQLFieldExactConfigMap<TSource, TContext> {
796+
return keyValMap(
797+
objectValues(fields),
798+
field => field.name,
799+
field => ({
800+
type: field.type,
801+
args: argsToArgsConfig(field.args),
802+
resolve: field.resolve,
803+
subscribe: field.subscribe,
804+
deprecationReason: field.deprecationReason,
805+
description: field.description,
806+
astNode: field.astNode,
807+
}),
808+
);
809+
}
810+
811+
export function argsToArgsConfig(
812+
args: Array<GraphQLArgument>,
813+
): GraphQLFieldExactConfigArgumentMap {
814+
return keyValMap(
815+
args,
816+
arg => arg.name,
817+
arg => ({
818+
type: arg.type,
819+
defaultValue: arg.defaultValue,
820+
description: arg.description,
821+
astNode: arg.astNode,
822+
}),
823+
);
824+
}
825+
754826
export type GraphQLObjectTypeConfig<TSource, TContext> = {|
755827
name: string,
756828
interfaces?: Thunk<?Array<GraphQLInterfaceType>>,
@@ -761,6 +833,16 @@ export type GraphQLObjectTypeConfig<TSource, TContext> = {|
761833
extensionASTNodes?: ?$ReadOnlyArray<ObjectTypeExtensionNode>,
762834
|};
763835
836+
export type GraphQLObjectTypeExactConfig<TSource, TContext> = {|
837+
name: string,
838+
interfaces: Array<GraphQLInterfaceType>,
839+
fields: GraphQLFieldExactConfigMap<TSource, TContext>,
840+
isTypeOf: ?GraphQLIsTypeOfFn<TSource, TContext>,
841+
description: ?string,
842+
astNode: ?ObjectTypeDefinitionNode,
843+
extensionASTNodes: $ReadOnlyArray<ObjectTypeExtensionNode>,
844+
|};
845+
764846
export type GraphQLTypeResolver<TSource, TContext> = (
765847
value: TSource,
766848
context: TContext,
@@ -816,7 +898,22 @@ export type GraphQLFieldConfig<
816898
astNode?: ?FieldDefinitionNode,
817899
|};
818900
901+
export type GraphQLFieldExactConfig<
902+
TSource,
903+
TContext,
904+
TArgs = { [argument: string]: any },
905+
> = {|
906+
type: GraphQLOutputType,
907+
args: GraphQLFieldExactConfigArgumentMap,
908+
resolve: ?GraphQLFieldResolver<TSource, TContext, TArgs>,
909+
subscribe: ?GraphQLFieldResolver<TSource, TContext, TArgs>,
910+
deprecationReason: ?string,
911+
description: ?string,
912+
astNode: ?FieldDefinitionNode,
913+
|};
914+
819915
export type GraphQLFieldConfigArgumentMap = ObjMap<GraphQLArgumentConfig>;
916+
export type GraphQLFieldExactConfigArgumentMap = ObjMap<GraphQLArgumentExactConfig>;
820917
821918
export type GraphQLArgumentConfig = {|
822919
type: GraphQLInputType,
@@ -825,9 +922,19 @@ export type GraphQLArgumentConfig = {|
825922
astNode?: ?InputValueDefinitionNode,
826923
|};
827924
925+
export type GraphQLArgumentExactConfig = {|
926+
type: GraphQLInputType,
927+
defaultValue: mixed,
928+
description: ?string,
929+
astNode: ?InputValueDefinitionNode,
930+
|};
931+
828932
export type GraphQLFieldConfigMap<TSource, TContext> = ObjMap<
829933
GraphQLFieldConfig<TSource, TContext>,
830934
>;
935+
export type GraphQLFieldExactConfigMap<TSource, TContext> = ObjMap<
936+
GraphQLFieldExactConfig<TSource, TContext>,
937+
>;
831938
832939
export type GraphQLField<
833940
TSource,
@@ -910,6 +1017,17 @@ export class GraphQLInterfaceType {
9101017
return this._fields;
9111018
}
9121019
1020+
toConfig(): GraphQLInterfaceTypeExactConfig<*, *> {
1021+
return {
1022+
name: this.name,
1023+
fields: fieldsToFieldsConfig(this.getFields()),
1024+
resolveType: this.resolveType,
1025+
description: this.description,
1026+
astNode: this.astNode,
1027+
extensionASTNodes: this.extensionASTNodes || [],
1028+
};
1029+
}
1030+
9131031
toString(): string {
9141032
return this.name;
9151033
}
@@ -933,6 +1051,15 @@ export type GraphQLInterfaceTypeConfig<TSource, TContext> = {|
9331051
extensionASTNodes?: ?$ReadOnlyArray<InterfaceTypeExtensionNode>,
9341052
|};
9351053
1054+
export type GraphQLInterfaceTypeExactConfig<TSource, TContext> = {|
1055+
name: string,
1056+
fields: GraphQLFieldExactConfigMap<TSource, TContext>,
1057+
resolveType: ?GraphQLTypeResolver<TSource, TContext>,
1058+
description: ?string,
1059+
astNode: ?InterfaceTypeDefinitionNode,
1060+
extensionASTNodes: $ReadOnlyArray<InterfaceTypeExtensionNode>,
1061+
|};
1062+
9361063
/**
9371064
* Union Type Definition
9381065
*
@@ -987,6 +1114,17 @@ export class GraphQLUnionType {
9871114
return this._types;
9881115
}
9891116
1117+
toConfig(): GraphQLUnionTypeExactConfig<*, *> {
1118+
return {
1119+
name: this.name,
1120+
types: this.getTypes(),
1121+
resolveType: this.resolveType,
1122+
description: this.description,
1123+
astNode: this.astNode,
1124+
extensionASTNodes: this.extensionASTNodes || [],
1125+
};
1126+
}
1127+
9901128
toString(): string {
9911129
return this.name;
9921130
}
@@ -1022,6 +1160,15 @@ export type GraphQLUnionTypeConfig<TSource, TContext> = {|
10221160
extensionASTNodes?: ?$ReadOnlyArray<UnionTypeExtensionNode>,
10231161
|};
10241162
1163+
export type GraphQLUnionTypeExactConfig<TSource, TContext> = {
1164+
name: string,
1165+
types: Array<GraphQLObjectType>,
1166+
resolveType: ?GraphQLTypeResolver<TSource, TContext>,
1167+
description: ?string,
1168+
astNode: ?UnionTypeDefinitionNode,
1169+
extensionASTNodes: $ReadOnlyArray<UnionTypeExtensionNode>,
1170+
};
1171+
10251172
/**
10261173
* Enum Type Definition
10271174
*
@@ -1101,6 +1248,25 @@ export class GraphQLEnumType /* <T> */ {
11011248
}
11021249
}
11031250
1251+
toConfig(): GraphQLEnumTypeExactConfig {
1252+
return {
1253+
name: this.name,
1254+
values: keyValMap(
1255+
this.getValues(),
1256+
value => value.name,
1257+
value => ({
1258+
value: value.value,
1259+
deprecationReason: value.deprecationReason,
1260+
description: value.description,
1261+
astNode: value.astNode,
1262+
}),
1263+
),
1264+
description: this.description,
1265+
astNode: this.astNode,
1266+
extensionASTNodes: this.extensionASTNodes || [],
1267+
};
1268+
}
1269+
11041270
toString(): string {
11051271
return this.name;
11061272
}
@@ -1148,15 +1314,32 @@ export type GraphQLEnumTypeConfig /* <T> */ = {|
11481314
extensionASTNodes?: ?$ReadOnlyArray<EnumTypeExtensionNode>,
11491315
|};
11501316
1317+
export type GraphQLEnumTypeExactConfig /* <T> */ = {|
1318+
name: string,
1319+
values: GraphQLEnumValueExactConfigMap /* <T> */,
1320+
description: ?string,
1321+
astNode: ?EnumTypeDefinitionNode,
1322+
extensionASTNodes: $ReadOnlyArray<EnumTypeExtensionNode>,
1323+
|};
1324+
11511325
export type GraphQLEnumValueConfigMap /* <T> */ = ObjMap<GraphQLEnumValueConfig /* <T> */>;
11521326
1327+
export type GraphQLEnumValueExactConfigMap /* <T> */ = ObjMap<GraphQLEnumValueExactConfig /* <T> */>;
1328+
11531329
export type GraphQLEnumValueConfig /* <T> */ = {|
11541330
value?: any /* T */,
11551331
deprecationReason?: ?string,
11561332
description?: ?string,
11571333
astNode?: ?EnumValueDefinitionNode,
11581334
|};
11591335
1336+
export type GraphQLEnumValueExactConfig /* <T> */ = {|
1337+
value?: any /* T */,
1338+
deprecationReason: ?string,
1339+
description: ?string,
1340+
astNode: ?EnumValueDefinitionNode,
1341+
|};
1342+
11601343
export type GraphQLEnumValue /* <T> */ = {
11611344
name: string,
11621345
description: ?string,
@@ -1210,6 +1393,25 @@ export class GraphQLInputObjectType {
12101393
return this._fields;
12111394
}
12121395
1396+
toConfig(): GraphQLInputObjectTypeExactConfig {
1397+
return {
1398+
name: this.name,
1399+
fields: keyValMap(
1400+
objectValues(this.getFields()),
1401+
field => field.name,
1402+
field => ({
1403+
type: field.type,
1404+
defaultValue: field.defaultValue,
1405+
description: field.description,
1406+
astNode: field.astNode,
1407+
}),
1408+
),
1409+
description: this.description,
1410+
astNode: this.astNode,
1411+
extensionASTNodes: this.extensionASTNodes || [],
1412+
};
1413+
}
1414+
12131415
toString(): string {
12141416
return this.name;
12151417
}
@@ -1252,14 +1454,30 @@ export type GraphQLInputObjectTypeConfig = {|
12521454
extensionASTNodes?: ?$ReadOnlyArray<InputObjectTypeExtensionNode>,
12531455
|};
12541456

1457+
export type GraphQLInputObjectTypeExactConfig = {|
1458+
name: string,
1459+
fields: GraphQLInputFieldExactConfigMap,
1460+
description: ?string,
1461+
astNode: ?InputObjectTypeDefinitionNode,
1462+
extensionASTNodes: $ReadOnlyArray<InputObjectTypeExtensionNode>,
1463+
|};
1464+
12551465
export type GraphQLInputFieldConfig = {|
12561466
type: GraphQLInputType,
12571467
defaultValue?: mixed,
12581468
description?: ?string,
12591469
astNode?: ?InputValueDefinitionNode,
12601470
|};
12611471

1472+
export type GraphQLInputFieldExactConfig = {|
1473+
type: GraphQLInputType,
1474+
defaultValue: mixed,
1475+
description: ?string,
1476+
astNode: ?InputValueDefinitionNode,
1477+
|};
1478+
12621479
export type GraphQLInputFieldConfigMap = ObjMap<GraphQLInputFieldConfig>;
1480+
export type GraphQLInputFieldExactConfigMap = ObjMap<GraphQLInputFieldExactConfig>;
12631481

12641482
export type GraphQLInputField = {
12651483
name: string,

src/type/directives.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
* @flow strict
88
*/
99

10+
import { argsToArgsConfig } from './definition';
1011
import type {
1112
GraphQLFieldConfigArgumentMap,
13+
GraphQLFieldExactConfigArgumentMap,
1214
GraphQLArgument,
1315
} from './definition';
1416
import { GraphQLNonNull } from './definition';
@@ -84,6 +86,16 @@ export class GraphQLDirective {
8486
toString(): string {
8587
return '@' + this.name;
8688
}
89+
90+
toConfig(): GraphQLDirectiveExactConfig {
91+
return {
92+
name: this.name,
93+
description: this.description,
94+
locations: this.locations,
95+
args: argsToArgsConfig(this.args),
96+
astNode: this.astNode,
97+
};
98+
}
8799
}
88100

89101
// Conditionally apply `[Symbol.toStringTag]` if `Symbol`s are supported
@@ -98,6 +110,14 @@ export type GraphQLDirectiveConfig = {|
98110
astNode?: ?DirectiveDefinitionNode,
99111
|};
100112

113+
export type GraphQLDirectiveExactConfig = {|
114+
name: string,
115+
description: ?string,
116+
locations: Array<DirectiveLocationEnum>,
117+
args: GraphQLFieldExactConfigArgumentMap,
118+
astNode: ?DirectiveDefinitionNode,
119+
|};
120+
101121
/**
102122
* Used to conditionally include fields or fragments.
103123
*/

0 commit comments

Comments
 (0)