graphiform 0.1.2 → 0.1.3

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: d348d5da31c862403a9127ada31cbb135e892c21928ca48b8ccfb5eb2d64d047
4
- data.tar.gz: 73db372bee299b12a56e219218d04c4977436f3457379def8d0dd952a5e97aec
3
+ metadata.gz: d6bde49d1fc203265aee73785f6ff23308b5d2098259dd7bbc762dc0a8c16322
4
+ data.tar.gz: 061af2e25afdf9dad9fdedab5847212e3b7005c36290dfcbccdc73b085dd1588
5
5
  SHA512:
6
- metadata.gz: b22a9b8d9e6b612406ab06a1d8fc86e8ad9e6305a8f1d222d7dc36a08f72cf95d911445ab5e2c1d852714f565b8d8758b6f3c2b3a1ebb1e62199a11f1a4880cb
7
- data.tar.gz: c5766eb8d166b5311189ede79a26e21b5d55155199f24f995a763c9ddcc6540a5973bf360e1f5edb6ed75959085661f597e81946102a04b9022ad2ada48f5a61
6
+ metadata.gz: 0df7c307f8fbaa68fa1a33ee60d9c364603dbd911cb3726fc20073cf1bb9decaf1809387f5048fa080cd4abb6908f51fd744c769cbbb5f8c1e88db7fe631a7a7
7
+ data.tar.gz: df1bc69c8e412ae66be318d65c5574fef2cf1d5d2371a98ddd1293a6ceea9e7b4ac6e3bcd794b1660e8c7a6e7fbe26aa97737b723e5f4d3d5b22ad256e42e8ee
@@ -130,7 +130,7 @@ module Graphiform
130
130
  end
131
131
  end
132
132
 
133
- def graphql_create_resolver(method_name, resolver_type = graphql_type)
133
+ def graphql_create_resolver(method_name, resolver_type = graphql_type, read_prepare: nil, **_options)
134
134
  Class.new(graphql_base_resolver) do
135
135
  type resolver_type, null: false
136
136
 
@@ -140,6 +140,7 @@ module Graphiform
140
140
  val = super(**args)
141
141
 
142
142
  val = val.public_send(method_name) if val.respond_to? method_name
143
+ val = instance_exec(val, context, &read_prepare) if read_prepare
143
144
 
144
145
  return val.apply_filters(where_hash) if val.respond_to? :apply_filters
145
146
 
@@ -93,6 +93,8 @@ module Graphiform
93
93
  end
94
94
 
95
95
  def graphql_resolve_argument_type(name, type)
96
+ type = graphql_create_enum(name) if type.blank? && enum_attribute?(name)
97
+
96
98
  return Helpers.graphql_type(type) if type.present?
97
99
 
98
100
  column_def = column(name)
@@ -203,19 +205,28 @@ module Graphiform
203
205
  graphql_type.class_eval do
204
206
  added_field = field(field_name, **field_options)
205
207
 
206
- define_method(added_field.method_sym, -> { read_prepare.call(object.public_send(added_field.method_sym)) }) if read_prepare
208
+ if read_prepare
209
+ define_method(
210
+ added_field.method_sym,
211
+ -> { instance_exec(object.public_send(added_field.method_sym), context, &read_prepare) }
212
+ )
213
+ end
207
214
  end
208
215
  end
209
216
 
210
217
  def graphql_add_column_field(field_name, column_def, type: nil, null: nil, **options)
211
- type = :string if type.blank? && enum_attribute?(field_name)
218
+ is_enum = type.blank? && enum_attribute?(field_name)
219
+ if is_enum
220
+ enum = graphql_create_enum(field_name)
221
+ type = enum
222
+ end
212
223
  type ||= column_def.type
213
224
  null = column_def.null if null.nil?
214
225
 
215
226
  graphql_add_field_to_type(field_name, type, null, **options)
216
227
  end
217
228
 
218
- def graphql_add_association_field(field_name, association_def, type: nil, null: nil, **options)
229
+ def graphql_add_association_field(field_name, association_def, type: nil, null: nil, include_connection: true, read_prepare: nil, **options)
219
230
  unless association_def.klass.respond_to?(:graphql_type)
220
231
  return Helpers.logger.warn(
221
232
  "Graphiform: `#{name}` trying to add association `#{field_name}` - `#{association_def.klass.name}` does not include Graphiform"
@@ -231,19 +242,23 @@ module Graphiform
231
242
  has_many = association_def.macro == :has_many
232
243
  klass = association_def.klass
233
244
 
234
- if has_many
245
+ if include_connection && has_many
235
246
  graphql_add_field_to_type(
236
247
  "#{field_name}_connection",
237
- klass.graphql_create_resolver(association_def.name, graphql_connection),
248
+ klass.graphql_create_resolver(association_def.name, klass.graphql_connection, read_prepare: read_prepare),
238
249
  false,
239
250
  **options
240
251
  )
241
252
  end
242
253
 
243
- type = has_many ? klass.graphql_create_resolver(association_def.name, [klass.graphql_type]) : klass.graphql_type if type.nil?
254
+ if type.nil?
255
+ type = has_many ? klass.graphql_create_resolver(association_def.name, [klass.graphql_type], read_prepare: read_prepare) : klass.graphql_type
256
+ read_prepare = nil if has_many
257
+ end
258
+
244
259
  null = association_def.macro != :has_many if null.nil?
245
260
 
246
- graphql_add_field_to_type(field_name, type, null, **options)
261
+ graphql_add_field_to_type(field_name, type, null, read_prepare: read_prepare, **options)
247
262
  end
248
263
 
249
264
  def graphql_add_method_field(field_name, type:, null: true, **options)
@@ -1,3 +1,3 @@
1
1
  module Graphiform
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.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.1.2
4
+ version: 0.1.3
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-03-18 00:00:00.000000000 Z
11
+ date: 2020-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.2.0
47
+ version: 0.2.2
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.2.0
54
+ version: 0.2.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: appraisal
57
57
  requirement: !ruby/object:Gem::Requirement