summaryrefslogtreecommitdiff
path: root/lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb')
-rw-r--r--lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb b/lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb
index f1b74999897..5a9d21e7469 100644
--- a/lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb
+++ b/lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb
@@ -4,11 +4,42 @@ module Gitlab
module Graphql
module Pagination
module Keyset
+ # https://gitlab.com/gitlab-org/gitlab/-/issues/334973
# Use the generic keyset implementation if the given ActiveRecord scope supports it.
# Note: this module is temporary, at some point it will be merged with Keyset::Connection
module GenericKeysetPagination
extend ActiveSupport::Concern
+ # rubocop: disable Naming/PredicateName
+ # rubocop: disable CodeReuse/ActiveRecord
+ def has_next_page
+ return super unless Gitlab::Pagination::Keyset::Order.keyset_aware?(items)
+
+ strong_memoize(:generic_keyset_pagination_has_next_page) do
+ if before
+ # If `before` is specified, that points to a specific record,
+ # even if it's the last one. Since we're asking for `before`,
+ # then the specific record we're pointing to is in the
+ # next page
+ true
+ elsif first
+ case sliced_nodes
+ when Array
+ sliced_nodes.size > limit_value
+ else
+ # If we count the number of requested items plus one (`limit_value + 1`),
+ # then if we get `limit_value + 1` then we know there is a next page
+ sliced_nodes.limit(1).offset(limit_value).exists?
+ # replacing relation count
+ # relation_count(set_limit(sliced_nodes, limit_value + 1)) == limit_value + 1
+ end
+ else
+ false
+ end
+ end
+ end
+
+ # rubocop: enable CodeReuse/ActiveRecord
def ordered_items
raise ArgumentError, 'Relation must have a primary key' unless items.primary_key.present?