Class: GraphQL::Models::AssociationLoadRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/models/association_load_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_model, association_name, context) ⇒ AssociationLoadRequest

Returns a new instance of AssociationLoadRequest.



6
7
8
9
10
11
12
13
14
# File 'lib/graphql/models/association_load_request.rb', line 6

def initialize(base_model, association_name, context)
  @base_model = base_model
  @association = base_model.association(association_name)
  @context = context

  if reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
    fail ArgumentError, "You cannot batch-load a has_many :through association. Instead, load each association individually."
  end
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



4
5
6
# File 'lib/graphql/models/association_load_request.rb', line 4

def association
  @association
end

#base_modelObject (readonly)

Returns the value of attribute base_model.



4
5
6
# File 'lib/graphql/models/association_load_request.rb', line 4

def base_model
  @base_model
end

#contextObject (readonly)

Returns the value of attribute context.



4
5
6
# File 'lib/graphql/models/association_load_request.rb', line 4

def context
  @context
end

Instance Method Details

#loadObject



23
24
25
26
27
28
29
# File 'lib/graphql/models/association_load_request.rb', line 23

def load
  loader.load(request).then do |result|
    result = result.first unless reflection.macro == :has_many
    Helpers.load_association_with(association, result)
    result
  end
end

#requestObject



16
17
18
19
20
21
# File 'lib/graphql/models/association_load_request.rb', line 16

def request
  AttributeLoader::Request.new(
    association.scope.where_values_hash,
    Helpers.orders_to_sql(association.scope.orders)
  )
end

#target_classObject

Public members specific to an association load request



35
36
37
38
39
40
41
# File 'lib/graphql/models/association_load_request.rb', line 35

def target_class
  case when reflection.polymorphic?
    base_model.send(reflection.foreign_type).constantize
  else
    reflection.klass
  end
end