Skip to content

Commit

Permalink
Merge pull request rmosolgo#4349 from calebpowell/calebpowell/fix-has…
Browse files Browse the repository at this point in the history
…NextPage-bug

Ensure correct hasNextPage Value When Relation is Already Loaded
  • Loading branch information
rmosolgo authored Feb 20, 2023
2 parents 2622f1f + c0b0978 commit 532bd2f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 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
13 changes: 10 additions & 3 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"
edges_query, has_next_page_query = log_entries
assert_equal 1, log_entries.size, "It should run 1 sql query"
edges_query, = log_entries.first
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 532bd2f

Please sign in to comment.