Skip to content

Commit

Permalink
Merge pull request #4263 from rmosolgo/fix-double-scoping
Browse files Browse the repository at this point in the history
don't re-scope nodes or edges
  • Loading branch information
rmosolgo committed Dec 7, 2022
2 parents c9ae523 + 2272ab2 commit 32e94c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/graphql/types/relay/connection_behaviors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def edge_type(edge_type_class, edge_class: GraphQL::Pagination::Connection::Edge
null: edges_nullable,
description: "A list of edges.",
connection: false,
# Assume that the connection was scoped before this step:
scope: false,
}

if field_options
Expand Down Expand Up @@ -135,6 +137,8 @@ def define_nodes_field(nullable, field_options: nil)
null: nullable,
description: "A list of nodes.",
connection: false,
# Assume that the connection was scoped before this step:
scope: false,
}
if field_options
base_field_options.merge!(field_options)
Expand Down
6 changes: 5 additions & 1 deletion spec/graphql/schema/member/scoped_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class BaseObject < GraphQL::Schema::Object

class Item < BaseObject
def self.scope_items(items, context)
context[:scope_calls] ||= 0
context[:scope_calls] += 1
if context[:french]
items.select { |i| i.name == "Trombone" }
elsif context[:english]
Expand Down Expand Up @@ -138,7 +140,7 @@ def get_item_names_with_context(ctx, field_name: "items")
assert_equal ["Trombone"], get_item_names_with_context({}, field_name: "frenchItems")
end

it "is called for connection fields" do
it "is called once for connection fields" do
query_str = "
{
itemsConnection {
Expand All @@ -153,6 +155,7 @@ def get_item_names_with_context(ctx, field_name: "items")
res = ScopeSchema.execute(query_str, context: {english: true})
names = res["data"]["itemsConnection"]["edges"].map { |e| e["node"]["name"] }
assert_equal ["Paperclip"], names
assert_equal 1, res.context[:scope_calls]

query_str = "
{
Expand All @@ -166,6 +169,7 @@ def get_item_names_with_context(ctx, field_name: "items")
res = ScopeSchema.execute(query_str, context: {english: true})
names = res["data"]["itemsConnection"]["nodes"].map { |e| e["name"] }
assert_equal ["Paperclip"], names
assert_equal 1, res.context[:scope_calls]
end

it "works for lazy connection values" do
Expand Down

0 comments on commit 32e94c1

Please sign in to comment.