Welcome to mirror list, hosted at ThFree Co, Russian Federation.

generic_keyset_pagination.rb « keyset « pagination « graphql « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15f95edd318c74fb932cc1a814ee114210d2c315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

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?

            return super unless Gitlab::Pagination::Keyset::Order.keyset_aware?(items)

            items
          end

          def cursor_for(node)
            return super unless Gitlab::Pagination::Keyset::Order.keyset_aware?(items)

            order = Gitlab::Pagination::Keyset::Order.extract_keyset_order_object(items)
            encode(order.cursor_attributes_for_node(node).to_json)
          end

          def slice_nodes(sliced, encoded_cursor, before_or_after)
            return super unless Gitlab::Pagination::Keyset::Order.keyset_aware?(sliced)

            order = Gitlab::Pagination::Keyset::Order.extract_keyset_order_object(sliced)
            order = order.reversed_order if before_or_after == :before

            decoded_cursor = ordering_from_encoded_json(encoded_cursor)
            order.apply_cursor_conditions(sliced, decoded_cursor)
          end

          def sliced_nodes
            return super unless Gitlab::Pagination::Keyset::Order.keyset_aware?(items)

            sliced = ordered_items
            sliced = slice_nodes(sliced, before, :before) if before.present?
            sliced = slice_nodes(sliced, after, :after) if after.present?
            sliced
          end

          def items
            original_items = super
            return original_items if Feature.disabled?(:new_graphql_keyset_pagination, default_enabled: :yaml) || Gitlab::Pagination::Keyset::Order.keyset_aware?(original_items)

            strong_memoize(:generic_keyset_pagination_items) do
              rebuilt_items_with_keyset_order, success = Gitlab::Pagination::Keyset::SimpleOrderBuilder.build(original_items)

              success ? rebuilt_items_with_keyset_order : original_items
            end
          end
        end
      end
    end
  end
end