graphiti 1.2.38 → 1.2.43
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graphiti/adapters/abstract.rb +3 -2
- data/lib/graphiti/adapters/active_record.rb +7 -3
- data/lib/graphiti/adapters/null.rb +1 -1
- data/lib/graphiti/delegates/pagination.rb +20 -8
- data/lib/graphiti/query.rb +4 -2
- data/lib/graphiti/request_validators/validator.rb +10 -5
- data/lib/graphiti/scoping/filter.rb +12 -8
- data/lib/graphiti/scoping/paginate.rb +20 -2
- data/lib/graphiti/sideload.rb +6 -1
- data/lib/graphiti/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6020ab7e3340dafb7ddad630e79456d6e50b581aaa3cd455069ad5a25860fbb5
|
4
|
+
data.tar.gz: da8286216aab03780ec61427289aa6fecfb82c6dc2ff374de5cbb4bd223ddbcd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c6dc240dbad87f9900412ac9afdfa32a20963e79827fcdb4bba94c2f7cac159057e80410f797da42b243dbca14d3753a60a6bfb7df404b16b01d8bad2c3c1d6
|
7
|
+
data.tar.gz: f12629255a14d042b311101110a47fbfe4a099a618e40c2af020f33b08d50bf00741d7f78c81cea0031748828774871d0d954be79a7f6c681a33bae33729f166
|
@@ -244,14 +244,15 @@ module Graphiti
|
|
244
244
|
# @param scope The scope object we are chaining
|
245
245
|
# @param [Integer] current_page The current page number
|
246
246
|
# @param [Integer] per_page The number of results per page
|
247
|
+
# @param [Integer] offset The offset to start from
|
247
248
|
# @return the scope
|
248
249
|
#
|
249
250
|
# @example ActiveRecord default
|
250
251
|
# # via kaminari gem
|
251
|
-
# def paginate(scope, current_page, per_page)
|
252
|
+
# def paginate(scope, current_page, per_page, offset)
|
252
253
|
# scope.page(current_page).per(per_page)
|
253
254
|
# end
|
254
|
-
def paginate(scope, current_page, per_page)
|
255
|
+
def paginate(scope, current_page, per_page, offset)
|
255
256
|
raise "you must override #paginate in an adapter subclass"
|
256
257
|
end
|
257
258
|
|
@@ -184,8 +184,11 @@ module Graphiti
|
|
184
184
|
end
|
185
185
|
|
186
186
|
# (see Adapters::Abstract#paginate)
|
187
|
-
def paginate(scope, current_page, per_page)
|
188
|
-
scope.page(current_page)
|
187
|
+
def paginate(scope, current_page, per_page, offset)
|
188
|
+
scope = scope.page(current_page) if current_page
|
189
|
+
scope = scope.per(per_page) if per_page
|
190
|
+
scope = scope.padding(offset) if offset
|
191
|
+
scope
|
189
192
|
end
|
190
193
|
|
191
194
|
# (see Adapters::Abstract#count)
|
@@ -240,7 +243,8 @@ module Graphiti
|
|
240
243
|
children.each do |child|
|
241
244
|
if association_type == :many_to_many &&
|
242
245
|
[:create, :update].include?(Graphiti.context[:namespace]) &&
|
243
|
-
!parent.send(association_name).exists?(child.id)
|
246
|
+
!parent.send(association_name).exists?(child.id) &&
|
247
|
+
child.errors.blank?
|
244
248
|
parent.send(association_name) << child
|
245
249
|
else
|
246
250
|
target = association.instance_variable_get(:@target)
|
@@ -30,13 +30,14 @@ module Graphiti
|
|
30
30
|
|
31
31
|
uri = URI(@proxy.resource.endpoint[:url].to_s)
|
32
32
|
|
33
|
+
page_params = {
|
34
|
+
number: page,
|
35
|
+
size: page_size
|
36
|
+
}
|
37
|
+
page_params[:offset] = offset if offset
|
38
|
+
|
33
39
|
# Overwrite the pagination query params with the desired page
|
34
|
-
uri.query = pagination_params.merge(
|
35
|
-
page: {
|
36
|
-
number: page,
|
37
|
-
size: page_size
|
38
|
-
}
|
39
|
-
}).to_query
|
40
|
+
uri.query = pagination_params.merge(page: page_params).to_query
|
40
41
|
uri.to_s
|
41
42
|
end
|
42
43
|
|
@@ -46,8 +47,11 @@ module Graphiti
|
|
46
47
|
elsif page_size == 0 || item_count == 0
|
47
48
|
return nil
|
48
49
|
end
|
49
|
-
|
50
|
-
|
50
|
+
|
51
|
+
count = item_count
|
52
|
+
count = item_count - offset if offset
|
53
|
+
@last_page = (count / page_size)
|
54
|
+
@last_page += 1 if count % page_size > 0
|
51
55
|
@last_page
|
52
56
|
end
|
53
57
|
|
@@ -82,6 +86,14 @@ module Graphiti
|
|
82
86
|
@current_page ||= (page_param[:number] || 1).to_i
|
83
87
|
end
|
84
88
|
|
89
|
+
def offset
|
90
|
+
@offset ||= begin
|
91
|
+
if (value = page_param[:offset])
|
92
|
+
value.to_i
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
85
97
|
def page_size
|
86
98
|
@page_size ||= (page_param[:size] ||
|
87
99
|
@proxy.resource.default_page_size ||
|
data/lib/graphiti/query.rb
CHANGED
@@ -32,7 +32,9 @@ module Graphiti
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def pagination_links?
|
35
|
-
if
|
35
|
+
if action == :find
|
36
|
+
false
|
37
|
+
elsif Graphiti.config.pagination_links_on_demand
|
36
38
|
[true, "true"].include?(@params[:pagination_links])
|
37
39
|
else
|
38
40
|
Graphiti.config.pagination_links
|
@@ -193,7 +195,7 @@ module Graphiti
|
|
193
195
|
end
|
194
196
|
elsif nested?(name)
|
195
197
|
hash[name.to_s.split(".").last.to_sym] = value
|
196
|
-
elsif top_level? && [:number, :size].include?(name.to_sym)
|
198
|
+
elsif top_level? && [:number, :size, :offset].include?(name.to_sym)
|
197
199
|
hash[name.to_sym] = value.to_i
|
198
200
|
end
|
199
201
|
end
|
@@ -25,7 +25,7 @@ module Graphiti
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
typecast_attributes(resource, deserialized_payload.attributes, deserialized_payload.meta[:payload_path])
|
28
|
+
typecast_attributes(resource, deserialized_payload.attributes, @action, deserialized_payload.meta[:payload_path])
|
29
29
|
process_relationships(resource, deserialized_payload.relationships, deserialized_payload.meta[:payload_path])
|
30
30
|
else
|
31
31
|
errors.add(:"data.type", :missing)
|
@@ -65,15 +65,20 @@ module Graphiti
|
|
65
65
|
next
|
66
66
|
end
|
67
67
|
|
68
|
-
|
69
|
-
|
68
|
+
resource = x[:resource]
|
69
|
+
attributes = x[:attributes]
|
70
|
+
relationships = x[:relationships]
|
71
|
+
payload_path = x[:meta][:payload_path]
|
72
|
+
action = x[:meta][:method]
|
73
|
+
typecast_attributes(resource, attributes, action, payload_path)
|
74
|
+
process_relationships(resource, relationships, payload_path)
|
70
75
|
end
|
71
76
|
end
|
72
77
|
|
73
|
-
def typecast_attributes(resource, attributes, payload_path)
|
78
|
+
def typecast_attributes(resource, attributes, action, payload_path)
|
74
79
|
attributes.each_pair do |key, value|
|
75
80
|
# Only validate id if create action, otherwise it's only used for lookup
|
76
|
-
next if
|
81
|
+
next if action != :create &&
|
77
82
|
key == :id &&
|
78
83
|
resource.class.config[:attributes][:id][:writable] == false
|
79
84
|
|
@@ -171,14 +171,18 @@ module Graphiti
|
|
171
171
|
type = Graphiti::Types[filter[:type]]
|
172
172
|
array_or_string = [:string, :array].include?(type[:canonical_name])
|
173
173
|
if (arr = value.scan(/\[.*?\]/)).present? && array_or_string
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
174
|
+
begin
|
175
|
+
value = arr.map { |json|
|
176
|
+
begin
|
177
|
+
JSON.parse(json)
|
178
|
+
rescue
|
179
|
+
raise Errors::InvalidJSONArray.new(resource, value)
|
180
|
+
end
|
181
|
+
}
|
182
|
+
value = value[0] if value.length == 1
|
183
|
+
rescue Errors::InvalidJSONArray => e
|
184
|
+
raise(e) if type[:canonical_name] == :array
|
185
|
+
end
|
182
186
|
else
|
183
187
|
value = parse_string_arrays(value, !!filter[:single])
|
184
188
|
end
|
@@ -33,12 +33,24 @@ module Graphiti
|
|
33
33
|
|
34
34
|
# Apply default pagination proc via the Resource adapter
|
35
35
|
def apply_standard_scope
|
36
|
-
resource.adapter.paginate
|
36
|
+
arity = resource.adapter.method(:paginate)
|
37
|
+
|
38
|
+
if arity == 4 # backwards-compat
|
39
|
+
resource.adapter.paginate(@scope, number, size)
|
40
|
+
else
|
41
|
+
resource.adapter.paginate(@scope, number, size, offset)
|
42
|
+
end
|
37
43
|
end
|
38
44
|
|
39
45
|
# Apply the custom pagination proc
|
40
46
|
def apply_custom_scope
|
41
|
-
resource.instance_exec
|
47
|
+
resource.instance_exec \
|
48
|
+
@scope,
|
49
|
+
number,
|
50
|
+
size,
|
51
|
+
resource.context,
|
52
|
+
offset,
|
53
|
+
&custom_scope
|
42
54
|
end
|
43
55
|
|
44
56
|
private
|
@@ -51,6 +63,12 @@ module Graphiti
|
|
51
63
|
@page_param ||= (query_hash[:page] || {})
|
52
64
|
end
|
53
65
|
|
66
|
+
def offset
|
67
|
+
if (value = page_param[:offset])
|
68
|
+
value.to_i
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
54
72
|
def number
|
55
73
|
(page_param[:number] || 1).to_i
|
56
74
|
end
|
data/lib/graphiti/sideload.rb
CHANGED
@@ -137,11 +137,16 @@ module Graphiti
|
|
137
137
|
end
|
138
138
|
|
139
139
|
def link_extra_fields
|
140
|
+
return unless context&.respond_to?(:params)
|
141
|
+
|
140
142
|
extra_fields_name = [association_name, resource.type].find { |param|
|
141
143
|
context.params.dig(:extra_fields, param)
|
142
144
|
}
|
143
145
|
|
144
|
-
|
146
|
+
if extra_fields_name
|
147
|
+
extra_fields = context.params.dig(:extra_fields, extra_fields_name)
|
148
|
+
{resource.type => extra_fields}
|
149
|
+
end
|
145
150
|
end
|
146
151
|
|
147
152
|
# The parent resource is a remote,
|
data/lib/graphiti/version.rb
CHANGED
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.
|
4
|
+
version: 1.2.43
|
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-
|
11
|
+
date: 2021-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jsonapi-serializable
|
@@ -361,7 +361,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
361
361
|
- !ruby/object:Gem::Version
|
362
362
|
version: '0'
|
363
363
|
requirements: []
|
364
|
-
rubygems_version: 3.0.
|
364
|
+
rubygems_version: 3.0.3
|
365
365
|
signing_key:
|
366
366
|
specification_version: 4
|
367
367
|
summary: Easily build jsonapi.org-compatible APIs
|