Class: GraphQL::Preload::Loader

Inherits:
Batch::Loader
  • Object
show all
Defined in:
lib/graphql/preload/loader.rb

Overview

Preloads ActiveRecord::Associations when called from the Preload::Instrument

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, association) ⇒ Loader

Returns a new instance of Loader.



11
12
13
14
15
16
# File 'lib/graphql/preload/loader.rb', line 11

def initialize(model, association)
  @association = association
  @model = model

  validate_association
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



5
6
7
# File 'lib/graphql/preload/loader.rb', line 5

def association
  @association
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/graphql/preload/loader.rb', line 5

def model
  @model
end

Instance Method Details

#cache_key(record) ⇒ Object



7
8
9
# File 'lib/graphql/preload/loader.rb', line 7

def cache_key(record)
  record.object_id
end

#load(record) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/graphql/preload/loader.rb', line 18

def load(record)
  unless record.is_a?(model)
    raise TypeError, "Loader for #{model} can't load associations for #{record.class} objects"
  end

  return Promise.resolve(record) if association_loaded?(record)
  super
end

#perform(records) ⇒ Object



27
28
29
30
# File 'lib/graphql/preload/loader.rb', line 27

def perform(records)
  preload_association(records)
  records.each { |record| fulfill(record, record) }
end