Class: GraphQL::AssociationBatchResolver::AssociationResolver

Inherits:
Schema::Resolver
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/graphql/association_batch_resolver/association_resolver.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, loader_class: GraphQL::AssociationBatchResolver.configuration.loader, **keywargs, &block) ⇒ AssociationResolver

Returns a new instance of AssociationResolver.



34
35
36
37
38
39
40
41
# File 'lib/graphql/association_batch_resolver/association_resolver.rb', line 34

def initialize(*args, loader_class: GraphQL::AssociationBatchResolver.configuration.loader, **keywargs, &block)
  super(*args, **keywargs, &block)
  initialization_arguments = [model, association]
  initialization_arguments << options if options

  @loader = loader_class.for(*initialization_arguments)
  @loader.context = context if @loader.respond_to?(:context=)
end

Class Attribute Details

.associationObject

Returns the value of attribute association.



9
10
11
# File 'lib/graphql/association_batch_resolver/association_resolver.rb', line 9

def association
  @association
end

.modelObject

Returns the value of attribute model.



9
10
11
# File 'lib/graphql/association_batch_resolver/association_resolver.rb', line 9

def model
  @model
end

.optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/graphql/association_batch_resolver/association_resolver.rb', line 9

def options
  @options
end

Instance Attribute Details

#loaderObject

Returns the value of attribute loader.



32
33
34
# File 'lib/graphql/association_batch_resolver/association_resolver.rb', line 32

def loader
  @loader
end

Class Method Details

.validate!Object



11
12
13
14
# File 'lib/graphql/association_batch_resolver/association_resolver.rb', line 11

def validate!
  validate_model!
  validate_association!
end

.validate_association!Object

Raises:



22
23
24
25
26
# File 'lib/graphql/association_batch_resolver/association_resolver.rb', line 22

def validate_association!
  is_association = !model.reflect_on_association(association).nil?

  raise InvalidAssociation, "Association :#{association} does not exist on #{model}" unless is_association
end

.validate_model!Object

Raises:



16
17
18
19
20
# File 'lib/graphql/association_batch_resolver/association_resolver.rb', line 16

def validate_model!
  is_active_record_model = model < ActiveRecord::Base

  raise InvalidModel, "Model (#{model}) must be an ActiveRecord::Base descendant" unless is_active_record_model
end

Instance Method Details

#resolve(*args) ⇒ Object



43
44
45
46
# File 'lib/graphql/association_batch_resolver/association_resolver.rb', line 43

def resolve(*args)
  loader.args = *args
  loader.load(object)
end