Skip to content

Commit 917befd

Browse files
authored
Generalize defineArguments() (#3050)
Fields and Directives both define arguments according to identical logic. This generalizes that and shares the implementation across both constructions.
1 parent 2d48fbb commit 917befd

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

src/type/definition.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -809,21 +809,11 @@ function defineFieldMap<TSource, TContext>(
809809
`${config.name}.${fieldName} args must be an object with argument names as keys.`,
810810
);
811811

812-
const args = Object.entries(argsConfig).map(([argName, argConfig]) => ({
813-
name: argName,
814-
description: argConfig.description,
815-
type: argConfig.type,
816-
defaultValue: argConfig.defaultValue,
817-
deprecationReason: argConfig.deprecationReason,
818-
extensions: argConfig.extensions && toObjMap(argConfig.extensions),
819-
astNode: argConfig.astNode,
820-
}));
821-
822812
return {
823813
name: fieldName,
824814
description: fieldConfig.description,
825815
type: fieldConfig.type,
826-
args,
816+
args: defineArguments(argsConfig),
827817
resolve: fieldConfig.resolve,
828818
subscribe: fieldConfig.subscribe,
829819
deprecationReason: fieldConfig.deprecationReason,
@@ -833,6 +823,20 @@ function defineFieldMap<TSource, TContext>(
833823
});
834824
}
835825

826+
export function defineArguments(
827+
config: GraphQLFieldConfigArgumentMap,
828+
): $ReadOnlyArray<GraphQLArgument> {
829+
return Object.entries(config).map(([argName, argConfig]) => ({
830+
name: argName,
831+
description: argConfig.description,
832+
type: argConfig.type,
833+
defaultValue: argConfig.defaultValue,
834+
deprecationReason: argConfig.deprecationReason,
835+
extensions: argConfig.extensions && toObjMap(argConfig.extensions),
836+
astNode: argConfig.astNode,
837+
}));
838+
}
839+
836840
function isPlainObj(obj: mixed): boolean {
837841
return isObjectLike(obj) && !Array.isArray(obj);
838842
}

src/type/directives.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import type {
1414
GraphQLFieldConfigArgumentMap,
1515
} from './definition';
1616
import { GraphQLString, GraphQLBoolean } from './scalars';
17-
import { argsToArgsConfig, GraphQLNonNull } from './definition';
17+
import {
18+
defineArguments,
19+
argsToArgsConfig,
20+
GraphQLNonNull,
21+
} from './definition';
1822

1923
/**
2024
* Test if the given value is a GraphQL directive.
@@ -44,7 +48,7 @@ export class GraphQLDirective {
4448
name: string;
4549
description: ?string;
4650
locations: Array<DirectiveLocationEnum>;
47-
args: Array<GraphQLArgument>;
51+
args: $ReadOnlyArray<GraphQLArgument>;
4852
isRepeatable: boolean;
4953
extensions: ?ReadOnlyObjMap<mixed>;
5054
astNode: ?DirectiveDefinitionNode;
@@ -69,15 +73,7 @@ export class GraphQLDirective {
6973
`@${config.name} args must be an object with argument names as keys.`,
7074
);
7175

72-
this.args = Object.entries(args).map(([argName, argConfig]) => ({
73-
name: argName,
74-
description: argConfig.description,
75-
type: argConfig.type,
76-
defaultValue: argConfig.defaultValue,
77-
deprecationReason: argConfig.deprecationReason,
78-
extensions: argConfig.extensions && toObjMap(argConfig.extensions),
79-
astNode: argConfig.astNode,
80-
}));
76+
this.args = defineArguments(args);
8177
}
8278

8379
toConfig(): GraphQLDirectiveNormalizedConfig {

src/utilities/printSchema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function printBlock(items: $ReadOnlyArray<string>): string {
228228
}
229229

230230
function printArgs(
231-
args: Array<GraphQLArgument>,
231+
args: $ReadOnlyArray<GraphQLArgument>,
232232
indentation: string = '',
233233
): string {
234234
if (args.length === 0) {

0 commit comments

Comments
 (0)