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

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

module Gitlab
  module Graphql
    module Loaders
      class BatchModelLoader
        attr_reader :model_class, :model_id

        def initialize(model_class, model_id)
          @model_class, @model_id = model_class, model_id
        end

        # rubocop: disable CodeReuse/ActiveRecord
        def find
          BatchLoader::GraphQL.for(model_id.to_i).batch(key: model_class) do |ids, loader, args|
            model = args[:key]
            results = model.where(id: ids)

            results.each { |record| loader.call(record.id, record) }
          end
        end
        # rubocop: enable CodeReuse/ActiveRecord
      end
    end
  end
end