Skip to content

Commit 613b082

Browse files
committed
Fix RelationConnections when relation already has limit
1 parent f4d99f6 commit 613b082

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/graphql/pagination/relation_connection.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ def calculate_sliced_nodes_parameters
117117
return
118118
else
119119
next_offset = relation_offset(items) || 0
120+
relation_limit = relation_limit(items)
121+
120122
if after_offset
121123
next_offset += after_offset
122124
end

spec/graphql/pagination/active_record_relation_connection_spec.rb

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ def total_count
2727
total_count_connection_class: RelationConnectionWithTotalCount,
2828
get_items: -> {
2929
if Food.respond_to?(:scoped)
30-
Food.scoped # Rails 3-friendly version of .all
30+
Food.scoped.limit(limit) # Rails 3-friendly version of .all
3131
else
32-
Food.all
32+
Food.all.limit(limit)
3333
end
3434
}
3535
)
3636
}
3737

38+
let(:limit) { nil }
39+
3840
include ConnectionAssertions
3941

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

67+
describe 'with application-provided limit, which is smaller than the max_page_size' do
68+
let(:limit) { 1 }
69+
70+
it "maintains an application-provided limit" do
71+
results = schema.execute("{
72+
limitedItems {
73+
nodes { name }
74+
}
75+
}")
76+
assert_equal ["Avocado"], results["data"]["limitedItems"]["nodes"].map { |n| n["name"] }
77+
end
78+
end
79+
80+
describe 'with application-provided limit, which is larger than the max_page_size' do
81+
let(:limit) { 3 }
82+
83+
it "applies a field-level max-page-size configuration" do
84+
results = schema.execute("{
85+
limitedItems {
86+
nodes { name }
87+
}
88+
}")
89+
assert_equal ["Avocado", "Beet"], results["data"]["limitedItems"]["nodes"].map { |n| n["name"] }
90+
end
91+
end
92+
93+
6594
it "doesn't run pageInfo queries when not necessary" do
6695
results = nil
6796
log = with_active_record_log do

0 commit comments

Comments
 (0)