Skip to content

Commit

Permalink
Generalize defineArguments() (#3050)
Browse files Browse the repository at this point in the history
Fields and Directives both define arguments according to identical logic. This generalizes that and shares the implementation across both constructions.
  • Loading branch information
leebyron committed Apr 27, 2021
1 parent 2d48fbb commit 917befd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
26 changes: 15 additions & 11 deletions src/type/definition.js
Expand Up @@ -809,21 +809,11 @@ function defineFieldMap<TSource, TContext>(
`${config.name}.${fieldName} args must be an object with argument names as keys.`,
);

const args = Object.entries(argsConfig).map(([argName, argConfig]) => ({
name: argName,
description: argConfig.description,
type: argConfig.type,
defaultValue: argConfig.defaultValue,
deprecationReason: argConfig.deprecationReason,
extensions: argConfig.extensions && toObjMap(argConfig.extensions),
astNode: argConfig.astNode,
}));

return {
name: fieldName,
description: fieldConfig.description,
type: fieldConfig.type,
args,
args: defineArguments(argsConfig),
resolve: fieldConfig.resolve,
subscribe: fieldConfig.subscribe,
deprecationReason: fieldConfig.deprecationReason,
Expand All @@ -833,6 +823,20 @@ function defineFieldMap<TSource, TContext>(
});
}

export function defineArguments(
config: GraphQLFieldConfigArgumentMap,
): $ReadOnlyArray<GraphQLArgument> {
return Object.entries(config).map(([argName, argConfig]) => ({
name: argName,
description: argConfig.description,
type: argConfig.type,
defaultValue: argConfig.defaultValue,
deprecationReason: argConfig.deprecationReason,
extensions: argConfig.extensions && toObjMap(argConfig.extensions),
astNode: argConfig.astNode,
}));
}

function isPlainObj(obj: mixed): boolean {
return isObjectLike(obj) && !Array.isArray(obj);
}
Expand Down
18 changes: 7 additions & 11 deletions src/type/directives.js
Expand Up @@ -14,7 +14,11 @@ import type {
GraphQLFieldConfigArgumentMap,
} from './definition';
import { GraphQLString, GraphQLBoolean } from './scalars';
import { argsToArgsConfig, GraphQLNonNull } from './definition';
import {
defineArguments,
argsToArgsConfig,
GraphQLNonNull,
} from './definition';

/**
* Test if the given value is a GraphQL directive.
Expand Down Expand Up @@ -44,7 +48,7 @@ export class GraphQLDirective {
name: string;
description: ?string;
locations: Array<DirectiveLocationEnum>;
args: Array<GraphQLArgument>;
args: $ReadOnlyArray<GraphQLArgument>;
isRepeatable: boolean;
extensions: ?ReadOnlyObjMap<mixed>;
astNode: ?DirectiveDefinitionNode;
Expand All @@ -69,15 +73,7 @@ export class GraphQLDirective {
`@${config.name} args must be an object with argument names as keys.`,
);

this.args = Object.entries(args).map(([argName, argConfig]) => ({
name: argName,
description: argConfig.description,
type: argConfig.type,
defaultValue: argConfig.defaultValue,
deprecationReason: argConfig.deprecationReason,
extensions: argConfig.extensions && toObjMap(argConfig.extensions),
astNode: argConfig.astNode,
}));
this.args = defineArguments(args);
}

toConfig(): GraphQLDirectiveNormalizedConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/printSchema.js
Expand Up @@ -228,7 +228,7 @@ function printBlock(items: $ReadOnlyArray<string>): string {
}

function printArgs(
args: Array<GraphQLArgument>,
args: $ReadOnlyArray<GraphQLArgument>,
indentation: string = '',
): string {
if (args.length === 0) {
Expand Down

0 comments on commit 917befd

Please sign in to comment.