Class: Ibrain::Loaders::AssociationLoader

Inherits:
GraphQL::Batch::Loader
  • Object
show all
Defined in:
app/graphql/ibrain/loaders/association_loader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, association_name) ⇒ AssociationLoader

Returns a new instance of AssociationLoader.



13
14
15
16
17
18
# File 'app/graphql/ibrain/loaders/association_loader.rb', line 13

def initialize(model, association_name)
  super()
  @model = model
  @association_name = association_name
  validate
end

Class Method Details

.validate(model, association_name) ⇒ Object



8
9
10
11
# File 'app/graphql/ibrain/loaders/association_loader.rb', line 8

def self.validate(model, association_name)
  new(model, association_name)
  nil
end

Instance Method Details

#cache_key(record) ⇒ Object

We want to load the associations on all records, even if they have the same id



31
32
33
# File 'app/graphql/ibrain/loaders/association_loader.rb', line 31

def cache_key(record)
  record.object_id
end

#load(record) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'app/graphql/ibrain/loaders/association_loader.rb', line 20

def load(record)
  unless record.is_a?(@model)
    raise TypeError,
          "#{@model} loader can't load association for #{record.class}"
  end
  return Promise.resolve(read_association(record)) if association_loaded?(record)

  super
end

#perform(records) ⇒ Object



35
36
37
38
# File 'app/graphql/ibrain/loaders/association_loader.rb', line 35

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