Class: PreloadFunction

Inherits:
GraphQL::Function
  • Object
show all
Defined in:
lib/preload_function.rb

Instance Method Summary collapse

Instance Method Details

#call(obj, args, ctx) ⇒ Object



3
4
5
6
7
8
# File 'lib/preload_function.rb', line 3

def call obj, args, ctx
  @ctx = ctx
  @json_tree = {}
  @result_tree = {}
  _call obj, args, ctx
end

#gen_accociations_tree(model_class = @root_model_class, tree = json_tree_without_root, result_tree = @result_tree) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/preload_function.rb', line 33

def gen_accociations_tree model_class = @root_model_class, tree = json_tree_without_root, result_tree = @result_tree
  tree.each do |k, v|
    if v.present? && model_class.reflect_on_all_associations.map(&:name).include?(k)
      result_tree[k.to_sym] = {}
      next if model_class.reflections[k.to_s].options[:polymorphic]
      _model_class = model_class.reflections[k.to_s].class_name.constantize
      gen_accociations_tree _model_class, tree[k], result_tree[k.to_sym]
    end
  end
end

#included_model(model_class) ⇒ Object



10
11
12
13
14
15
# File 'lib/preload_function.rb', line 10

def included_model model_class
  @root_model_class = model_class
  recursion query_tree
  gen_accociations_tree model_class
  model_class.includes @result_tree
end

#json_tree_without_rootObject



29
30
31
# File 'lib/preload_function.rb', line 29

def json_tree_without_root
  @json_tree.values.first
end

#query_treeObject



17
18
19
# File 'lib/preload_function.rb', line 17

def query_tree
  @ctx.query.document.children.first.selections.first
end

#recursion(node, tree = @json_tree) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/preload_function.rb', line 21

def recursion node, tree = @json_tree
  node_key = node.name.underscore.to_sym
  tree[node_key] = {}
  node.selections.each do |n|
    recursion n, tree[node_key]
  end
end