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

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

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Instance Method Details

#cursor_for(node) ⇒ Object

rubocop: enable Naming/PredicateName


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

def cursor_for(node)
  encoded_json_from_ordering(node)
end

#has_next_pageObject


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gitlab/graphql/pagination/keyset/connection.rb', line 59

def has_next_page
  strong_memoize(: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
      # 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
      relation_count(set_limit(sliced_nodes, limit_value + 1)) == limit_value + 1
    else
      false
    end
  end
end

#has_previous_pageObject


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

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

#nodesObject


95
96
97
98
99
100
101
# File 'lib/gitlab/graphql/pagination/keyset/connection.rb', line 95

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
end

#sliced_nodesObject


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

def sliced_nodes
  @sliced_nodes ||=
    begin
      OrderInfo.validate_ordering(ordered_items, order_list) unless loaded?(ordered_items)

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

      sliced
    end
end