Class: GraphQLIncludable::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql_includable/resolver.rb

Class Method Summary collapse

Class Method Details

.figure_out_association(attribute_name, parent_model) ⇒ Object



182
183
184
185
186
187
# File 'lib/graphql_includable/resolver.rb', line 182

def self.figure_out_association(attribute_name, parent_model)
  delegated_through = includes_delegated_through(parent_model, attribute_name)
  delegated_model = model_name_to_class(delegated_through.last) if delegated_through.present?
  association = (delegated_model || parent_model).reflect_on_association(attribute_name)
  association
end

.find_node_by_return_type(node, desired_return_type) ⇒ Object

Returns the first node in the tree which returns a specific type



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphql_includable/resolver.rb', line 48

def self.find_node_by_return_type(node, desired_return_type)
  return_type = node.return_type.unwrap.to_s
  return node if return_type == desired_return_type
  if node.respond_to?(:scoped_children)
    matching_node = nil
    node.scoped_children.values.each do |selections|
      matching_node = selections.values.find do |child_node|
        find_node_by_return_type(child_node, desired_return_type)
      end
      break if matching_node
    end
    matching_node
  end
end

.includes_delegated_through(base_model, method_name) ⇒ Object

If method_name is delegated from base_model, return an array of associations through which those methods can be delegated



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/graphql_includable/resolver.rb', line 191

def self.includes_delegated_through(base_model, method_name)
  chain = []
  method = method_name.to_sym
  model_name = base_model.instance_variable_get(:@delegate_cache).try(:[], method)
  while model_name
    chain << model_name
    model = model_name_to_class(model_name)
    model_name = model.instance_variable_get(:@delegate_cache).try(:[], method)
  end
  chain
end

.includes_for_child(node, parent_model, includes_manager) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/graphql_includable/resolver.rb', line 129

def self.includes_for_child(node, parent_model, includes_manager)
  associations = possible_associations(node, parent_model)

  if associations.present?
    if node_is_relay_connection?(node)
      includes_from_connection(node, parent_model, associations, includes_manager)
    else
      association = associations[:default] # should only be one
      child_includes_manager = includes_manager.add_child_include(association)
      includes_for_node(node, child_includes_manager)
    end
  end
end

.includes_for_node(node, includes_manager) ⇒ Object

Translate a node’s selections into includes values Combine and format children values Noop on nodes that don’t return AR (so no associations to include)



66
67
68
69
70
71
72
73
74
75
# File 'lib/graphql_includable/resolver.rb', line 66

def self.includes_for_node(node, includes_manager)
  return_model = node_return_model(node)
  return [] if return_model.blank?

  children = node.scoped_children[node.return_type.unwrap]

  children.each_value do |child_node|
    includes_for_child(child_node, return_model, includes_manager)
  end
end

.includes_from_connection(node, parent_model, associations_from_parent_model, includes_manager) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/graphql_includable/resolver.rb', line 77

def self.includes_from_connection(node, parent_model, associations_from_parent_model, includes_manager)
  return unless node.return_type.fields['edges'].edge_class <= GraphQLIncludable::Relay::EdgeWithNode  # TODO: Possibly basic support for connections with only nodes

  edges_association = associations_from_parent_model[:edges]
  nodes_association = associations_from_parent_model[:nodes]

  edge_node_attribute = node.return_type.fields['edges'].[:edge_to_node_property]
  edge_model = edges_association.klass
  edge_to_node_association = edge_model.reflect_on_association(edge_node_attribute)
  node_model = edge_to_node_association.klass

  connection_children = node.scoped_children[node.return_type.unwrap]
  connection_children.each_value do |connection_node|
    # connection_field {
    #   pageInfo {...}
    #   nodes {
    #     node_model_field ...
    #   }
    #   edges {
    #     edge_model_field ...
    #     node {
    #       node_model_field ...
    #     }
    #   }
    # }

    if connection_node.name == 'edges'
      edges_includes_manager = includes_manager.add_child_include(edges_association)

      edge_children = connection_node.scoped_children[connection_node.return_type.unwrap]
      edge_children.each_value do |edge_child_node|
        if edge_child_node.name == 'node'
          node_includes_manager = edges_includes_manager.add_child_include(edge_to_node_association)

          node_children = edge_child_node.scoped_children[edge_child_node.return_type.unwrap]
          node_children.each_value do |node_child_node|
            includes_for_child(node_child_node, node_model, node_includes_manager)
          end
        else
          includes_for_child(edge_child_node, edge_model, edges_includes_manager)
        end
      end
    elsif connection_node.name == 'nodes'
      nodes_includes_manager = includes_manager.add_child_include(nodes_association)
      node_children = connection_node.scoped_children[connection_node.return_type.unwrap]
      node_children.each_value do |node_child_node|
        includes_for_child(node_child_node, node_model, nodes_includes_manager)
      end
    end
  end
end

.model_name_to_class(model_name) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/graphql_includable/resolver.rb', line 143

def self.model_name_to_class(model_name)
  begin
    model_name.to_s.camelize.constantize
  rescue NameError
    model_name.to_s.singularize.camelize.constantize
  end
rescue
end

.node_is_relay_connection?(node) ⇒ Boolean

Returns:



159
160
161
# File 'lib/graphql_includable/resolver.rb', line 159

def self.node_is_relay_connection?(node)
  node.return_type.unwrap.name =~ /Connection$/
end

.node_possible_association_names(node) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/graphql_includable/resolver.rb', line 168

def self.node_possible_association_names(node)
  definition = node.definitions.first

  association_names = {}
  if node_is_relay_connection?(node)
    association_names[:edges] = definition.[:edges_property] if definition..key?(:edges_property)
    association_names[:nodes] = definition.[:nodes_property] if definition..key?(:nodes_property)
    return association_names if association_names.present? # This should be an includable connection with no :property or name fallback.
  end

  association_names[:default] = definition.[:includes] || (definition.property || definition.name).to_sym
  association_names
end

.node_return_model(node) ⇒ Object

Translate a node’s return type to an ActiveRecord model



153
154
155
156
157
# File 'lib/graphql_includable/resolver.rb', line 153

def self.node_return_model(node)
  model = Object.const_get(node.return_type.unwrap.name.gsub(/(^SquareFoot|Edge$|Connection$)/, ''))
  model if model < ActiveRecord::Base
rescue NameError
end

.possible_associations(node, parent_model) ⇒ Object



163
164
165
166
# File 'lib/graphql_includable/resolver.rb', line 163

def self.possible_associations(node, parent_model)
  attribute_names = node_possible_association_names(node)
  attribute_names.transform_values { |attribute_name| figure_out_association(attribute_name, parent_model) }.compact
end