From 917befd7a54fe99d6141fa0ed92bc2b82544625f Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Mon, 26 Apr 2021 23:59:41 -0700 Subject: [PATCH] Generalize defineArguments() (#3050) Fields and Directives both define arguments according to identical logic. This generalizes that and shares the implementation across both constructions. --- src/type/definition.js | 26 +++++++++++++++----------- src/type/directives.js | 18 +++++++----------- src/utilities/printSchema.js | 2 +- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/type/definition.js b/src/type/definition.js index 9461e79fb8..9b3f022449 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -809,21 +809,11 @@ function defineFieldMap( `${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, @@ -833,6 +823,20 @@ function defineFieldMap( }); } +export function defineArguments( + config: GraphQLFieldConfigArgumentMap, +): $ReadOnlyArray { + 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); } diff --git a/src/type/directives.js b/src/type/directives.js index cbe32751a4..a6ca5005c9 100644 --- a/src/type/directives.js +++ b/src/type/directives.js @@ -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. @@ -44,7 +48,7 @@ export class GraphQLDirective { name: string; description: ?string; locations: Array; - args: Array; + args: $ReadOnlyArray; isRepeatable: boolean; extensions: ?ReadOnlyObjMap; astNode: ?DirectiveDefinitionNode; @@ -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 { diff --git a/src/utilities/printSchema.js b/src/utilities/printSchema.js index b77923eb41..f20b8edb42 100644 --- a/src/utilities/printSchema.js +++ b/src/utilities/printSchema.js @@ -228,7 +228,7 @@ function printBlock(items: $ReadOnlyArray): string { } function printArgs( - args: Array, + args: $ReadOnlyArray, indentation: string = '', ): string { if (args.length === 0) {