graphoid 0.0.10 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 375c20ef39601434456ec8704edecc50a6d746904ae4b66bd9f14372c0ff6cb5
4
- data.tar.gz: 88250e49413a5e779cbb38734b814ec80896e14cf111ca730b2d9afcdc9d7c53
3
+ metadata.gz: 34f70b682b22f97dbe9d9f23cc51db8cb3e7ab30ce5b0ed545916a671f312601
4
+ data.tar.gz: ef6d309e4c7310c397aedfcba0b037a283b0b30675688c64b1455de78438e9db
5
5
  SHA512:
6
- metadata.gz: 28225d479a2ddf3ba1ed6b9715c39cf62843e3b0b74dc4224db7e74132bbb1ab2cfb90f5c289f16dafa69919bd2067bf5905962e6a8d9fc24669d847eeb8704c
7
- data.tar.gz: 440fd7a515d9451a629e3c99d7ddcff351fd621ecad77d942772e96012663a5c80a7e50ac7890280043d690e5678ea301773e200aff77fa05b097d44988ef7ab
6
+ metadata.gz: 29cbfea735f3a72fd1b6842c914a1128eec1cc0e61982a565a179b57a4c5e49a25b0d24563a57f39ba9e72c7252b0f8b2fad42237361080b299c3529e1511126
7
+ data.tar.gz: e20edd2b6ade55cd0a26090945ff0a19794f8f4ebcd9c1636dddd5b9fcdfaeffe309aeb1b1d4a53221140851949f2bc35e3c212660c0b75490080d6ae58fecee
@@ -83,8 +83,26 @@ module Graphoid
83
83
  relation.class
84
84
  end
85
85
 
86
- def eager_load(_selection, model)
87
- model
86
+ def generate_array(nodes)
87
+ include_array = []
88
+ nodes.each do |node|
89
+ children = node.selections.select!{ |n| !n.selections.empty? }
90
+
91
+ if children.empty?
92
+ include_array.push(node.name.to_sym)
93
+ else
94
+ include_array.push(node.name.to_sym => generate_array(children))
95
+ end
96
+ end
97
+
98
+ include_array
99
+ end
100
+
101
+ def eager_load(selection, model)
102
+ nodes = selection.ast_node.selections.first.selections
103
+ nodes.select!{ |n| !n.selections.empty? }
104
+ include_array = generate_array(nodes)
105
+ include_array.empty? ? model : model.includes(*include_array)
88
106
  end
89
107
 
90
108
  def execute_and(scope, parsed)
@@ -114,7 +114,7 @@ module Graphoid
114
114
  relation.relation
115
115
  end
116
116
 
117
- def eager_load(selection, model)
117
+ def eager_load(selection, model, first = true)
118
118
  referenced_relations = [
119
119
  mongo_constants[:many_to_many],
120
120
  mongo_constants[:has_many],
@@ -122,21 +122,27 @@ module Graphoid
122
122
  mongo_constants[:belongs_to]
123
123
  ]
124
124
 
125
- properties = Graphoid::Queries::Processor.children_of(selection)
125
+ properties = first ? Utils.first_children_of(selection) : Utils.children_of(selection)
126
126
  inclusions = Utils.symbolize(properties)
127
127
 
128
128
  Relation.relations_of(model).each do |name, relation|
129
129
  name = relation.name
130
- next if inclusions.exclude?(name) || referenced_relations.exclude?(association.relation)
130
+ next if inclusions.exclude?(name) || referenced_relations.exclude?(relation.relation)
131
131
 
132
132
  subselection = properties[name.to_s.camelize(:lower)]
133
- children = Utils.symbolize(Graphoid::Queries::Processor.children_of(subselection))
134
- relations = relation.class_name.constantize.reflections.values.map(&:name)
133
+ subproperties = Utils.children_of(subselection)
134
+ subchildren = Utils.symbolize(subproperties)
135
+ subrelations = relation.class_name.constantize.relations.values.map(&:name)
135
136
 
136
- if (relations & children).empty?
137
+ if (subrelations & subchildren).empty?
137
138
  model = model.includes(name)
138
139
  else
139
- model = model.includes(name, with: ->(instance) { Graphoid::Queries::Processor.eager_load(subselection, instance) })
140
+ begin
141
+ gem "mongoid_includes"
142
+ model = model.includes(name, with: ->(instance) { eager_load(subselection, instance, false) })
143
+ rescue Gem::LoadError
144
+ model = model.includes(name)
145
+ end
140
146
  end
141
147
  end
142
148
 
@@ -33,10 +33,6 @@ module Graphoid
33
33
  end
34
34
  end
35
35
 
36
- def children_of(selection)
37
- selection.scoped_children.values[0]
38
- end
39
-
40
36
  def parse_order(scope, order)
41
37
  fields = Attribute.fieldnames_of(scope)
42
38
  Utils.underscore(order, fields)
@@ -17,7 +17,15 @@ module Graphoid
17
17
  end
18
18
 
19
19
  def symbolize(fields)
20
- fields.keys.map(&:underscore).map(&:to_sym)
20
+ fields.keys.map { |f| f.underscore.to_sym }
21
+ end
22
+
23
+ def children_of(selection)
24
+ selection.scoped_children.values.first
25
+ end
26
+
27
+ def first_children_of(selection)
28
+ selection.scoped_children.values.first.values.first.scoped_children.values.first
21
29
  end
22
30
 
23
31
  def underscore(props, fields = [])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maximiliano Perez Coto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-03 00:00:00.000000000 Z
11
+ date: 2019-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql