Skip to content

Commit 42b5c1f

Browse files
committed
Don't pass resolver objects to field extensions
1 parent d483c91 commit 42b5c1f

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

lib/graphql/schema/field.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,6 @@ def resolve_field(obj, args, ctx)
388388
if @resolve_proc
389389
# Might be nil, still want to call the func in that case
390390
@resolve_proc.call(inner_obj, args, ctx)
391-
elsif @resolver_class
392-
singleton_inst = @resolver_class.new(object: inner_obj, context: query_ctx)
393-
public_send_field(singleton_inst, args, ctx)
394391
else
395392
public_send_field(after_obj, args, ctx)
396393
end
@@ -463,6 +460,13 @@ def public_send_field(obj, graphql_args, field_ctx)
463460

464461
query_ctx = field_ctx.query.context
465462
with_extensions(obj, ruby_kwargs, query_ctx) do |extended_obj, extended_args|
463+
if @resolver_class
464+
if extended_obj.is_a?(GraphQL::Schema::Object)
465+
extended_obj = extended_obj.object
466+
end
467+
extended_obj = @resolver_class.new(object: extended_obj, context: query_ctx)
468+
end
469+
466470
if extended_args.any?
467471
extended_obj.public_send(@method_sym, **extended_args)
468472
else

lib/graphql/schema/field/connection_extension.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ def after_resolve(value:, object:, arguments:, context:, memo:)
2929
elsif value.nil?
3030
nil
3131
else
32-
if object.is_a?(GraphQL::Schema::Object)
33-
object = object.object
34-
end
3532
connection_class = GraphQL::Relay::BaseConnection.connection_for_nodes(value)
3633
connection_class.new(
3734
value,

spec/integration/rails/graphql/relay/connection_resolve_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@
5353
end
5454
end
5555

56+
describe "when a resolver is used" do
57+
it "returns the items with the correct parent" do
58+
resolver_query_str = <<-GRAPHQL
59+
{
60+
rebels {
61+
shipsByResolver {
62+
parentClassName
63+
}
64+
}
65+
}
66+
GRAPHQL
67+
result = star_wars_query(resolver_query_str)
68+
assert_equal "StarWars::FactionRecord", result["data"]["rebels"]["shipsByResolver"]["parentClassName"]
69+
end
70+
end
71+
5672
describe "when nil is returned" do
5773
it "becomes null" do
5874
result = star_wars_query(query_string, { "name" => "null" })

spec/support/star_wars/schema.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ def parent_class_name
108108
end
109109
end
110110

111+
class ShipsByResolver < GraphQL::Schema::Resolver
112+
type ShipConnectionWithParentType, null: false
113+
114+
def resolve
115+
object.ships.map { |ship_id| StarWars::DATA["Ship"][ship_id] }
116+
end
117+
end
118+
111119
class Faction < GraphQL::Schema::Object
112120
implements GraphQL::Relay::Node.interface
113121

@@ -117,6 +125,8 @@ class Faction < GraphQL::Schema::Object
117125
argument :name_includes, String, required: false
118126
end
119127

128+
field :shipsByResolver, resolver: ShipsByResolver, connection: true
129+
120130
def ships(name_includes: nil)
121131
all_ships = object.ships.map {|ship_id| StarWars::DATA["Ship"][ship_id] }
122132
if name_includes

0 commit comments

Comments
 (0)