Skip to content

Commit 8a03c24

Browse files
Robert Mosolgormosolgo
authored andcommitted
Merge pull request #1774 from rmosolgo/fix-connections
Don't pass resolver objects to field extensions
1 parent 09c0ec7 commit 8a03c24

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

lib/graphql/schema/field.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,6 @@ def resolve_field(obj, args, ctx)
392392
if @resolve_proc
393393
# Might be nil, still want to call the func in that case
394394
@resolve_proc.call(inner_obj, args, ctx)
395-
elsif @resolver_class
396-
singleton_inst = @resolver_class.new(object: inner_obj, context: query_ctx)
397-
public_send_field(singleton_inst, args, ctx)
398395
else
399396
public_send_field(after_obj, args, ctx)
400397
end
@@ -467,6 +464,13 @@ def public_send_field(obj, graphql_args, field_ctx)
467464

468465
query_ctx = field_ctx.query.context
469466
with_extensions(obj, ruby_kwargs, query_ctx) do |extended_obj, extended_args|
467+
if @resolver_class
468+
if extended_obj.is_a?(GraphQL::Schema::Object)
469+
extended_obj = extended_obj.object
470+
end
471+
extended_obj = @resolver_class.new(object: extended_obj, context: query_ctx)
472+
end
473+
470474
if extended_args.any?
471475
extended_obj.public_send(@method_sym, **extended_args)
472476
else

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)