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

keyset_connection.rb « connections « graphql « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 715963a44c1d6da8be331193920d939f9d565566 (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
# frozen_string_literal: true

module Gitlab
  module Graphql
    module Connections
      class KeysetConnection < GraphQL::Relay::BaseConnection
        def cursor_from_node(node)
          encode(node[order_field].to_s)
        end

        # rubocop: disable CodeReuse/ActiveRecord
        def sliced_nodes
          @sliced_nodes ||=
            begin
              sliced = nodes

              sliced = sliced.where(before_slice) if before.present?
              sliced = sliced.where(after_slice) if after.present?

              sliced
            end
        end
        # rubocop: enable CodeReuse/ActiveRecord

        def paged_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.
          @paged_nodes ||= load_paged_nodes.to_a
        end

        private

        def load_paged_nodes
          if first && last
            raise Gitlab::Graphql::Errors::ArgumentError.new("Can only provide either `first` or `last`, not both")
          end

          if last
            sliced_nodes.last(limit_value)
          else
            sliced_nodes.limit(limit_value) # rubocop: disable CodeReuse/ActiveRecord
          end
        end

        def before_slice
          if sort_direction == :asc
            table[order_field].lt(decode(before))
          else
            table[order_field].gt(decode(before))
          end
        end

        def after_slice
          if sort_direction == :asc
            table[order_field].gt(decode(after))
          else
            table[order_field].lt(decode(after))
          end
        end

        def limit_value
          @limit_value ||= [first, last, max_page_size].compact.min
        end

        def table
          nodes.arel_table
        end

        def order_info
          @order_info ||= nodes.order_values.first
        end

        def order_field
          @order_field ||= order_info&.expr&.name || nodes.primary_key
        end

        def sort_direction
          @order_direction ||= order_info&.direction || :desc
        end
      end
    end
  end
end