graphql-rails-resolver 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +18 -0
- data/lib/graphql/rails/resolver.rb +16 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55eaefeb286881c45c50fdef3b3d9914eb4323de
|
4
|
+
data.tar.gz: 7e02bc7b14f7df22fb63a0555736db4bb21efbce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86c21d6f507499de46135bd4a65a7acc08e61c7a95a93029bd8e62c40db5da8ba1045bd353e10c451d7056d60403125bfc0b546acabfbcd3ea2c095777fdab92
|
7
|
+
data.tar.gz: 4786d130b65b80bd5974484b5389583257986546b56a6f2e24f4a7d5c40e0d006008348512c598027168f1f4c6ec382442f33a6cab14b2a9ebcde40712f70dbe
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -96,6 +96,9 @@ class Post < GraphQL::Rails::Resolver
|
|
96
96
|
# Condition resolution on title being present using the `unless` option
|
97
97
|
resolve :title, unless: -> (value) { value.blank? }
|
98
98
|
|
99
|
+
# Resolve :title but preprocess the argument value first (strip leading/trailing spaces)
|
100
|
+
resolve :title, map: -> (value) { value.strip }
|
101
|
+
|
99
102
|
# Resolve :featured argument with default test: if argument `featured` is present
|
100
103
|
resolve :featured, :scope => :featured
|
101
104
|
|
@@ -152,7 +155,22 @@ def check_value(value)
|
|
152
155
|
end
|
153
156
|
```
|
154
157
|
|
158
|
+
### Preprocessing argument values
|
159
|
+
You can alter an argument's value before it is being resolved. To do this, pass a method
|
160
|
+
name (as a symbol or a string), or a `Proc` (or lambda expression) to the `:map` option
|
161
|
+
of `resolve`. The method or `Proc` you specify is then passed the original argument value
|
162
|
+
and expected to return the value that shall be used for resolution.
|
163
|
+
|
164
|
+
This comes in handy in various cases, for instance when you need to make sure that an
|
165
|
+
argument value is well-defined:
|
166
|
+
|
167
|
+
```
|
168
|
+
resolve :offset, map: -> (value) { [value, 0].max }
|
169
|
+
resolve :limit, map: -> (value) { [value, 100].min }
|
170
|
+
```
|
155
171
|
|
172
|
+
The above example guarantees that the offset is never negative and that the limit is
|
173
|
+
capped at a reasonable value (for [security reasons](https://rmosolgo.github.io/graphql-ruby/queries/security)).
|
156
174
|
|
157
175
|
### Detecting the Model
|
158
176
|
The resolver will automatically resolve to a Rails model with the same name. This behavior can be overridden by defining a `Post#model` which returns the appropriate model.
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module GraphQL
|
2
2
|
module Rails
|
3
3
|
class Resolver
|
4
|
-
VERSION = '0.2.
|
4
|
+
VERSION = '0.2.8'
|
5
5
|
|
6
6
|
attr_accessor :resolvers
|
7
7
|
|
@@ -43,13 +43,14 @@ module GraphQL
|
|
43
43
|
@result = resolve_id(args[@id_field])
|
44
44
|
end
|
45
45
|
|
46
|
-
@resolvers.each do |arg,resolvers|
|
46
|
+
@resolvers.each do |arg, resolvers|
|
47
47
|
if args.key? arg
|
48
|
-
|
48
|
+
original_value = args[arg]
|
49
49
|
|
50
50
|
resolvers.each do |method, params|
|
51
|
-
next unless condition_met?(params.fetch(:if, nil), true,
|
52
|
-
next unless condition_met?(params.fetch(:unless, nil), false,
|
51
|
+
next unless condition_met?(params.fetch(:if, nil), true, original_value)
|
52
|
+
next unless condition_met?(params.fetch(:unless, nil), false, original_value)
|
53
|
+
value = map_value(params.fetch(:map, nil), original_value)
|
53
54
|
|
54
55
|
# Match scopes
|
55
56
|
if params.key? :scope
|
@@ -206,6 +207,16 @@ module GraphQL
|
|
206
207
|
end
|
207
208
|
end
|
208
209
|
|
210
|
+
def map_value(mapper, value)
|
211
|
+
if mapper.respond_to? :call
|
212
|
+
mapper.call(value)
|
213
|
+
elsif (mapper.is_a?(Symbol) || mapper.is_a?(String)) && self.respond_to?(mapper)
|
214
|
+
self.send(mapper, value)
|
215
|
+
else
|
216
|
+
value
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
209
220
|
class << self
|
210
221
|
@@id_field = :id
|
211
222
|
|