graphql-rails-resolver 0.2.5 → 0.2.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd138790cf8944a77acea1535d813421e37cdfdd
4
- data.tar.gz: a61fbe2035032bf6dc0acd09ce6786635732f178
3
+ metadata.gz: 6ef4cb840bbd737f5fe330cbc66ced0223beb614
4
+ data.tar.gz: 7eee317b609f529c10f48e59cd44ffef05af1776
5
5
  SHA512:
6
- metadata.gz: 5c0fe031a810c3368921b2e215da435a47a33f3694c060e91f5ca037577c5d23a3e2e1ffb78a3080446aa12b3b7e66271e42ac392803e4237e00a2fb88ba3fdf
7
- data.tar.gz: ec1191eb7e4f9601f5eef38e59ea941615f2adf4d28a84f53a62c48230b8e69ac3dece258fd54cc03543216e68eb53cd245177633ade16cfec1dacee5a06f466
6
+ metadata.gz: fc822cad0c39a550e12678cef7224a2f4925bee0a45a5730eed08145b041c323d3c733eb21714ace5392a28a068d50ccf4d4f439d2a9ab177512ea3f12a12ccb
7
+ data.tar.gz: 9d4b16ab71ffcff4d036b349ec98959b8163488a73abb7cfb4ecf7dad2790ee25f17ed2a2e4beabb49dbc1ec070df3e15d10e2f83b3d5233ce30a9ee8c9c533e
@@ -1,6 +1,12 @@
1
1
  # GraphQL::Rails::Resolver
2
2
  ## CHANGELOG
3
3
 
4
+ ### Version 0.2.7
5
+ Added conditional resolution by means of `:if` and `:unless` options
6
+
7
+ ### Version 0.2.6
8
+ Fixes issue where resolution may skip over :where
9
+
4
10
  ### Version 0.2.5
5
11
  Adds heirarchal resolution strategy
6
12
 
data/README.md CHANGED
@@ -92,6 +92,9 @@ class Post < GraphQL::Rails::Resolver
92
92
  resolve :title
93
93
  resolve :createdAt, :where => :created_at
94
94
  resolve :updatedAt, :where => :updated_at
95
+
96
+ # Condition resolution on title being present using the `unless` option
97
+ resolve :title, unless: -> (value) { value.blank? }
95
98
 
96
99
  # Resolve :featured argument with default test: if argument `featured` is present
97
100
  resolve :featured, :scope => :featured
@@ -127,7 +130,28 @@ In the examples above, the three primary arguments to `resolve` are:
127
130
 
128
131
  Alternatively you can specify a symbol representing a method name: (ie: `resolve :arg_1, :custom_method`). The resolver will use it's own method if it exists, or else it will call the method on the object itself.
129
132
 
133
+ ### Conditional resolution
134
+ Sometimes it is necessary to condition resolution of an argument on its value. For instance, by default
135
+ an empty string as an argument matches only records whose corresponding field is an empty string as well.
136
+ However, you may want an empty argument to mean that this argument should be ignored and all records shall
137
+ be matched. To achieve this, you would condition resolution of that argument on it being not empty.
138
+
139
+ You can condition resolution by passing the `:if` or `:unless` option to the `resolve` method. This option
140
+ can take a method name (as a symbol or a string), or a `Proc` (or lambda expression for that matter), which
141
+ will be called with the argument's value:
130
142
 
143
+ ```
144
+ resolve :tagline, unless: -> (value) { value.blank? }
145
+
146
+ resolve :tagline, if: -> (value) { value.present? }
147
+
148
+ resolve :tagline, if: :check_value
149
+
150
+ def check_value(value)
151
+ value.present?
152
+ end
153
+ ```
154
+
131
155
 
132
156
 
133
157
  ### Detecting the Model
@@ -162,3 +186,4 @@ I wanted to release this utility for the hopes of sparking interest in Rails int
162
186
  # Credits
163
187
  - Cole Turner ([@colepatrickturner](https://github.com/colepatrickturner))
164
188
  - Peter Salanki ([@salanki](https://github.com/salanki))
189
+ - Jonas Schwertfeger ([@jschwertfeger](https://github.com/jschwertfeger))
@@ -1,7 +1,7 @@
1
1
  module GraphQL
2
2
  module Rails
3
3
  class Resolver
4
- VERSION = '0.2.5'
4
+ VERSION = '0.2.7'
5
5
 
6
6
  attr_accessor :resolvers
7
7
 
@@ -48,6 +48,9 @@ module GraphQL
48
48
  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)
53
+
51
54
  # Match scopes
52
55
  if params.key? :scope
53
56
  scope_name = params[:scope]
@@ -74,7 +77,8 @@ module GraphQL
74
77
  else
75
78
  raise ArgumentError, "Unable to resolve parameter of type #{method.class} in #{self}"
76
79
  end
77
- elsif params.size < 1
80
+ else
81
+ # Resolve ID arguments
78
82
  if is_arg_id_type? arg
79
83
  value = resolve_id(value)
80
84
  end
@@ -83,7 +87,7 @@ module GraphQL
83
87
  @result = send(arg, value)
84
88
  elsif @result.respond_to? arg and params[:where].present? == false
85
89
  @result = @result.send(arg, value)
86
- else
90
+ elsif @result.respond_to? :where
87
91
  attribute =
88
92
  if params[:where].present?
89
93
  params[:where]
@@ -91,12 +95,16 @@ module GraphQL
91
95
  arg
92
96
  end
93
97
 
98
+ unless @result.has_attribute?(attribute)
99
+ raise ArgumentError, "Unable to resolve attribute #{attribute} on #{@result}"
100
+ end
101
+
94
102
  hash = {}
95
103
  hash[attribute] = value
96
104
  @result = @result.where(hash)
105
+ else
106
+ raise ArgumentError, "Unable to resolve argument #{arg} in #{self}"
97
107
  end
98
- else
99
- raise ArgumentError, "Unable to resolve argument #{arg} in #{self}"
100
108
  end
101
109
  end
102
110
  end
@@ -188,6 +196,16 @@ module GraphQL
188
196
  end
189
197
  end
190
198
 
199
+ def condition_met?(conditional, expectation, value)
200
+ if conditional.respond_to? :call
201
+ conditional.call(value) == expectation
202
+ elsif (conditional.is_a?(Symbol) || conditional.is_a?(String)) && self.respond_to?(conditional)
203
+ self.send(conditional, value) == expectation
204
+ else
205
+ true
206
+ end
207
+ end
208
+
191
209
  class << self
192
210
  @@id_field = :id
193
211
 
@@ -201,28 +219,28 @@ module GraphQL
201
219
  @resolvers
202
220
  end
203
221
 
204
- def resolve(field, definition=nil, **otherArgs)
222
+ def resolve(arg, definition=nil, **otherArgs)
205
223
  @resolvers ||= {}
206
- @resolvers[field] ||= []
207
- @resolvers[field].push([definition, otherArgs])
224
+ @resolvers[arg] ||= []
225
+ @resolvers[arg].push([definition, otherArgs])
208
226
  end
209
227
 
210
- def resolve_where(field)
228
+ def resolve_where(arg)
211
229
  warn "[DEPRECATION] `resolve_where` is deprecated. Please use `resolve` instead."
212
- resolve(field)
230
+ resolve(arg)
213
231
  end
214
232
 
215
- def resolve_scope(field, test=nil, scope_name: nil, with_value: false)
233
+ def resolve_scope(arg, test=nil, scope_name: nil, with_value: false)
216
234
  warn "[DEPRECATION] `resolve_scope` is deprecated. Please use `resolve` instead."
217
235
  test = lambda { |value| value.present? } if test.nil?
218
- scope_name = field if scope_name.nil?
236
+ scope_name = arg if scope_name.nil?
219
237
 
220
- resolve(field, :scope => -> (value) { test.call(value) ? scope_name : nil }, :with_value => with_value)
238
+ resolve(arg, :scope => -> (value) { test.call(value) ? scope_name : nil }, :with_value => with_value)
221
239
  end
222
240
 
223
- def resolve_method(field)
241
+ def resolve_method(arg)
224
242
  warn "[DEPRECATION] `resolve_method` is deprecated. Please use `resolve` instead."
225
- resolve(field)
243
+ resolve(arg)
226
244
  end
227
245
  end
228
246
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-rails-resolver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cole Turner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-13 00:00:00.000000000 Z
11
+ date: 2016-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.19.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.19.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: A utility for ease graphql-ruby integration into a Rails project.
@@ -44,11 +44,11 @@ executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
- - CHANGELOG.md
47
+ - lib/graphql/rails/resolver.rb
48
+ - lib/graphql/rails.rb
48
49
  - LICENSE
49
50
  - README.md
50
- - lib/graphql/rails.rb
51
- - lib/graphql/rails/resolver.rb
51
+ - CHANGELOG.md
52
52
  homepage: http://rubygems.org/gems/graphql-rails-resolver
53
53
  licenses:
54
54
  - MIT
@@ -59,17 +59,17 @@ require_paths:
59
59
  - lib
60
60
  required_ruby_version: !ruby/object:Gem::Requirement
61
61
  requirements:
62
- - - ">="
62
+ - - '>='
63
63
  - !ruby/object:Gem::Version
64
64
  version: 2.3.0
65
65
  required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ">="
67
+ - - '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  requirements: []
71
71
  rubyforge_project:
72
- rubygems_version: 2.4.5.1
72
+ rubygems_version: 2.0.14.1
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: GraphQL + Rails integration for Field Resolvers.