Skip to content

Commit 1a21f66

Browse files
authored
Merge pull request rmosolgo#2349 from yask123/fix-array-bug
Fix incorrect hasNextPage value when the last cursor belongs to the last element in the array
2 parents b0927e7 + bb70bd8 commit 1a21f66

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

lib/graphql/relay/array_connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def has_next_page
1313
sliced_nodes.count > first
1414
elsif GraphQL::Relay::ConnectionType.bidirectional_pagination && before
1515
# The original array is longer than the `before` index
16-
index_from_cursor(before) < nodes.length
16+
index_from_cursor(before) < nodes.length + 1
1717
else
1818
false
1919
end

spec/integration/rails/graphql/relay/array_connection_spec.rb

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,35 +65,7 @@ def get_page_info(result, key = "bases")
6565
assert_equal("NQ", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
6666
end
6767

68-
it "provides bidirectional_pagination" do
69-
result = star_wars_query(query_string, "first" => 1)
70-
last_cursor = get_last_cursor(result)
71-
72-
# When going forwards, bi-directional pagination
73-
# returns `true` even for `hasPreviousPage`
74-
result = star_wars_query(query_string, "first" => 1, "after" => last_cursor)
75-
assert_equal(true, get_page_info(result, "ships")["hasNextPage"])
76-
assert_equal(false, get_page_info(result, "ships")["hasPreviousPage"])
77-
78-
result = with_bidirectional_pagination {
79-
star_wars_query(query_string, "first" => 3, "after" => last_cursor)
80-
}
81-
assert_equal(true, get_page_info(result, "ships")["hasNextPage"])
82-
assert_equal(true, get_page_info(result, "ships")["hasPreviousPage"])
83-
84-
# When going backwards, bi-directional pagination
85-
# returns true for `hasNextPage`
86-
last_cursor = get_last_cursor(result)
87-
result = star_wars_query(query_string, "last" => 1, "before" => last_cursor)
88-
assert_equal(false, get_page_info(result, "ships")["hasNextPage"])
89-
assert_equal(true, get_page_info(result, "ships")["hasPreviousPage"])
9068

91-
result = with_bidirectional_pagination {
92-
star_wars_query(query_string, "last" => 2, "before" => last_cursor)
93-
}
94-
assert_equal(true, get_page_info(result, "ships")["hasNextPage"])
95-
assert_equal(true, get_page_info(result, "ships")["hasPreviousPage"])
96-
end
9769

9870
it 'slices the result' do
9971
result = star_wars_query(query_string, "first" => 1)
@@ -286,5 +258,52 @@ def get_page_info(result)
286258
assert_equal(first_second_and_third_names, get_names(result))
287259
end
288260
end
261+
262+
describe "bidirectional pagination" do
263+
it "provides bidirectional_pagination" do
264+
result = star_wars_query(query_string, "first" => 1)
265+
last_cursor = get_last_cursor(result)
266+
267+
# When going forwards, bi-directional pagination
268+
# returns `true` even for `hasPreviousPage`
269+
result = star_wars_query(query_string, "first" => 1, "after" => last_cursor)
270+
assert_equal(true, get_page_info(result, "ships")["hasNextPage"])
271+
assert_equal(false, get_page_info(result, "ships")["hasPreviousPage"])
272+
273+
result = with_bidirectional_pagination {
274+
star_wars_query(query_string, "first" => 3, "after" => last_cursor)
275+
}
276+
assert_equal(true, get_page_info(result, "ships")["hasNextPage"])
277+
assert_equal(true, get_page_info(result, "ships")["hasPreviousPage"])
278+
279+
# When going backwards, bi-directional pagination
280+
# returns true for `hasNextPage`
281+
last_cursor = get_last_cursor(result)
282+
result = star_wars_query(query_string, "last" => 1, "before" => last_cursor)
283+
assert_equal(false, get_page_info(result, "ships")["hasNextPage"])
284+
assert_equal(true, get_page_info(result, "ships")["hasPreviousPage"])
285+
286+
result = with_bidirectional_pagination {
287+
star_wars_query(query_string, "last" => 2, "before" => last_cursor)
288+
}
289+
assert_equal(true, get_page_info(result, "ships")["hasNextPage"])
290+
assert_equal(true, get_page_info(result, "ships")["hasPreviousPage"])
291+
end
292+
293+
it "returns correct page info when the before cursor belongs to the last element in the array" do
294+
result = with_bidirectional_pagination{
295+
star_wars_query(query_string, "last" => 1)
296+
}
297+
298+
last_cursor = get_last_cursor(result)
299+
300+
result = with_bidirectional_pagination{
301+
star_wars_query(query_string, "before" => last_cursor, "last" => 1)
302+
}
303+
304+
assert_equal(true, get_page_info(result, "ships")["hasNextPage"])
305+
assert_equal(true, get_page_info(result, "ships")["hasPreviousPage"])
306+
end
307+
end
289308
end
290309
end

0 commit comments

Comments
 (0)