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
# 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
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

#ensure_cardinality(result) ⇒ Object

If the value should be an array, make sure it’s an array. If it should be a single value, make sure it’s single. Passed in result could be a single model or an array of models.



45
46
47
48
49
50
51
52
# File 'lib/graphql/models/association_load_request.rb', line 45

def ensure_cardinality(result)
  case reflection.macro
  when :has_many
    Array.wrap(result)
  else
    result.is_a?(Array) ? result[0] : result
  end
end

#fulfilled(result) ⇒ Object

When the request is fulfilled, this method is called so that it can do whatever caching, etc. is needed



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/graphql/models/association_load_request.rb', line 55

def fulfilled(result)
  association.loaded!

  if reflection.macro == :has_many
    association.target.concat(result)
    result.each do |m|
      association.set_inverse_instance(m)
    end
  else
    association.target = result
    association.set_inverse_instance(result) if result
  end
end

#loadObject



69
70
71
# File 'lib/graphql/models/association_load_request.rb', line 69

def load
  loader.load(self)
end

#load_targetObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/graphql/models/association_load_request.rb', line 25

def load_target
  case reflection.macro
  when :belongs_to
    base_model.send(reflection.foreign_key)
  when :has_many
    base_model.send(association.reflection.name)
  else
    # has_one, need to construct our own relation, because accessing the relation will load the model
    condition = { reflection.foreign_key => base_model.id }

    if reflection.options.include?(:as)
      condition[reflection.type] = base_model.class.name
    end

    target_class.where(condition)
  end
end

#load_typeObject

Public members that all load requests should implement



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

def load_type
  case reflection.macro
  when :belongs_to
    :id
  else
    :relation
  end
end

#target_classObject

Public members specific to an association load request



77
78
79
80
81
82
83
# File 'lib/graphql/models/association_load_request.rb', line 77

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