2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
module Loaders
|
|
|
|
class BatchModelLoader
|
|
|
|
attr_reader :model_class, :model_id
|
|
|
|
|
|
|
|
def initialize(model_class, model_id)
|
2021-04-29 21:17:54 +05:30
|
|
|
@model_class = model_class
|
|
|
|
@model_id = model_id
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def find
|
2021-01-29 00:20:46 +05:30
|
|
|
BatchLoader::GraphQL.for(model_id.to_i).batch(key: model_class) do |ids, loader, args|
|
|
|
|
model = args[:key]
|
|
|
|
results = model.where(id: ids)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
results.each { |record| loader.call(record.id, record) }
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|