graphiti 1.2.35 → 1.2.36

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: 5aaea1da30c8eb6b86abc1be0df8fba47d2df8f55be42f6141f78976a2c02dea
4
- data.tar.gz: c4611d1226bc10b04a755e29943bc131831cb9df23cb55480ab3daa3cfa8d4b5
3
+ metadata.gz: 2b7630aef39d3ce4584f53710a5cbe8617addc0a0ea042077c41562accb1c5cf
4
+ data.tar.gz: aad06cd10d1f2604432303990cf92d7b885333d15ccd733a5d0bbab062346872
5
5
  SHA512:
6
- metadata.gz: 20c0ca9615484a566e528c5851702dfff7436c37e3ccf7774ef37611699397870bb74be19f633b1dfc9eca9c315642a29777412c66823550290bca8406e6a254
7
- data.tar.gz: 1edb1ae4364f3b348fa74dffc6c8a794b0e54d39289144614e0c9e605e0dc3df6f850ea72b7117f99a72725ffc9cf536cda93a9694ee3a39a7cc21748405973b
6
+ metadata.gz: 063c97d7d4c719c1cbff7a8028b54a0d862e763bda69a8bae12cbe4a2d5201183b6d516936e9339faf3d35307a1375cf28859253e5fd3c6d1b5824b306fe0d2a
7
+ data.tar.gz: 07dc6893d86bbdfbe676b15707edae40601159d1bb7239b58004280e3c9e5d29c313a9bcc525a2fe7e9582f6191972b8095c0d4a5873a72f15cb5589c9879841
@@ -74,19 +74,22 @@ module Graphiti
74
74
  name_chain << k unless name_chain.last == k
75
75
 
76
76
  unless remote_resource? && serializers.nil?
77
- attrs[name.to_sym] = if serializers.is_a?(Array)
78
- serializers.map do |rr|
77
+ payload = if serializers.is_a?(Array)
78
+ data = serializers.map { |rr|
79
79
  rr.to_hash(fields: fields, include: nested_include, graphql: graphql, name_chain: name_chain)
80
- end
80
+ }
81
+ graphql ? {nodes: data} : data
81
82
  elsif serializers.nil?
82
83
  if @resource.class.respond_to?(:sideload)
83
84
  if @resource.class.sideload(k).type.to_s.include?("_many")
84
- []
85
+ graphql ? {nodes: []} : []
85
86
  end
86
87
  end
87
88
  else
88
89
  serializers.to_hash(fields: fields, include: nested_include, graphql: graphql, name_chain: name_chain)
89
90
  end
91
+
92
+ attrs[name.to_sym] = payload
90
93
  end
91
94
  end
92
95
 
@@ -133,29 +136,58 @@ module Graphiti
133
136
  serializers = options[:data]
134
137
  opts = options.slice(:fields, :include)
135
138
  opts[:graphql] = @graphql
136
- to_hash(serializers, opts).tap do |hash|
137
- hash.merge!(options.slice(:meta)) unless options[:meta].empty?
138
- end
139
+ top_level_key = get_top_level_key(@resource, serializers.is_a?(Array))
140
+
141
+ hash = {top_level_key => {}}
142
+ nodes = get_nodes(serializers, opts)
143
+ add_nodes(hash, top_level_key, options, nodes, @graphql)
144
+ add_stats(hash, top_level_key, options, @graphql)
145
+ hash
139
146
  end
140
147
 
141
148
  private
142
149
 
143
- def to_hash(serializers, opts)
144
- {}.tap do |hash|
145
- top_level_key = :data
146
- if @graphql
147
- top_level_key = @resource.graphql_entrypoint
148
- unless serializers.is_a?(Array)
149
- top_level_key = top_level_key.to_s.singularize.to_sym
150
- end
150
+ def get_top_level_key(resource, is_many)
151
+ key = :data
152
+
153
+ if @graphql
154
+ key = @resource.graphql_entrypoint
155
+ key = key.to_s.singularize.to_sym unless is_many
156
+ end
157
+
158
+ key
159
+ end
160
+
161
+ def get_nodes(serializers, opts)
162
+ if serializers.is_a?(Array)
163
+ serializers.map do |s|
164
+ s.to_hash(**opts)
151
165
  end
166
+ else
167
+ serializers.to_hash(**opts)
168
+ end
169
+ end
170
+
171
+ def add_nodes(hash, top_level_key, opts, nodes, graphql)
172
+ payload = nodes
173
+ if graphql && nodes.is_a?(Array)
174
+ payload = {nodes: nodes}
175
+ end
152
176
 
153
- hash[top_level_key] = if serializers.is_a?(Array)
154
- serializers.map do |s|
155
- s.to_hash(**opts)
177
+ # Don't render nodes if we only requested stats
178
+ unless graphql && opts[:fields].values == [[:stats]]
179
+ hash[top_level_key] = payload
180
+ end
181
+ end
182
+
183
+ def add_stats(hash, top_level_key, options, graphql)
184
+ if options[:meta] && !options[:meta].empty?
185
+ if @graphql
186
+ if (stats = options[:meta][:stats])
187
+ hash[top_level_key][:stats] = stats
156
188
  end
157
189
  else
158
- serializers.to_hash(**opts)
190
+ hash.merge!(options.slice(:meta))
159
191
  end
160
192
  end
161
193
  end
@@ -96,9 +96,14 @@ module Graphiti
96
96
  extra_attributes: extra_attributes(r),
97
97
  sorts: sorts(r),
98
98
  filters: filters(r),
99
- relationships: relationships(r)
99
+ relationships: relationships(r),
100
+ stats: stats(r)
100
101
  }
101
102
 
103
+ if r.grouped_filters.any?
104
+ config[:filter_group] = r.grouped_filters
105
+ end
106
+
102
107
  if r.default_sort
103
108
  default_sort = r.default_sort.map { |s|
104
109
  {s.keys.first.to_s => s.values.first.to_s}
@@ -165,6 +170,14 @@ module Graphiti
165
170
  end
166
171
  end
167
172
 
173
+ def stats(resource)
174
+ {}.tap do |stats|
175
+ resource.stats.each_pair do |name, config|
176
+ stats[name] = config.calculations.keys
177
+ end
178
+ end
179
+ end
180
+
168
181
  def sorts(resource)
169
182
  {}.tap do |s|
170
183
  resource.sorts.each_pair do |name, sort|
@@ -212,6 +225,10 @@ module Graphiti
212
225
  end
213
226
  end
214
227
 
228
+ def filter_group(resource)
229
+ resource.config[:grouped_filters]
230
+ end
231
+
215
232
  def relationships(resource)
216
233
  {}.tap do |r|
217
234
  resource.sideloads.each_pair do |name, config|
@@ -30,6 +30,8 @@ module Graphiti
30
30
  compare_extra_attributes(r, new_resource)
31
31
  compare_sorts(r, new_resource)
32
32
  compare_filters(r, new_resource)
33
+ compare_filter_group(r, new_resource)
34
+ compare_stats(r, new_resource)
33
35
  compare_relationships(r, new_resource)
34
36
  end
35
37
  end
@@ -204,6 +206,42 @@ module Graphiti
204
206
  end
205
207
  end
206
208
 
209
+ def compare_filter_group(old_resource, new_resource)
210
+ if new_resource[:filter_group]
211
+ if old_resource[:filter_group]
212
+ new_names = new_resource[:filter_group][:names]
213
+ old_names = old_resource[:filter_group][:names]
214
+ diff = new_names - old_names
215
+ if !diff.empty? && new_resource[:filter_group][:required] == :all
216
+ @errors << "#{old_resource[:name]}: all required filter group #{old_names.inspect} added #{"member".pluralize(diff.length)} #{diff.inspect}."
217
+ end
218
+
219
+ old_required = old_resource[:filter_group][:required]
220
+ new_required = new_resource[:filter_group][:required]
221
+ if old_required == :any && new_required == :all
222
+ @errors << "#{old_resource[:name]}: filter group #{old_names.inspect} moved from required: :any to required: :all"
223
+ end
224
+ else
225
+ @errors << "#{old_resource[:name]}: filter group #{new_resource[:filter_group][:names].inspect} was added."
226
+ end
227
+ end
228
+ end
229
+
230
+ def compare_stats(old_resource, new_resource)
231
+ old_resource[:stats].each_pair do |name, old_calculations|
232
+ new_calculations = new_resource[:stats][name]
233
+ if new_calculations
234
+ old_calculations.each do |calc|
235
+ unless new_calculations.include?(calc)
236
+ @errors << "#{old_resource[:name]}: calculation #{calc.inspect} was removed from stat #{name.inspect}."
237
+ end
238
+ end
239
+ else
240
+ @errors << "#{old_resource[:name]}: stat #{name.inspect} was removed."
241
+ end
242
+ end
243
+ end
244
+
207
245
  def compare_endpoints
208
246
  @old[:endpoints].each_pair do |path, old_endpoint|
209
247
  unless (new_endpoint = @new[:endpoints][path])
@@ -1,3 +1,3 @@
1
1
  module Graphiti
2
- VERSION = "1.2.35"
2
+ VERSION = "1.2.36"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphiti
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.35
4
+ version: 1.2.36
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-18 00:00:00.000000000 Z
11
+ date: 2021-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializable