Class: GraphQL::AssociationBatchResolver::AssociationLoader

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, association_name, options = {}) ⇒ AssociationLoader

Returns a new instance of AssociationLoader.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 17

def initialize(model, association_name, options = {})
  @model = model
  @association_name = association_name
  @options = options
  validate
  @model_primary_key = model.primary_key
  association = @model.reflect_on_association(association_name)
  @is_collection = association.collection?
  @association_model = association.klass
  @association_primary_key = @association_model.primary_key
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



10
11
12
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 10

def args
  @args
end

#association_modelObject (readonly)

Returns the value of attribute association_model.



8
9
10
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 8

def association_model
  @association_model
end

#association_nameObject (readonly)

Returns the value of attribute association_name.



8
9
10
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 8

def association_name
  @association_name
end

#association_primary_keyObject (readonly)

Returns the value of attribute association_primary_key.



8
9
10
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 8

def association_primary_key
  @association_primary_key
end

#bucketObject

Returns the value of attribute bucket.



10
11
12
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 10

def bucket
  @bucket
end

#contextObject

Returns the value of attribute context.



10
11
12
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 10

def context
  @context
end

#is_collectionObject (readonly)

Returns the value of attribute is_collection.



8
9
10
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 8

def is_collection
  @is_collection
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 8

def model
  @model
end

#model_primary_keyObject (readonly)

Returns the value of attribute model_primary_key.



8
9
10
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 8

def model_primary_key
  @model_primary_key
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 8

def options
  @options
end

#scopeObject

Returns the value of attribute scope.



10
11
12
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 10

def scope
  @scope
end

Class Method Details

.validate(model, association_name, options = {}) ⇒ Object



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

def self.validate(model, association_name, options = {})
  new(model, association_name, options)
  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



36
37
38
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 36

def cache_key(record)
  record.object_id
end

#load(record) ⇒ Object

Raises:

  • (TypeError)


29
30
31
32
33
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 29

def load(record)
  raise TypeError, "#{model} loader can't load association for #{record.class}" unless record.is_a?(model)

  super
end

#perform(records) ⇒ Object



40
41
42
43
# File 'lib/graphql/association_batch_resolver/association_loader.rb', line 40

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