graphiform 0.2.1 → 0.2.2

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: 526c94bd74004084cc768835a9053591a755f8eccfdebefae6ed4bd44a972399
4
- data.tar.gz: 913464048608818458ede08de55cfdda5c8cc3cd4855ee98bbb44763d1c1423b
3
+ metadata.gz: 76a4ecba322d7e4c1f2486f621531307b184bf42f52827023ef5f11942b4bcbc
4
+ data.tar.gz: ed8815feff1a1eaba966dd44b5db53f1739fce88f0d0e9fb7a850dda522d74bc
5
5
  SHA512:
6
- metadata.gz: 0e8eb91cf3db0dd05c907b9e2d5cd31d5a262a939c491ae6d7b791b9ac4bee9853c80475d031ee2fdfb38e944ea35fa0d80c2af6bf15061045a9b0c3b674bbec
7
- data.tar.gz: 9fc6e350f1b2603a3e8205e5ce4d762f7107959040ac9cee557e912c52891cca1e315ffe0a86847538ab14aaa659f9842bcaabde018915ed970240aab619125d
6
+ metadata.gz: 412b372b015026737c993a1990031222c066272c5b9aec2563cd95e1474f5d2637d0a0ede3d3e589982ccb161b4b62d18c8380b117548e33221172689e1a5a41
7
+ data.tar.gz: b2c489a6c8f250f535657b5324aed7aa341365fa39540908ce2eabb6c403b793f71863cabfa63b6313ce60aab43731c94fc82492f7105e00d162a5eca0e1c96e
@@ -161,14 +161,15 @@ module Graphiform
161
161
  end
162
162
  end
163
163
 
164
- def graphql_create_resolver(method_name, resolver_type = graphql_type, read_prepare: nil, **)
164
+ def graphql_create_resolver(method_name, resolver_type = graphql_type, read_prepare: nil, read_resolve: nil, **)
165
165
  Class.new(graphql_base_resolver) do
166
166
  type resolver_type, null: false
167
167
 
168
168
  define_method :base_resolve do |**args|
169
169
  @value = object
170
170
 
171
- @value = @value.public_send(method_name) if @value.respond_to?(method_name)
171
+ @value = instance_exec(@value, context, &read_resolve) if read_resolve
172
+ @value = @value.public_send(method_name) if !read_resolve && @value.respond_to?(method_name)
172
173
  @value = instance_exec(@value, context, &read_prepare) if read_prepare
173
174
 
174
175
  apply_built_ins(**args)
@@ -41,7 +41,7 @@ module Graphiform
41
41
  argument_type = graphql_resolve_argument_type(as || name, type)
42
42
  as = has_nested_attributes_method ? "#{as}_attributes".to_sym : as.to_sym if as
43
43
 
44
- return Helpers.logger.warn "Graphiform: Missing `type` for argument #{name}" if argument_type.nil?
44
+ return Helpers.logger.warn "Graphiform: Missing `type` for argument `#{name}` in model `#{self.name}`" if argument_type.nil?
45
45
 
46
46
  prepare = write_prepare || prepare
47
47
 
@@ -184,6 +184,7 @@ module Graphiform
184
184
  method: nil,
185
185
  as: nil,
186
186
  read_prepare: nil,
187
+ read_resolve: nil,
187
188
  **
188
189
  )
189
190
  type = Helpers.graphql_type(type)
@@ -206,10 +207,13 @@ module Graphiform
206
207
  graphql_type.class_eval do
207
208
  added_field = field(field_name, **field_options)
208
209
 
209
- if read_prepare
210
+ if read_prepare || read_resolve
210
211
  define_method(
211
212
  added_field.method_sym,
212
- -> { instance_exec(object.public_send(added_field.method_sym), context, &read_prepare) }
213
+ lambda do
214
+ value = read_resolve ? instance_exec(object, context, &read_resolve) : object.public_send(added_field.method_sym)
215
+ instance_exec(value, context, &read_prepare) if read_prepare
216
+ end
213
217
  )
214
218
  end
215
219
  end
@@ -227,7 +231,16 @@ module Graphiform
227
231
  graphql_add_field_to_type(field_name, type, null, as: as, **options)
228
232
  end
229
233
 
230
- def graphql_add_association_field(field_name, association_def, type: nil, null: nil, include_connection: true, read_prepare: nil, **options)
234
+ def graphql_add_association_field(
235
+ field_name,
236
+ association_def,
237
+ type: nil,
238
+ null: nil,
239
+ include_connection: true,
240
+ read_prepare: nil,
241
+ read_resolve: nil,
242
+ **options
243
+ )
231
244
  unless association_def.klass.respond_to?(:graphql_type)
232
245
  return Helpers.logger.warn(
233
246
  "Graphiform: `#{name}` trying to add association `#{field_name}` - `#{association_def.klass.name}` does not include Graphiform"
@@ -246,20 +259,40 @@ module Graphiform
246
259
  if include_connection && has_many
247
260
  graphql_add_field_to_type(
248
261
  "#{field_name}_connection",
249
- klass.graphql_create_resolver(association_def.name, klass.graphql_connection, read_prepare: read_prepare),
262
+ klass.graphql_create_resolver(
263
+ association_def.name,
264
+ klass.graphql_connection,
265
+ read_prepare: read_prepare,
266
+ read_resolve: read_resolve
267
+ ),
250
268
  false,
251
269
  **options
252
270
  )
253
271
  end
254
272
 
255
273
  if type.nil?
256
- type = has_many ? klass.graphql_create_resolver(association_def.name, [klass.graphql_type], read_prepare: read_prepare) : klass.graphql_type
257
- read_prepare = nil if has_many
274
+ type = (
275
+ if has_many
276
+ klass.graphql_create_resolver(
277
+ association_def.name,
278
+ [klass.graphql_type],
279
+ read_prepare: read_prepare,
280
+ read_resolve: read_resolve
281
+ )
282
+ else
283
+ klass.graphql_type
284
+ end
285
+ )
286
+
287
+ if has_many
288
+ read_prepare = nil
289
+ read_resolve = nil
290
+ end
258
291
  end
259
292
 
260
293
  null = association_def.macro != :has_many if null.nil?
261
294
 
262
- graphql_add_field_to_type(field_name, type, null, read_prepare: read_prepare, **options)
295
+ graphql_add_field_to_type(field_name, type, null, read_prepare: read_prepare, read_resolve: read_resolve, **options)
263
296
  end
264
297
 
265
298
  def graphql_add_method_field(field_name, type: nil, null: true, **options)
@@ -1,3 +1,3 @@
1
1
  module Graphiform
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '0.2.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphiform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - jayce.pulsipher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-15 00:00:00.000000000 Z
11
+ date: 2020-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord