Skip to content

Commit 17919f3

Browse files
committed
Fix hasNextPage when max_page_size is set
1 parent 1479664 commit 17919f3

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

lib/graphql/pagination/array_connection.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def index_from_cursor(cursor)
3535
def load_nodes
3636
@nodes ||= begin
3737
sliced_nodes = if before && after
38-
end_idx = index_from_cursor(before)-1
38+
end_idx = index_from_cursor(before) - 2
3939
end_idx < 0 ? [] : items[index_from_cursor(after)..end_idx] || []
4040
elsif before
41-
end_idx = index_from_cursor(before)-2
41+
end_idx = index_from_cursor(before) - 2
4242
end_idx < 0 ? [] : items[0..end_idx] || []
4343
elsif after
4444
items[index_from_cursor(after)..-1] || []
@@ -56,7 +56,7 @@ def load_nodes
5656
false
5757
end
5858

59-
@has_next_page = if first
59+
@has_next_page = if first_value && first
6060
# There are more items after these items
6161
sliced_nodes.count > first
6262
elsif before

lib/graphql/pagination/relation_connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def has_next_page
3131
if @has_next_page.nil?
3232
@has_next_page = if before_offset && before_offset > 0
3333
true
34-
elsif first
34+
elsif first && first_value
3535
if @nodes && @nodes.count < first
3636
false
3737
else

spec/support/connection_assertions.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ def assert_names(expected_names, result)
243243
refute get_page_info(res3, "hasPreviousPage")
244244
end
245245

246+
it "returns empty lists for `after: 1` and `before: 2`" do
247+
res = exec_query(query_str, first: 2)
248+
assert_names(["Avocado", "Beet"], res)
249+
after_cursor = get_page_info(res, "startCursor")
250+
before_cursor = get_page_info(res, "endCursor")
251+
252+
res = exec_query(query_str, after: after_cursor, before: before_cursor)
253+
assert_equal true, get_page_info(res, "hasNextPage")
254+
assert_equal true, get_page_info(res, "hasPreviousPage")
255+
assert_names [], res
256+
end
257+
246258
it "handles out-of-bounds cursors" do
247259
# It treats negative cursors like zero
248260
bogus_negative_cursor = NonceEnabledEncoder.encode("-10")
@@ -300,7 +312,7 @@ def assert_names(expected_names, result)
300312
res = exec_query(query_str, {})
301313
# Neither first nor last was provided, so default_page_size was applied.
302314
assert_names(["Avocado", "Beet", "Cucumber", "Dill"], res)
303-
assert_equal true, get_page_info(res, "hasNextPage")
315+
assert_equal false, get_page_info(res, "hasNextPage")
304316
assert_equal false, get_page_info(res, "hasPreviousPage")
305317
end
306318

0 commit comments

Comments
 (0)