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



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

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)
    raise 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.



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

def association
  @association
end

#base_modelObject (readonly)

Returns the value of attribute base_model.



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

def base_model
  @base_model
end

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

Instance Method Details

#loadObject



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

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



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

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



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

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