Skip to content

Commit

Permalink
Ensure hasNextPage Field is false On Last Page
Browse files Browse the repository at this point in the history
Remove the overridden ActiveRecordRelationConnection#relation_larger_than method.
The overridden method was flawed as demonstrated by the test case. The
parent class implementation appears to return the correct result.
  • Loading branch information
calebpowell committed Feb 17, 2023
1 parent 259fa7b commit 4fc149c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
8 changes: 0 additions & 8 deletions lib/graphql/pagination/active_record_relation_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ module Pagination
class ActiveRecordRelationConnection < Pagination::RelationConnection
private

def relation_larger_than(relation, initial_offset, size)
if already_loaded?(relation)
(relation.size + initial_offset) > size
else
set_offset(sliced_nodes, initial_offset + size).exists?
end
end

def relation_count(relation)
int_or_hash = if already_loaded?(relation)
relation.size
Expand Down
11 changes: 9 additions & 2 deletions spec/integration/rails/graphql/relay/relation_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ def get_last_cursor(result)
assert_equal("Mw", get_last_cursor(result))
end

it 'returns the correct hasNextPage value' do
first_page_result = star_wars_query(query_string, { "first" => 2})
assert_equal(true, get_page_info(first_page_result)["hasNextPage"])

result = star_wars_query(query_string, { "first" => 2, after: get_page_info(first_page_result)["endCursor"] })
assert_equal(false, get_page_info(result)["hasNextPage"])
end

it "uses unscope(:order) count(*) when the relation has some complicated SQL" do
query_s = <<-GRAPHQL
query getShips($first: Int, $after: String, $complexOrder: Boolean){
Expand Down Expand Up @@ -93,10 +101,9 @@ def get_last_cursor(result)
assert_equal(true, conn["pageInfo"]["hasNextPage"])

log_entries = log.split("\n")
assert_equal 2, log_entries.size, "It ran 2 sql queries"
assert_equal 1, log_entries.size, "It should run 1 sql query"
edges_query, has_next_page_query = log_entries
assert_includes edges_query, "ORDER BY bases.name", "The query for edges _is_ ordered"
refute_includes has_next_page_query, "ORDER BY bases.name", "The count query **does not** have an order"
end

it 'provides custom fields on the connection type' do
Expand Down
5 changes: 4 additions & 1 deletion spec/support/star_wars/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ def bases(name_includes: nil, complex_order: nil)
if complex_order
all_bases = all_bases.order("bases.name DESC")
end
all_bases

# Emulates ActiveRecord::Base.connected_to(role: :reading) do
# https://github.com/rails/rails/blob/d18fc329993df5a583ef721330cffb248ef9a213/activerecord/lib/active_record/connection_handling.rb#L355
all_bases.load
end

field :bases_clone, BaseConnection
Expand Down

0 comments on commit 4fc149c

Please sign in to comment.