Class: HQ::GraphQL::AssociationLoader

Inherits:
GraphQL::Batch::Loader
  • Object
show all
Defined in:
lib/hq/graphql/association_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, association_name) ⇒ AssociationLoader

Returns a new instance of AssociationLoader.



6
7
8
9
10
# File 'lib/hq/graphql/association_loader.rb', line 6

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

Instance Method Details

#cache_key(record) ⇒ Object

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



19
20
21
# File 'lib/hq/graphql/association_loader.rb', line 19

def cache_key(record)
  record.object_id
end

#load(record) ⇒ Object

Raises:

  • (TypeError)


12
13
14
15
16
# File 'lib/hq/graphql/association_loader.rb', line 12

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

#perform(records) ⇒ Object



23
24
25
26
# File 'lib/hq/graphql/association_loader.rb', line 23

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