Skip to content

Commit

Permalink
Fix RelationConnections when relation already has limit
Browse files Browse the repository at this point in the history
  • Loading branch information
RongRongTeng committed Jun 19, 2022
1 parent f4d99f6 commit 613b082
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/graphql/pagination/relation_connection.rb
Expand Up @@ -117,6 +117,8 @@ def calculate_sliced_nodes_parameters
return
else
next_offset = relation_offset(items) || 0
relation_limit = relation_limit(items)

if after_offset
next_offset += after_offset
end
Expand Down
33 changes: 31 additions & 2 deletions spec/graphql/pagination/active_record_relation_connection_spec.rb
Expand Up @@ -27,14 +27,16 @@ def total_count
total_count_connection_class: RelationConnectionWithTotalCount,
get_items: -> {
if Food.respond_to?(:scoped)
Food.scoped # Rails 3-friendly version of .all
Food.scoped.limit(limit) # Rails 3-friendly version of .all
else
Food.all
Food.all.limit(limit)
end
}
)
}

let(:limit) { nil }

include ConnectionAssertions

it "maintains an application-provided offset" do
Expand Down Expand Up @@ -62,6 +64,33 @@ def total_count
assert_equal ["Cucumber", "Dill"], results["data"]["offsetItems"]["nodes"].map { |n| n["name"] }
end

describe 'with application-provided limit, which is smaller than the max_page_size' do
let(:limit) { 1 }

it "maintains an application-provided limit" do
results = schema.execute("{
limitedItems {
nodes { name }
}
}")
assert_equal ["Avocado"], results["data"]["limitedItems"]["nodes"].map { |n| n["name"] }
end
end

describe 'with application-provided limit, which is larger than the max_page_size' do
let(:limit) { 3 }

it "applies a field-level max-page-size configuration" do
results = schema.execute("{
limitedItems {
nodes { name }
}
}")
assert_equal ["Avocado", "Beet"], results["data"]["limitedItems"]["nodes"].map { |n| n["name"] }
end
end


it "doesn't run pageInfo queries when not necessary" do
results = nil
log = with_active_record_log do
Expand Down

0 comments on commit 613b082

Please sign in to comment.