Skip to content

Commit ef34b49

Browse files
committed
Optimise RelationConnection.has_next_page with exists? for ActiveRecord and record.empty?
1 parent 6b768d4 commit ef34b49

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lib/graphql/relay/relation_connection.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,21 @@ def cursor_from_node(item)
2323
end
2424
end
2525

26+
def record_exists?(record)
27+
if(defined?(ActiveRecord::Relation) && record.is_a?(ActiveRecord::Relation))
28+
record.exists?
29+
else
30+
!record.empty?
31+
end
32+
end
33+
2634
def has_next_page
2735
if first
28-
paged_nodes.length >= first && nodes.offset(first).exists?
36+
if after
37+
paged_nodes.length >= first && record_exists?(nodes.offset(first + offset_from_cursor(after)))
38+
else
39+
paged_nodes.length >= first && record_exists?(nodes.offset(first))
40+
end
2941
elsif GraphQL::Relay::ConnectionType.bidirectional_pagination && last
3042
sliced_nodes_count >= last
3143
else

0 commit comments

Comments
 (0)