Method: GraphQL::Dataloader#merge_records

Defined in:
lib/graphql/dataloader.rb

#merge_records(records, index_by: :id) ⇒ void

This method returns an undefined value.

Pre-warm the Dataloader cache with ActiveRecord objects which were loaded elsewhere. These will be used by ActiveRecordSource, ActiveRecordAssociationSource and their helper methods, dataload_record and dataload_association.

Parameters:

  • records (Array<ActiveRecord::Base>)

    Already-loaded records to warm the cache with

  • index_by (Symbol) (defaults to: :id)

    The attribute to use as the cache key. (Should match find_by: when using ActiveRecordSource)



265
266
267
268
269
270
271
272
273
# File 'lib/graphql/dataloader.rb', line 265

def merge_records(records, index_by: :id)
  records_by_class = Hash.new { |h, k| h[k] = {} }
  records.each do |r|
    records_by_class[r.class][r.public_send(index_by)] = r
  end
  records_by_class.each do |r_class, records|
    with(ActiveRecordSource, r_class).merge(records)
  end
end