Skip to content

Commit 26803ad

Browse files
author
Robert Mosolgo
authored
Merge pull request rmosolgo#3486 from rmosolgo/fix-load-methods
Don't override inherited load_ argument methods
2 parents 8cbbffd + 7ee502b commit 26803ad

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

lib/graphql/schema/resolver.rb

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -333,30 +333,32 @@ def argument(*args, **kwargs, &block)
333333
arg_defn = super(*args, from_resolver: true, **kwargs)
334334
own_arguments_loads_as_type[arg_defn.keyword] = loads if loads
335335

336-
if loads && arg_defn.type.list?
337-
class_eval <<-RUBY, __FILE__, __LINE__ + 1
338-
def load_#{arg_defn.keyword}(values)
339-
argument = @arguments_by_keyword[:#{arg_defn.keyword}]
340-
lookup_as_type = @arguments_loads_as_type[:#{arg_defn.keyword}]
341-
context.schema.after_lazy(values) do |values2|
342-
GraphQL::Execution::Lazy.all(values2.map { |value| load_application_object(argument, lookup_as_type, value, context) })
336+
if !method_defined?(:"load_#{arg_defn.keyword}")
337+
if loads && arg_defn.type.list?
338+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
339+
def load_#{arg_defn.keyword}(values)
340+
argument = @arguments_by_keyword[:#{arg_defn.keyword}]
341+
lookup_as_type = @arguments_loads_as_type[:#{arg_defn.keyword}]
342+
context.schema.after_lazy(values) do |values2|
343+
GraphQL::Execution::Lazy.all(values2.map { |value| load_application_object(argument, lookup_as_type, value, context) })
344+
end
343345
end
346+
RUBY
347+
elsif loads
348+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
349+
def load_#{arg_defn.keyword}(value)
350+
argument = @arguments_by_keyword[:#{arg_defn.keyword}]
351+
lookup_as_type = @arguments_loads_as_type[:#{arg_defn.keyword}]
352+
load_application_object(argument, lookup_as_type, value, context)
353+
end
354+
RUBY
355+
else
356+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
357+
def load_#{arg_defn.keyword}(value)
358+
value
359+
end
360+
RUBY
344361
end
345-
RUBY
346-
elsif loads
347-
class_eval <<-RUBY, __FILE__, __LINE__ + 1
348-
def load_#{arg_defn.keyword}(value)
349-
argument = @arguments_by_keyword[:#{arg_defn.keyword}]
350-
lookup_as_type = @arguments_loads_as_type[:#{arg_defn.keyword}]
351-
load_application_object(argument, lookup_as_type, value, context)
352-
end
353-
RUBY
354-
else
355-
class_eval <<-RUBY, __FILE__, __LINE__ + 1
356-
def load_#{arg_defn.keyword}(value)
357-
value
358-
end
359-
RUBY
360362
end
361363

362364
arg_defn

spec/graphql/schema/resolver_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,19 @@ def add_error_assertions(field_name, description)
733733
end
734734
end
735735

736+
describe "load_* argument methods" do
737+
it "doesn't override inherited methods" do
738+
r1 = Class.new(GraphQL::Schema::Resolver) do
739+
def load_input(input); end
740+
end
741+
r2 = Class.new(r1) do
742+
argument :input, Integer, required: false
743+
end
744+
745+
assert_equal r1.instance_method(:load_input).source_location, r2.instance_method(:load_input).source_location
746+
end
747+
end
748+
736749
describe "Loading inputs" do
737750
it "calls object_from_id" do
738751
res = exec_query('{ prepResolver9(intId: "5") { value } }')

0 commit comments

Comments
 (0)