diff options
author | Filipa Lacerda <filipa@gitlab.com> | 2018-11-27 20:58:27 +0300 |
---|---|---|
committer | Filipa Lacerda <filipa@gitlab.com> | 2018-11-27 20:58:27 +0300 |
commit | a99f342b4231659d39b9a145acc3652f3de3bce8 (patch) | |
tree | 096b376fac6ccd6519f1e60720ba67d56e1bef75 /lib | |
parent | d0b529d17a507709467cc75c607c19d465f9852d (diff) | |
parent | 50e21a89a0009813b9f090288b22c64c5cefbd58 (diff) |
Merge branch 'issuable-suggestions' into 'master'
Suggest issues when typing title
Closes #22071
See merge request gitlab-org/gitlab-ce!22866
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/graphql/loaders/batch_model_loader.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/gitlab/graphql/loaders/batch_model_loader.rb b/lib/gitlab/graphql/loaders/batch_model_loader.rb new file mode 100644 index 00000000000..5a0099dc6b1 --- /dev/null +++ b/lib/gitlab/graphql/loaders/batch_model_loader.rb @@ -0,0 +1,29 @@ +# 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.for({ model: model_class, id: model_id }).batch do |loader_info, loader| + per_model = loader_info.group_by { |info| info[:model] } + per_model.each do |model, info| + ids = info.map { |i| i[:id] } + results = model.where(id: ids) + + results.each { |record| loader.call({ model: model, id: record.id }, record) } + end + end + end + # rubocop: enable CodeReuse/ActiveRecord + end + end + end +end |