summaryrefslogtreecommitdiff
path: root/app/graphql/loaders/iid_loader.rb
blob: e89031da0c29458de1121a2076d94cae89ee6b35 (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
class Loaders::IidLoader < Loaders::BaseLoader
  class << self
    def merge_request(obj, args, ctx)
      iid = args[:iid]
      promise = Loaders::FullPathLoader.project_by_full_path(args[:project])

      promise.then do |project|
        if project
          merge_request_by_project_and_iid(project.id, iid)
        else
          nil
        end
      end
    end

    def merge_request_by_project_and_iid(project_id, iid)
      self.for(MergeRequest, target_project_id: project_id.to_s).load(iid.to_s)
    end
  end

  attr_reader :model, :restrictions

  def initialize(model, restrictions = {})
    @model = model
    @restrictions = restrictions
  end

  def perform(keys)
    relation = model.where(iid: keys)
    relation = relation.where(restrictions) if restrictions.present?

    # IIDs are represented as the GraphQL `id` type, which is a string
    fulfill_all(relation, keys) { |instance| instance.iid.to_s }
  end
end