Module: Gitlab::Graphql::Pagination::Keyset::GenericKeysetPagination

Extended by:
ActiveSupport::Concern
Included in:
Connection
Defined in:
lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#cursor_for(node) ⇒ Object


43
44
45
46
47
48
# File 'lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb', line 43

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

#has_next_pageObject

rubocop: disable Naming/PredicateName rubocop: disable CodeReuse/ActiveRecord


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb', line 15

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
      true
    elsif first
      case sliced_nodes
      when Array
        sliced_nodes.size > limit_value
      else
        sliced_nodes.limit(1).offset(limit_value).exists?
      end
    else
      false
    end
  end
end

#itemsObject


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb', line 69

def items
  original_items = super
  return original_items if Feature.disabled?(:new_graphql_keyset_pagination) || 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)

    if success
      rebuilt_items_with_keyset_order
    else
      if original_items.is_a?(ActiveRecord::Relation)
        old_keyset_pagination_usage.increment({ model: original_items.model.to_s })
      end

      original_items
    end
  end
end

#old_keyset_pagination_usageObject


88
89
90
91
92
93
# File 'lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb', line 88

def old_keyset_pagination_usage
  @old_keyset_pagination_usage ||= Gitlab::Metrics.counter(
    :old_keyset_pagination_usage,
    'The number of times the old keyset pagination code was used'
  )
end

#ordered_itemsObject

rubocop: enable CodeReuse/ActiveRecord

Raises:

  • (ArgumentError)

35
36
37
38
39
40
41
# File 'lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb', line 35

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

#slice_nodes(sliced, encoded_cursor, before_or_after) ⇒ Object


50
51
52
53
54
55
56
57
58
# File 'lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb', line 50

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

#sliced_nodesObject


60
61
62
63
64
65
66
67
# File 'lib/gitlab/graphql/pagination/keyset/generic_keyset_pagination.rb', line 60

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