Class: Gitlab::Graphql::Pagination::Keyset::Connection

Inherits:
GraphQL::Pagination::ActiveRecordRelationConnection
  • Object
show all
Includes:
ConnectionCollectionMethods, ConnectionRedaction, Utils::StrongMemoize
Defined in:
lib/gitlab/graphql/pagination/keyset/connection.rb

Instance Method Summary collapse

Instance Method Details

#cursor_for(node) ⇒ Object

rubocop: enable Naming/PredicateName



70
71
72
73
# File 'lib/gitlab/graphql/pagination/keyset/connection.rb', line 70

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

#has_next_pageObject



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

def has_next_page
  strong_memoize(:has_next_page) do
    if before
      true
    elsif first
      limited_nodes.size > limit_value
    else
      false
    end
  end
end

#has_previous_pageObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gitlab/graphql/pagination/keyset/connection.rb', line 35

def has_previous_page
  strong_memoize(:has_previous_page) do
    if after
      # If `after` is specified, that points to a specific record,
      # even if it's the first one.  Since we're asking for `after`,
      # then the specific record we're pointing to is in the
      # previous page
      true
    elsif last
      limited_nodes
      !!@has_previous_page
    else
      # Key thing to remember.  When `before` is specified (and no `last`),
      # the spec says return _all_ edges minus anything after the `before`.
      # Which means the returned list starts at the very first record.
      # Then the max_page kicks in, and returns the first max_page items.
      # Because of this, `has_previous_page` will be false
      false
    end
  end
end

#itemsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gitlab/graphql/pagination/keyset/connection.rb', line 90

def items
  original_items = super
  return original_items if Gitlab::Pagination::Keyset::Order.keyset_aware?(original_items)

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

    raise(Gitlab::Pagination::Keyset::UnsupportedScopeOrder) unless success

    rebuilt_items_with_keyset_order
  end
end

#nodesObject



82
83
84
85
86
87
88
# File 'lib/gitlab/graphql/pagination/keyset/connection.rb', line 82

def nodes
  # These are the nodes that will be loaded into memory for rendering
  # So we're ok loading them into memory here as that's bound to happen
  # anyway. Having them ready means we can modify the result while
  # rendering the fields.
  @nodes ||= limited_nodes.to_a.take(limit_value) # rubocop: disable CodeReuse/ActiveRecord
end

#sliced_nodesObject



75
76
77
78
79
80
# File 'lib/gitlab/graphql/pagination/keyset/connection.rb', line 75

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