debian-mirror-gitlab/lib/gitlab/graphql/loaders/batch_model_loader.rb
2021-01-30 21:13:34 +05:30

26 lines
683 B
Ruby

# 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