Skip to content

Commit 272e36e

Browse files
authored
Merge pull request rmosolgo#2365 from rmosolgo/fix-resolver-loads-master
Fix resolver loads
2 parents f95e4e7 + d7782de commit 272e36e

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed

lib/graphql/schema/argument.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class Argument
2424
# @return [Class, Module, nil] If this argument should load an application object, this is the type of object to load
2525
attr_reader :loads
2626

27+
# @return [Boolean] true if a resolver defined this argument
28+
def from_resolver?
29+
@from_resolver
30+
end
31+
2732
# @param arg_name [Symbol]
2833
# @param type_expr
2934
# @param desc [String]
@@ -33,8 +38,9 @@ class Argument
3338
# @param as [Symbol] Override the keyword name when passed to a method
3439
# @param prepare [Symbol] A method to call to transform this argument's valuebefore sending it to field resolution
3540
# @param camelize [Boolean] if true, the name will be camelized when building the schema
41+
# @param from_resolver [Boolean] if true, a Resolver class defined this argument
3642
# @param method_access [Boolean] If false, don't build method access on legacy {Query::Arguments} instances.
37-
def initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, loads: nil, description: nil, default_value: NO_DEFAULT, as: nil, camelize: true, prepare: nil, method_access: true, owner:, &definition_block)
43+
def initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, method_access: true, owner:, &definition_block)
3844
arg_name ||= name
3945
name_str = camelize ? Member::BuildType.camelize(arg_name.to_s) : arg_name.to_s
4046
@name = name_str.freeze
@@ -47,6 +53,8 @@ def initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil
4753
@loads = loads
4854
@keyword = as || Schema::Member::BuildType.underscore(@name).to_sym
4955
@prepare = prepare
56+
@ast_node = ast_node
57+
@from_resolver = from_resolver
5058
@method_access = method_access
5159

5260
if definition_block

lib/graphql/schema/input_object.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(values = nil, ruby_kwargs: nil, context:, defaults_used:)
2323
ruby_kwargs_key = arg_defn.keyword
2424
loads = arg_defn.loads
2525

26-
if @ruby_style_hash.key?(ruby_kwargs_key) && loads
26+
if @ruby_style_hash.key?(ruby_kwargs_key) && loads && !arg_defn.from_resolver?
2727
value = @ruby_style_hash[ruby_kwargs_key]
2828
@ruby_style_hash[ruby_kwargs_key] = if arg_defn.type.list?
2929
GraphQL::Execution::Lazy.all(value.map { |val| load_application_object(arg_defn, loads, val) })
@@ -99,8 +99,6 @@ class << self
9999
attr_accessor :arguments_class
100100

101101
def argument(*args, **kwargs, &block)
102-
# Translate `loads:` to `as:` if needed`
103-
*args, kwargs = argument_with_loads(*args, **kwargs, &block)
104102
argument_defn = super(*args, **kwargs, &block)
105103
# Add a method access
106104
method_name = argument_defn.keyword

lib/graphql/schema/member/has_arguments.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ def self.extended(cls)
1313
cls.include(ArgumentObjectLoader)
1414
end
1515

16-
def argument_with_loads(*args, **kwargs)
16+
# @see {GraphQL::Schema::Argument#initialize} for parameters
17+
# @return [GraphQL::Schema::Argument] An instance of {arguments_class}, created from `*args`
18+
def argument(*args, **kwargs, &block)
19+
kwargs[:owner] = self
1720
loads = kwargs[:loads]
1821
if loads
1922
name = args[0]
@@ -32,14 +35,6 @@ def argument_with_loads(*args, **kwargs)
3235

3336
kwargs[:as] ||= inferred_arg_name
3437
end
35-
36-
return [*args, **kwargs]
37-
end
38-
39-
# @see {GraphQL::Schema::Argument#initialize} for parameters
40-
# @return [GraphQL::Schema::Argument] An instance of {arguments_class}, created from `*args`
41-
def argument(*args, **kwargs, &block)
42-
kwargs[:owner] = self
4338
arg_defn = self.argument_class.new(*args, **kwargs, &block)
4439
add_argument(arg_defn)
4540
end

lib/graphql/schema/resolver.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,11 @@ def type_expr
278278
# Add an argument to this field's signature, but
279279
# also add some preparation hook methods which will be used for this argument
280280
# @see {GraphQL::Schema::Argument#initialize} for the signature
281-
def argument(name, type, *rest, loads: nil, **kwargs, &block)
282-
*args, kwargs = argument_with_loads(name, type, *rest, loads: loads, **kwargs, &block)
283-
# Short-circuit the InputObject's own `loads:` implementation
281+
def argument(*args, **kwargs, &block)
282+
loads = kwargs[:loads]
283+
# Use `from_resolver: true` to short-circuit the InputObject's own `loads:` implementation
284284
# so that we can support `#load_{x}` methods below.
285-
kwargs.delete(:loads)
286-
arg_defn = super(*args, **kwargs)
285+
arg_defn = super(*args, from_resolver: true, **kwargs)
287286
own_arguments_loads_as_type[arg_defn.keyword] = loads if loads
288287

289288
if loads && arg_defn.type.list?

spec/graphql/schema/resolver_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,11 @@ def add_error_assertions(field_name, description)
563563
refute res.key?("errors"), "#{description}: silent auth failure (no top-level error)"
564564
end
565565

566+
it "keeps track of the `loads:` option" do
567+
arg = ResolverTest::MutationWithNullableLoadsArgument.arguments["labelId"]
568+
assert_equal ResolverTest::HasValue, arg.loads
569+
end
570+
566571
describe "ready?" do
567572
it "can raise errors" do
568573
res = exec_query("{ int: prepResolver5(int: 5) }")

0 commit comments

Comments
 (0)