summaryrefslogtreecommitdiff
path: root/lib/gitlab/graphql/pagination/externally_paginated_array_connection.rb
blob: 12e047420bf54dafc0288cb053817affb2e3a5f2 (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
# frozen_string_literal: true

# Make a customized connection type
module Gitlab
  module Graphql
    module Pagination
      class ExternallyPaginatedArrayConnection < GraphQL::Pagination::ArrayConnection
        def start_cursor
          items.previous_cursor
        end

        def end_cursor
          items.next_cursor
        end

        def next_page?
          end_cursor.present?
        end

        def previous_page?
          start_cursor.present?
        end

        alias_method :has_next_page, :next_page?
        alias_method :has_previous_page, :previous_page?

        private

        def load_nodes
          @nodes ||= begin
            # As the pagination happens externally we just grab all the nodes
            limited_nodes = items

            limited_nodes = limited_nodes.first(first) if first
            limited_nodes = limited_nodes.last(last) if last

            limited_nodes
          end
        end
      end
    end
  end
end