Skip to content

Commit 1194822

Browse files
committed
Update implementation and tests for HasArgument
1 parent 17dd8cf commit 1194822

3 files changed

Lines changed: 28 additions & 53 deletions

File tree

lib/graphql/schema/member/has_arguments.rb

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@ def self.extended(cls)
1717
# @param arg_name [Symbol] The underscore-cased name of this argument, `name:` keyword also accepted
1818
# @param type_expr The GraphQL type of this argument; `type:` keyword also accepted
1919
# @param desc [String] Argument description, `description:` keyword also accepted
20-
# @param required [Boolean, :nullable] if true, this argument is non-null; if false, this argument is nullable. If `:nullable`, then the argument must be provided, though it may be `null`.
21-
# @param description [String] Positional argument also accepted
22-
# @param type [Class, Array<Class>] Input type; positional argument also accepted
23-
# @param name [Symbol] positional argument also accepted
24-
# @param default_value [Object]
25-
# @param loads [Class, Array<Class>] A GraphQL type to load for the given ID when one is present
26-
# @param as [Symbol] Override the keyword name when passed to a method
27-
# @param prepare [Symbol] A method to call to transform this argument's valuebefore sending it to field resolution
28-
# @param camelize [Boolean] if true, the name will be camelized when building the schema
29-
# @param from_resolver [Boolean] if true, a Resolver class defined this argument
30-
# @param directives [Hash{Class => Hash}]
31-
# @param deprecation_reason [String]
32-
# @param comment [String] Private, used by GraphQL-Ruby when parsing GraphQL schema files
33-
# @param ast_node [GraphQL::Language::Nodes::InputValueDefinition] Private, used by GraphQL-Ruby when parsing schema files
34-
# @param validates [Hash, nil] Options for building validators, if any should be applied
35-
# @param replace_null_with_default [Boolean] if `true`, incoming values of `null` will be replaced with the configured `default_value`
20+
# @option kwargs [Boolean, :nullable] :required if true, this argument is non-null; if false, this argument is nullable. If `:nullable`, then the argument must be provided, though it may be `null`.
21+
# @option kwargs [String] :description Positional argument also accepted
22+
# @option kwargs [Class, Array<Class>] :type Input type; positional argument also accepted
23+
# @option kwargs [Symbol] :name positional argument also accepted
24+
# @option kwargs [Object] :default_value
25+
# @option kwargs [Class, Array<Class>] :loads A GraphQL type to load for the given ID when one is present
26+
# @option kwargs [Symbol] :as Override the keyword name when passed to a method
27+
# @option kwargs [Symbol] :prepare A method to call to transform this argument's valuebefore sending it to field resolution
28+
# @option kwargs [Boolean] :camelize if true, the name will be camelized when building the schema
29+
# @option kwargs [Boolean] :from_resolver if true, a Resolver class defined this argument
30+
# @option kwargs [Hash{Class => Hash}] :directives
31+
# @option kwargs [String] :deprecation_reason
32+
# @option kwargs [String] :comment Private, used by GraphQL-Ruby when parsing GraphQL schema files
33+
# @option kwargs [GraphQL::Language::Nodes::InputValueDefinition] :ast_node Private, used by GraphQL-Ruby when parsing schema files
34+
# @option kwargs [Hash, nil] :validates Options for building validators, if any should be applied
35+
# @option kwargs [Boolean] :replace_null_with_default if `true`, incoming values of `null` will be replaced with the configured `default_value`
3636
# @param definition_block [Proc] Called with the newly-created {Argument}
37-
# @param custom_kwargs [Hash] Any application-specific options; must be handled by your base {Argument} class
38-
# @return [GraphQL::Schema::Argument] An instance of {argument_class}, created from `*args`
39-
def argument(arg_name = nil, type_expr = nil, desc = nil, required: true, type: nil, name: nil, loads: nil, description: nil, comment: nil, ast_node: nil, default_value: NOT_CONFIGURED, as: nil, from_resolver: false, camelize: true, prepare: nil, validates: nil, directives: nil, deprecation_reason: nil, replace_null_with_default: false, **custom_kwargs, &definition_block)
40-
if loads
41-
loads_name = arg_name || name
37+
# @param kwargs [Hash] Keywords for defining an argument. Any keywords not documented here must be handled by your base Argument class.
38+
# @return [GraphQL::Schema::Argument] An instance of {argument_class} created from these arguments
39+
def argument(arg_name = nil, type_expr = nil, desc = nil, **kwargs, &definition_block)
40+
if kwargs[:loads]
41+
loads_name = arg_name || kwargs[:name]
4242
loads_name_as_string = loads_name.to_s
4343

4444
inferred_arg_name = case loads_name_as_string
@@ -52,28 +52,12 @@ def argument(arg_name = nil, type_expr = nil, desc = nil, required: true, type:
5252
loads_name
5353
end
5454

55-
as ||= inferred_arg_name
55+
kwargs[:as] ||= inferred_arg_name
5656
end
57+
kwargs[:owner] = self
5758
arg_defn = self.argument_class.new(
5859
arg_name, type_expr, desc,
59-
required: required,
60-
type: type,
61-
name: name,
62-
loads: loads,
63-
description: description,
64-
comment: comment,
65-
ast_node: ast_node,
66-
default_value: default_value,
67-
as: as,
68-
from_resolver: from_resolver,
69-
camelize: camelize,
70-
prepare: prepare,
71-
owner: self,
72-
validates: validates,
73-
directives: directives,
74-
deprecation_reason: deprecation_reason,
75-
replace_null_with_default: replace_null_with_default,
76-
**custom_kwargs,
60+
**kwargs,
7761
&definition_block
7862
)
7963
add_argument(arg_defn)

lib/graphql/schema/member/has_fields.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ module HasFields
4545
# @option kwargs [Boolean] :relay_node_field (Private, used by GraphQL-Ruby)
4646
# @option kwargs [Boolean] :relay_nodes_field (Private, used by GraphQL-Ruby)
4747
# @option kwargs [Array<:ast_node, :parent, :lookahead, :owner, :execution_errors, :graphql_name, :argument_details, Symbol>] :extras Extra arguments to be injected into the resolver for this field
48-
4948
# @param kwargs [Hash] Keywords for defining the field. Any not documented here will be passed to your base field class where they must be handled.
5049
# @param definition_block [Proc] an additional block for configuring the field. Receive the field as a block param, or, if no block params are defined, then the block is `instance_eval`'d on the new {Field}.
5150
# @yieldparam field [GraphQL::Schema::Field] The newly-created field instance

spec/graphql/schema/argument_spec.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -800,20 +800,12 @@ class Query < GraphQL::Schema::Object
800800
end
801801

802802
describe "argument definitions" do
803-
it "matches the HasArgument::argument arguments for better IDE support" do
804-
argument_new_arguments = GraphQL::Schema::Argument.instance_method(:initialize).parameters
805-
has_arguments_argument_arguments = GraphQL::Schema::Member::HasArguments.instance_method(:argument).parameters
806-
extra_argument_args = [[:keyrest, :custom_kwargs]]
807-
assert_equal extra_argument_args, has_arguments_argument_arguments - argument_new_arguments
808-
extra_new_args = [[:keyreq, :owner]]
809-
assert_equal extra_new_args, argument_new_arguments - has_arguments_argument_arguments
810-
end
811-
812803
it "HasArguments::argument documents each argument" do
813804
has_arguments_argument_comment = File.read("./lib/graphql/schema/member/has_arguments.rb")[/(\s+#[^\n]*\n)+\s+def argument\(/m]
814-
has_arguments_argument_doc_param_names = has_arguments_argument_comment.split("\n").map { |line| line[/@param (\S+)/]; $1 }.compact
815-
has_arguments_argument_argument_names = GraphQL::Schema::Member::HasArguments.instance_method(:argument).parameters.map { |param| param[1].to_s }
816-
assert_equal has_arguments_argument_doc_param_names.sort, has_arguments_argument_argument_names.sort
805+
has_arguments_argument_doc_param_names = has_arguments_argument_comment.split("\n").map { |line| (line[/@param (\S+)/] || line[/@option kwargs \[.*\] :(\S+)/]); $1 }.compact
806+
argument_initialize_argument_names = GraphQL::Schema::Argument.instance_method(:initialize).parameters.map { |param| param[1].to_s }
807+
assert_equal ["kwargs"], has_arguments_argument_doc_param_names - argument_initialize_argument_names
808+
assert_equal ["owner"], argument_initialize_argument_names - has_arguments_argument_doc_param_names
817809
end
818810

819811
it "Argument::initialize documents each argument" do

0 commit comments

Comments
 (0)