Skip to content

Commit 920fc67

Browse files
committed
Unify InputValueConfig in schema definition
Defines a central `GraphQLInputValueConfig` and `GraphQLInputValue` as well as single definitions for converting between them, unifying this common functionality between input values and arguments. This is a pre-req for #3049
1 parent 3cfb2be commit 920fc67

File tree

8 files changed

+151
-142
lines changed

8 files changed

+151
-142
lines changed

src/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,13 @@ export {
145145
GraphQLSchemaExtensions,
146146
GraphQLDirectiveConfig,
147147
GraphQLDirectiveExtensions,
148+
GraphQLDirectiveArgument,
149+
GraphQLDirectiveArgumentConfig,
148150
GraphQLArgument,
149151
GraphQLArgumentConfig,
150152
GraphQLArgumentExtensions,
153+
GraphQLInputValue,
154+
GraphQLInputValueConfig,
151155
GraphQLEnumTypeConfig,
152156
GraphQLEnumTypeExtensions,
153157
GraphQLEnumValue,

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ export type {
144144
GraphQLDirectiveConfig,
145145
GraphQLArgument,
146146
GraphQLArgumentConfig,
147+
GraphQLDirectiveArgument,
148+
GraphQLDirectiveArgumentConfig,
149+
GraphQLInputValue,
150+
GraphQLInputValueConfig,
147151
GraphQLEnumTypeConfig,
148152
GraphQLEnumValue,
149153
GraphQLEnumValueConfig,

src/type/definition.d.ts

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,6 @@ export class GraphQLObjectType<TSource = any, TContext = any> {
441441
get [Symbol.toStringTag](): string;
442442
}
443443

444-
export function argsToArgsConfig(
445-
args: ReadonlyArray<GraphQLArgument>,
446-
): GraphQLFieldConfigArgumentMap;
447-
448444
export interface GraphQLObjectTypeConfig<TSource, TContext> {
449445
name: string;
450446
description?: Maybe<string>;
@@ -545,14 +541,7 @@ export interface GraphQLArgumentExtensions {
545541
[attributeName: string]: unknown;
546542
}
547543

548-
export interface GraphQLArgumentConfig {
549-
description?: Maybe<string>;
550-
type: GraphQLInputType;
551-
defaultValue?: unknown;
552-
deprecationReason?: Maybe<string>;
553-
extensions?: Maybe<Readonly<GraphQLArgumentExtensions>>;
554-
astNode?: Maybe<InputValueDefinitionNode>;
555-
}
544+
export type GraphQLArgumentConfig = GraphQLInputValueConfig<GraphQLArgumentExtensions>;
556545

557546
export type GraphQLFieldConfigMap<TSource, TContext> = ObjMap<
558547
GraphQLFieldConfig<TSource, TContext>
@@ -574,21 +563,32 @@ export interface GraphQLField<
574563
astNode?: Maybe<FieldDefinitionNode>;
575564
}
576565

577-
export interface GraphQLArgument {
566+
export type GraphQLArgument = GraphQLInputValue<GraphQLArgumentExtensions>;
567+
568+
export function isRequiredArgument(arg: GraphQLArgument): boolean;
569+
570+
export type GraphQLFieldMap<TSource, TContext> = ObjMap<
571+
GraphQLField<TSource, TContext>
572+
>;
573+
574+
export interface GraphQLInputValue<Extensions> {
578575
name: string;
579576
description: Maybe<string>;
580577
type: GraphQLInputType;
581578
defaultValue: unknown;
582579
deprecationReason: Maybe<string>;
583-
extensions: Maybe<Readonly<GraphQLArgumentExtensions>>;
580+
extensions: Maybe<Readonly<Extensions>>;
584581
astNode: Maybe<InputValueDefinitionNode>;
585582
}
586583

587-
export function isRequiredArgument(arg: GraphQLArgument): boolean;
588-
589-
export type GraphQLFieldMap<TSource, TContext> = ObjMap<
590-
GraphQLField<TSource, TContext>
591-
>;
584+
export interface GraphQLInputValueConfig<Extensions> {
585+
description?: Maybe<string>;
586+
type: GraphQLInputType;
587+
defaultValue?: unknown;
588+
deprecationReason?: Maybe<string>;
589+
extensions?: Maybe<Readonly<Extensions>>;
590+
astNode?: Maybe<InputValueDefinitionNode>;
591+
}
592592

593593
/**
594594
* Custom extensions
@@ -913,29 +913,14 @@ export interface GraphQLInputObjectTypeConfig {
913913
* an object which can contain all the values you need.
914914
*/
915915
export interface GraphQLInputFieldExtensions {
916-
[attributeName: string]: any;
916+
[attributeName: string]: unknown;
917917
}
918918

919-
export interface GraphQLInputFieldConfig {
920-
description?: Maybe<string>;
921-
type: GraphQLInputType;
922-
defaultValue?: unknown;
923-
deprecationReason?: Maybe<string>;
924-
extensions?: Maybe<Readonly<GraphQLInputFieldExtensions>>;
925-
astNode?: Maybe<InputValueDefinitionNode>;
926-
}
919+
export type GraphQLInputFieldConfig = GraphQLInputValueConfig<GraphQLInputFieldExtensions>;
927920

928921
export type GraphQLInputFieldConfigMap = ObjMap<GraphQLInputFieldConfig>;
929922

930-
export interface GraphQLInputField {
931-
name: string;
932-
description?: Maybe<string>;
933-
type: GraphQLInputType;
934-
defaultValue?: unknown;
935-
deprecationReason: Maybe<string>;
936-
extensions: Maybe<Readonly<GraphQLInputFieldExtensions>>;
937-
astNode?: Maybe<InputValueDefinitionNode>;
938-
}
923+
export type GraphQLInputField = GraphQLInputValue<GraphQLInputFieldExtensions>;
939924

940925
export function isRequiredInputField(field: GraphQLInputField): boolean;
941926

src/type/definition.js

Lines changed: 66 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,9 @@ function defineFieldMap<TSource, TContext>(
821821
name: fieldName,
822822
description: fieldConfig.description,
823823
type: fieldConfig.type,
824-
args: defineArguments(argsConfig),
824+
args: Object.entries(argsConfig).map(([argName, argConfig]) =>
825+
defineInputValue(argConfig, argName),
826+
),
825827
resolve: fieldConfig.resolve,
826828
subscribe: fieldConfig.subscribe,
827829
deprecationReason: fieldConfig.deprecationReason,
@@ -831,20 +833,6 @@ function defineFieldMap<TSource, TContext>(
831833
});
832834
}
833835

834-
export function defineArguments(
835-
config: GraphQLFieldConfigArgumentMap,
836-
): $ReadOnlyArray<GraphQLArgument> {
837-
return Object.entries(config).map(([argName, argConfig]) => ({
838-
name: argName,
839-
description: argConfig.description,
840-
type: argConfig.type,
841-
defaultValue: argConfig.defaultValue,
842-
deprecationReason: argConfig.deprecationReason,
843-
extensions: argConfig.extensions && toObjMap(argConfig.extensions),
844-
astNode: argConfig.astNode,
845-
}));
846-
}
847-
848836
function isPlainObj(obj: mixed): boolean {
849837
return isObjectLike(obj) && !Array.isArray(obj);
850838
}
@@ -855,7 +843,7 @@ function fieldsToFieldsConfig(
855843
return mapValue(fields, (field) => ({
856844
description: field.description,
857845
type: field.type,
858-
args: argsToArgsConfig(field.args),
846+
args: keyValMap(field.args, (arg) => arg.name, inputValueToConfig),
859847
resolve: field.resolve,
860848
subscribe: field.subscribe,
861849
deprecationReason: field.deprecationReason,
@@ -864,26 +852,6 @@ function fieldsToFieldsConfig(
864852
}));
865853
}
866854

867-
/**
868-
* @internal
869-
*/
870-
export function argsToArgsConfig(
871-
args: $ReadOnlyArray<GraphQLArgument>,
872-
): GraphQLFieldConfigArgumentMap {
873-
return keyValMap(
874-
args,
875-
(arg) => arg.name,
876-
(arg) => ({
877-
description: arg.description,
878-
type: arg.type,
879-
defaultValue: arg.defaultValue,
880-
deprecationReason: arg.deprecationReason,
881-
extensions: arg.extensions,
882-
astNode: arg.astNode,
883-
}),
884-
);
885-
}
886-
887855
export type GraphQLObjectTypeConfig<TSource, TContext> = {|
888856
name: string,
889857
description?: ?string,
@@ -960,14 +928,7 @@ export type GraphQLFieldConfig<
960928

961929
export type GraphQLFieldConfigArgumentMap = ObjMap<GraphQLArgumentConfig>;
962930

963-
export type GraphQLArgumentConfig = {|
964-
description?: ?string,
965-
type: GraphQLInputType,
966-
defaultValue?: mixed,
967-
extensions?: ?ReadOnlyObjMapLike<mixed>,
968-
deprecationReason?: ?string,
969-
astNode?: ?InputValueDefinitionNode,
970-
|};
931+
export type GraphQLArgumentConfig = GraphQLInputValueConfig;
971932

972933
export type GraphQLFieldConfigMap<TSource, TContext> = ObjMap<
973934
GraphQLFieldConfig<TSource, TContext>,
@@ -989,7 +950,55 @@ export type GraphQLField<
989950
astNode: ?FieldDefinitionNode,
990951
|};
991952

992-
export type GraphQLArgument = {|
953+
export type GraphQLArgument = GraphQLInputValue;
954+
955+
export function isRequiredArgument(arg: GraphQLArgument): boolean %checks {
956+
return isNonNullType(arg.type) && arg.defaultValue === undefined;
957+
}
958+
959+
export type GraphQLFieldMap<TSource, TContext> = ObjMap<
960+
GraphQLField<TSource, TContext>,
961+
>;
962+
963+
/**
964+
* @internal
965+
*/
966+
export function defineInputValue(
967+
config: GraphQLInputValueConfig,
968+
name: string,
969+
): GraphQLInputValue {
970+
devAssert(
971+
!('resolve' in config),
972+
`${name} has a resolve property, but inputs cannot define resolvers.`,
973+
);
974+
return {
975+
name,
976+
description: config.description,
977+
type: config.type,
978+
defaultValue: config.defaultValue,
979+
deprecationReason: config.deprecationReason,
980+
extensions: config.extensions && toObjMap(config.extensions),
981+
astNode: config.astNode,
982+
};
983+
}
984+
985+
/**
986+
* @internal
987+
*/
988+
export function inputValueToConfig(
989+
inputValue: GraphQLInputValue,
990+
): GraphQLInputValueConfig {
991+
return {
992+
description: inputValue.description,
993+
type: inputValue.type,
994+
defaultValue: inputValue.defaultValue,
995+
deprecationReason: inputValue.deprecationReason,
996+
extensions: inputValue.extensions,
997+
astNode: inputValue.astNode,
998+
};
999+
}
1000+
1001+
export type GraphQLInputValue = {|
9931002
name: string,
9941003
description: ?string,
9951004
type: GraphQLInputType,
@@ -999,13 +1008,14 @@ export type GraphQLArgument = {|
9991008
astNode: ?InputValueDefinitionNode,
10001009
|};
10011010

1002-
export function isRequiredArgument(arg: GraphQLArgument): boolean %checks {
1003-
return isNonNullType(arg.type) && arg.defaultValue === undefined;
1004-
}
1005-
1006-
export type GraphQLFieldMap<TSource, TContext> = ObjMap<
1007-
GraphQLField<TSource, TContext>,
1008-
>;
1011+
export type GraphQLInputValueConfig = {|
1012+
description?: ?string,
1013+
type: GraphQLInputType,
1014+
defaultValue?: mixed,
1015+
deprecationReason?: ?string,
1016+
extensions?: ?ReadOnlyObjMapLike<mixed>,
1017+
astNode?: ?InputValueDefinitionNode,
1018+
|};
10091019

10101020
/**
10111021
* Interface Type Definition
@@ -1497,18 +1507,10 @@ export class GraphQLInputObjectType {
14971507
}
14981508

14991509
toConfig(): GraphQLInputObjectTypeNormalizedConfig {
1500-
const fields = mapValue(this.getFields(), (field) => ({
1501-
description: field.description,
1502-
type: field.type,
1503-
defaultValue: field.defaultValue,
1504-
extensions: field.extensions,
1505-
astNode: field.astNode,
1506-
}));
1507-
15081510
return {
15091511
name: this.name,
15101512
description: this.description,
1511-
fields,
1513+
fields: mapValue(this.getFields(), inputValueToConfig),
15121514
extensions: this.extensions,
15131515
astNode: this.astNode,
15141516
extensionASTNodes: this.extensionASTNodes,
@@ -1542,16 +1544,7 @@ function defineInputFieldMap(
15421544
!('resolve' in fieldConfig),
15431545
`${config.name}.${fieldName} field has a resolve property, but Input Types cannot define resolvers.`,
15441546
);
1545-
1546-
return {
1547-
name: fieldName,
1548-
description: fieldConfig.description,
1549-
type: fieldConfig.type,
1550-
defaultValue: fieldConfig.defaultValue,
1551-
deprecationReason: fieldConfig.deprecationReason,
1552-
extensions: fieldConfig.extensions && toObjMap(fieldConfig.extensions),
1553-
astNode: fieldConfig.astNode,
1554-
};
1547+
return defineInputValue(fieldConfig, fieldName);
15551548
});
15561549
}
15571550

@@ -1571,26 +1564,11 @@ type GraphQLInputObjectTypeNormalizedConfig = {|
15711564
extensionASTNodes: $ReadOnlyArray<InputObjectTypeExtensionNode>,
15721565
|};
15731566

1574-
export type GraphQLInputFieldConfig = {|
1575-
description?: ?string,
1576-
type: GraphQLInputType,
1577-
defaultValue?: mixed,
1578-
deprecationReason?: ?string,
1579-
extensions?: ?ReadOnlyObjMapLike<mixed>,
1580-
astNode?: ?InputValueDefinitionNode,
1581-
|};
1567+
export type GraphQLInputFieldConfig = GraphQLInputValueConfig;
15821568

15831569
export type GraphQLInputFieldConfigMap = ObjMap<GraphQLInputFieldConfig>;
15841570

1585-
export type GraphQLInputField = {|
1586-
name: string,
1587-
description: ?string,
1588-
type: GraphQLInputType,
1589-
defaultValue: mixed,
1590-
deprecationReason: ?string,
1591-
extensions: ?ReadOnlyObjMap<mixed>,
1592-
astNode: ?InputValueDefinitionNode,
1593-
|};
1571+
export type GraphQLInputField = GraphQLInputValue;
15941572

15951573
export function isRequiredInputField(
15961574
field: GraphQLInputField,

0 commit comments

Comments
 (0)