graphql-rails-resolver 0.2.7 → 0.2.8

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
  SHA1:
3
- metadata.gz: 6ef4cb840bbd737f5fe330cbc66ced0223beb614
4
- data.tar.gz: 7eee317b609f529c10f48e59cd44ffef05af1776
3
+ metadata.gz: 55eaefeb286881c45c50fdef3b3d9914eb4323de
4
+ data.tar.gz: 7e02bc7b14f7df22fb63a0555736db4bb21efbce
5
5
  SHA512:
6
- metadata.gz: fc822cad0c39a550e12678cef7224a2f4925bee0a45a5730eed08145b041c323d3c733eb21714ace5392a28a068d50ccf4d4f439d2a9ab177512ea3f12a12ccb
7
- data.tar.gz: 9d4b16ab71ffcff4d036b349ec98959b8163488a73abb7cfb4ecf7dad2790ee25f17ed2a2e4beabb49dbc1ec070df3e15d10e2f83b3d5233ce30a9ee8c9c533e
6
+ metadata.gz: 86c21d6f507499de46135bd4a65a7acc08e61c7a95a93029bd8e62c40db5da8ba1045bd353e10c451d7056d60403125bfc0b546acabfbcd3ea2c095777fdab92
7
+ data.tar.gz: 4786d130b65b80bd5974484b5389583257986546b56a6f2e24f4a7d5c40e0d006008348512c598027168f1f4c6ec382442f33a6cab14b2a9ebcde40712f70dbe
data/CHANGELOG.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # GraphQL::Rails::Resolver
2
2
  ## CHANGELOG
3
3
 
4
+ ### Version 0.2.8
5
+ Added argument value preprocessing using `:map` option
6
+
4
7
  ### Version 0.2.7
5
8
  Added conditional resolution by means of `:if` and `:unless` options
6
9
 
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.7'
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
- value = args[arg]
48
+ original_value = args[arg]
49
49
 
50
50
  resolvers.each do |method, params|
51
- next unless condition_met?(params.fetch(:if, nil), true, value)
52
- next unless condition_met?(params.fetch(:unless, nil), false, value)
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-rails-resolver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cole Turner