Class: Gitlab::Graphql::Loaders::BatchModelLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/graphql/loaders/batch_model_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, model_id) ⇒ BatchModelLoader

Returns a new instance of BatchModelLoader.


9
10
11
12
# File 'lib/gitlab/graphql/loaders/batch_model_loader.rb', line 9

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

Instance Attribute Details

#model_classObject (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_idObject (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

Instance Method Details

#findObject

rubocop: disable CodeReuse/ActiveRecord


15
16
17
18
19
20
21
22
# File 'lib/gitlab/graphql/loaders/batch_model_loader.rb', line 15

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