summaryrefslogtreecommitdiff
path: root/app/graphql/resolvers/base_resolver.rb
blob: 7daff68c069a107f88b1a94f82730e22f63b28cd (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
86
87
# frozen_string_literal: true

module Resolvers
  class BaseResolver < GraphQL::Schema::Resolver
    extend ::Gitlab::Utils::Override
    include ::Gitlab::Utils::StrongMemoize

    def self.single
      @single ||= Class.new(self) do
        def ready?(**args)
          ready, early_return = super
          [ready, select_result(early_return)]
        end

        def resolve(**args)
          select_result(super)
        end

        def single?
          true
        end

        def select_result(results)
          results&.first
        end
      end
    end

    def self.last
      @last ||= Class.new(self.single) do
        def select_result(results)
          results&.last
        end
      end
    end

    def self.complexity
      0
    end

    def self.resolver_complexity(args, child_complexity:)
      complexity = 1
      complexity += 1 if args[:sort]
      complexity += 5 if args[:search]

      complexity
    end

    def self.complexity_multiplier(args)
      # When fetching many items, additional complexity is added to the field
      # depending on how many items is fetched. For each item we add 1% of the
      # original complexity - this means that loading 100 items (our default
      # maxp_age_size limit) doubles the original complexity.
      #
      # Complexity is not increased when searching by specific ID(s), because
      # complexity difference is minimal in this case.
      [args[:iid], args[:iids]].any? ? 0 : 0.01
    end

    override :object
    def object
      super.tap do |obj|
        # If the field this resolver is used in is wrapped in a presenter, unwrap its subject
        break obj.subject if obj.is_a?(Gitlab::View::Presenter::Base)
      end
    end

    def synchronized_object
      strong_memoize(:synchronized_object) do
        case object
        when BatchLoader::GraphQL
          object.sync
        else
          object
        end
      end
    end

    def single?
      false
    end

    def current_user
      context[:current_user]
    end
  end
end