Skip to content

Commit

Permalink
Merge pull request #4668 from rmosolgo/conn-name-fix
Browse files Browse the repository at this point in the history
Don't assume connection behaviors for types named Connection
  • Loading branch information
rmosolgo authored Oct 17, 2023
2 parents ab01562 + 400024c commit 1bafe88
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
11 changes: 0 additions & 11 deletions lib/graphql/schema/build_from_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,12 @@ def build_fields(owner, field_definitions, type_resolver, default_resolve:)
builder = self

field_definitions.each do |field_definition|
type_name = resolve_type_name(field_definition.type)
resolve_method_name = -"resolve_field_#{field_definition.name}"
schema_field_defn = owner.field(
field_definition.name,
description: field_definition.description,
type: type_resolver.call(field_definition.type),
null: true,
connection: type_name.end_with?("Connection"),
connection_extension: nil,
deprecation_reason: build_deprecation_reason(field_definition.directives),
ast_node: field_definition,
Expand Down Expand Up @@ -487,15 +485,6 @@ def build_resolve_type(lookup_hash, directives, missing_type_handler)
}
resolve_type_proc
end

def resolve_type_name(type)
case type
when GraphQL::Language::Nodes::TypeName
return type.name
else
resolve_type_name(type.of_type)
end
end
end

private_constant :Builder
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def connection?
# As a last ditch, try to force loading the return type:
type.unwrap.name
end
@connection = return_type_name.end_with?("Connection")
@connection = return_type_name.end_with?("Connection") && return_type_name != "Connection"
else
@connection
end
Expand Down
2 changes: 0 additions & 2 deletions lib/graphql/schema/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def build_fields(type_defn, fields, type_resolver)
while (of_type = unwrapped_field_hash["ofType"])
unwrapped_field_hash = of_type
end
type_name = unwrapped_field_hash["name"]

type_defn.field(
field_hash["name"],
Expand All @@ -186,7 +185,6 @@ def build_fields(type_defn, fields, type_resolver)
null: true,
camelize: false,
connection_extension: nil,
connection: type_name.end_with?("Connection"),
) do
if field_hash["args"].any?
loader.build_arguments(self, field_hash["args"], type_resolver)
Expand Down
17 changes: 17 additions & 0 deletions spec/graphql/schema/field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -841,4 +841,21 @@ def example
res = HashDefautSchema.execute('query { example { implicitLookup explicitLookup } }').to_h
assert_equal({ "implicitLookup" => [], "explicitLookup" => [] }, res["data"]["example"])
end

module FieldConnectionTest
class SomeConnection < GraphQL::Schema::Object; end
class Connection < GraphQL::Schema::Object; end
end

it "Automatically detects connection, but can be overridden" do
field = GraphQL::Schema::Field.new(name: "blah", owner: nil, type: FieldConnectionTest::SomeConnection)
assert field.connection?
field = GraphQL::Schema::Field.new(name: "blah", owner: nil, type: FieldConnectionTest::SomeConnection, connection: false)
refute field.connection?

field = GraphQL::Schema::Field.new(name: "blah", owner: nil, type: FieldConnectionTest::Connection)
refute field.connection?
field = GraphQL::Schema::Field.new(name: "blah", owner: nil, type: FieldConnectionTest::Connection, connection: true)
assert field.connection?
end
end

0 comments on commit 1bafe88

Please sign in to comment.