Class: Gitlab::Graphql::Loaders::BatchModelLoader
- Inherits:
-
Object
- Object
- Gitlab::Graphql::Loaders::BatchModelLoader
- Defined in:
- lib/gitlab/graphql/loaders/batch_model_loader.rb
Instance Attribute Summary collapse
-
#default_value ⇒ Object
readonly
Returns the value of attribute default_value.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#preloads ⇒ Object
readonly
Returns the value of attribute preloads.
Instance Method Summary collapse
-
#find ⇒ Object
rubocop: disable CodeReuse/ActiveRecord.
-
#initialize(model_class, model_id, preloads = nil, default_value: nil) ⇒ BatchModelLoader
constructor
A new instance of BatchModelLoader.
Constructor Details
#initialize(model_class, model_id, preloads = nil, default_value: nil) ⇒ BatchModelLoader
9 10 11 12 13 14 |
# File 'lib/gitlab/graphql/loaders/batch_model_loader.rb', line 9 def initialize(model_class, model_id, preloads = nil, default_value: nil) @model_class = model_class @model_id = model_id @preloads = preloads || [] @default_value = default_value end |
Instance Attribute Details
#default_value ⇒ Object (readonly)
Returns the value of attribute default_value.
7 8 9 |
# File 'lib/gitlab/graphql/loaders/batch_model_loader.rb', line 7 def default_value @default_value end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
7 8 9 |
# File 'lib/gitlab/graphql/loaders/batch_model_loader.rb', line 7 def model_class @model_class end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
7 8 9 |
# File 'lib/gitlab/graphql/loaders/batch_model_loader.rb', line 7 def model_id @model_id end |
#preloads ⇒ Object (readonly)
Returns the value of attribute preloads.
7 8 9 |
# File 'lib/gitlab/graphql/loaders/batch_model_loader.rb', line 7 def preloads @preloads end |
Instance Method Details
#find ⇒ Object
rubocop: disable CodeReuse/ActiveRecord
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/gitlab/graphql/loaders/batch_model_loader.rb', line 17 def find BatchLoader::GraphQL.for([model_id.to_i, preloads]).batch(key: model_class) do |for_params, loader, args| model = args[:key] keys_by_id = for_params.group_by(&:first) ids = for_params.map(&:first) preloads = for_params.flat_map(&:second).uniq results = model.where(id: ids) results = results.preload(*preloads) unless preloads.empty? results = results.index_by(&:id) keys_by_id.each do |id, keys| keys.each { |k| loader.call(k, results[id] || default_value) } end end end |