Skip to content

Commit b851229

Browse files
authored
Merge pull request #3967 from rmosolgo/node-interface-connection-type-fix
Fix edge/connection class lookup when Node is included in another module
2 parents db7c89e + c44417e commit b851229

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

lib/graphql/schema/member/relay_shortcuts.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,31 @@ def edge_type_class(new_edge_type_class = nil)
88
if new_edge_type_class
99
@edge_type_class = new_edge_type_class
1010
else
11-
@edge_type_class || find_inherited_value(:edge_type_class, Types::Relay::BaseEdge)
11+
# Don't call `ancestor.edge_type_class`
12+
# because we don't want a fallback from any ancestors --
13+
# only apply the fallback if _no_ ancestor has a configured value!
14+
for ancestor in self.ancestors
15+
if ancestor.respond_to?(:configured_edge_type_class, true) && (etc = ancestor.configured_edge_type_class)
16+
return etc
17+
end
18+
end
19+
Types::Relay::BaseEdge
1220
end
1321
end
1422

1523
def connection_type_class(new_connection_type_class = nil)
1624
if new_connection_type_class
1725
@connection_type_class = new_connection_type_class
1826
else
19-
@connection_type_class || find_inherited_value(:connection_type_class, Types::Relay::BaseConnection)
27+
# Don't call `ancestor.connection_type_class`
28+
# because we don't want a fallback from any ancestors --
29+
# only apply the fallback if _no_ ancestor has a configured value!
30+
for ancestor in self.ancestors
31+
if ancestor.respond_to?(:configured_connection_type_class, true) && (ctc = ancestor.configured_connection_type_class)
32+
return ctc
33+
end
34+
end
35+
Types::Relay::BaseConnection
2036
end
2137
end
2238

@@ -41,6 +57,16 @@ def connection_type
4157
end
4258
end
4359
end
60+
61+
protected
62+
63+
def configured_connection_type_class
64+
@connection_type_class
65+
end
66+
67+
def configured_edge_type_class
68+
@edge_type_class
69+
end
4470
end
4571
end
4672
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
require "spec_helper"
3+
4+
describe GraphQL::Schema::Member::RelayShortcuts do
5+
describe ".connection_type_class, .edge_type_class" do
6+
class CustomBaseConnectionType < GraphQL::Types::Relay::BaseConnection
7+
end
8+
9+
class CustomEdgeType < GraphQL::Types::Relay::BaseEdge
10+
end
11+
12+
class ConnectionTypeBaseObject < GraphQL::Schema::Object
13+
connection_type_class CustomBaseConnectionType
14+
edge_type_class CustomEdgeType
15+
end
16+
17+
class ImplementationTypeObject < ConnectionTypeBaseObject
18+
implements GraphQL::Types::Relay::Node
19+
end
20+
21+
module ConnectionTypeBaseInterface
22+
include GraphQL::Schema::Interface
23+
connection_type_class CustomBaseConnectionType
24+
edge_type_class CustomEdgeType
25+
end
26+
27+
module NodeImplementingInterface
28+
include ConnectionTypeBaseInterface
29+
implements GraphQL::Types::Relay::Node
30+
end
31+
32+
it "uses the custom class, even when Node is implemented" do
33+
assert_equal CustomBaseConnectionType, ConnectionTypeBaseObject.connection_type_class
34+
assert_equal GraphQL::Types::Relay::BaseConnection, GraphQL::Types::Relay::Node.connection_type_class
35+
assert_equal CustomBaseConnectionType, ImplementationTypeObject.connection_type_class
36+
assert_equal CustomBaseConnectionType, ConnectionTypeBaseInterface.connection_type_class
37+
assert_equal CustomBaseConnectionType, NodeImplementingInterface.connection_type_class
38+
39+
assert_equal CustomEdgeType, ConnectionTypeBaseObject.edge_type_class
40+
assert_equal GraphQL::Types::Relay::BaseEdge, GraphQL::Types::Relay::Node.edge_type_class
41+
assert_equal CustomEdgeType, ImplementationTypeObject.edge_type_class
42+
assert_equal CustomEdgeType, ConnectionTypeBaseInterface.edge_type_class
43+
assert_equal CustomEdgeType, NodeImplementingInterface.edge_type_class
44+
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)