Skip to content

Generalize defineArguments() #3050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/type/definition.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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