graphiform 0.2.0 → 0.2.5

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: f568ee911e1cd7c4dc85decd8913292d3abcfb2d6038ff247e7a99b861168060
4
- data.tar.gz: 74eef82837648da958be37d5bd8672a03e27b618321a9ef0ea6cf6ee440eb164
3
+ metadata.gz: 2f56c92956905df19b6e8a756b8826c63710619ab9c67f5e019280ccca4c0cd5
4
+ data.tar.gz: 6ac0fbb69feaa5d7a544dc1c5618f7c09b9cfd09fa6a0ad498e139e2c7539547
5
5
  SHA512:
6
- metadata.gz: 6e42cf06adab34fb2db93633578b2418b8127749a16b25488fc9a8abd8ea3b2b529d49310b8cb2e437c7f04e1812242b40b530b443131dd8e489b6b85d79cb03
7
- data.tar.gz: 656299a9d3c659197dc5650e93bea7f193043e1f6d2e0690a6e1c9e40a9b83111e3e20560fb8f57bde32860d91bdf63407a7b1ab9afff6a38be2b57fd6f2b916
6
+ metadata.gz: 8fe2ea6fc9d0c5bd906357d7571b2aa187c247a2fccd573716da623e22f784c3aaf4337fae660ec5b30061a40af57ac99f2cd1ec68b93c35cf06b8098196da3d
7
+ data.tar.gz: ee62442607c271725f12401d2d27f89667ef3992c41e7e09935809e5c2ee77c8aa99986d69a60ddacb7b0ab439717a47dd803a7579f3749514fca572fc440b15
@@ -136,7 +136,7 @@ module Graphiform
136
136
  Helpers.get_const_or_create(demodulized_name, ::Resolvers::Queries) do
137
137
  local_graphql_type = graphql_type
138
138
  Class.new(graphql_base_resolver) do
139
- type local_graphql_type, null: false
139
+ type local_graphql_type, null: true
140
140
 
141
141
  def base_resolve(**args)
142
142
  @value = model.all
@@ -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, null: true, **)
165
165
  Class.new(graphql_base_resolver) do
166
- type resolver_type, null: false
166
+ type resolver_type, null: null
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)
@@ -9,17 +9,18 @@ module Graphiform
9
9
  module ClassMethods
10
10
  def graphql_readable_field(
11
11
  name,
12
+ as: nil,
12
13
  **options
13
14
  )
14
- column_def = column(name)
15
- association_def = association(name)
15
+ column_def = column(as || name)
16
+ association_def = association(as || name)
16
17
 
17
- graphql_add_column_field(name, column_def, **options) if column_def.present?
18
- graphql_add_association_field(name, association_def, **options) if association_def.present?
19
- graphql_add_method_field(name, **options) unless column_def.present? || association_def.present?
18
+ graphql_add_column_field(name, column_def, as: as, **options) if column_def.present?
19
+ graphql_add_association_field(name, association_def, as: as, **options) if association_def.present?
20
+ graphql_add_method_field(name, as: as, **options) unless column_def.present? || association_def.present?
20
21
 
21
- graphql_add_scopes_to_filter(name)
22
- graphql_field_to_sort(name)
22
+ graphql_add_scopes_to_filter(name, as || name)
23
+ graphql_field_to_sort(name, as || name)
23
24
  end
24
25
 
25
26
  def graphql_writable_field(
@@ -31,25 +32,30 @@ module Graphiform
31
32
  description: nil,
32
33
  default_value: ::GraphQL::Schema::Argument::NO_DEFAULT,
33
34
  as: nil,
34
- **_options
35
+ **
35
36
  )
36
37
  name = name.to_sym
37
- argument_name = graphql_resolve_argument_name(name)
38
- argument_type = graphql_resolve_argument_type(name, type)
38
+ has_nested_attributes_method = instance_methods.include?("#{as || name}_attributes=".to_sym)
39
+
40
+ argument_name = has_nested_attributes_method ? "#{name}_attributes".to_sym : name
41
+ argument_type = graphql_resolve_argument_type(as || name, type)
42
+ as = has_nested_attributes_method ? "#{as}_attributes".to_sym : as.to_sym if as
39
43
 
40
- 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?
41
45
 
42
46
  prepare = write_prepare || prepare
43
47
 
44
48
  graphql_input.class_eval do
45
- argument \
49
+ argument(
46
50
  argument_name,
47
51
  argument_type,
48
52
  required: required,
49
53
  prepare: prepare,
50
54
  description: description,
51
55
  default_value: default_value,
52
- as: as
56
+ as: as,
57
+ method_access: false
58
+ )
53
59
  end
54
60
  end
55
61
 
@@ -84,14 +90,6 @@ module Graphiform
84
90
 
85
91
  private
86
92
 
87
- def graphql_resolve_argument_name(name)
88
- attributes_name = "#{name}_attributes"
89
-
90
- return attributes_name.to_sym if instance_methods.include?("#{attributes_name}=".to_sym)
91
-
92
- name
93
- end
94
-
95
93
  def graphql_resolve_argument_type(name, type)
96
94
  type = graphql_create_enum(name) if type.blank? && enum_attribute?(name)
97
95
 
@@ -109,12 +107,12 @@ module Graphiform
109
107
  has_many ? [association_def.klass.graphql_input] : association_def.klass.graphql_input
110
108
  end
111
109
 
112
- def graphql_add_scopes_to_filter(name)
113
- added_scopes = auto_scopes_by_attribute(name)
110
+ def graphql_add_scopes_to_filter(name, as)
111
+ added_scopes = auto_scopes_by_attribute(as)
114
112
 
115
113
  return if added_scopes.empty?
116
114
 
117
- association_def = association(name)
115
+ association_def = association(as)
118
116
 
119
117
  if association_def.present?
120
118
  return unless Helpers.association_arguments_valid?(association_def, :graphql_filter)
@@ -130,15 +128,15 @@ module Graphiform
130
128
  enum = graphql_create_enum(name)
131
129
  scope_argument_type = scope_argument_type.is_a?(Array) ? [enum] : enum
132
130
  end
133
- add_scope_def_to_filter(added_scope, scope_argument_type)
131
+ add_scope_def_to_filter(name, added_scope, scope_argument_type)
134
132
  end
135
133
  end
136
134
 
137
- def add_scope_def_to_filter(scope_def, argument_type)
135
+ def add_scope_def_to_filter(name, scope_def, argument_type)
138
136
  return unless argument_type
139
137
 
140
138
  argument_type = Helpers.graphql_type(argument_type)
141
- argument_attribute = scope_def.attribute
139
+ argument_attribute = name
142
140
  argument_prefix = scope_def.prefix
143
141
  argument_suffix = scope_def.suffix == '_is' ? '' : scope_def.suffix
144
142
  argument_name = "#{argument_prefix}#{argument_attribute}#{argument_suffix}".underscore
@@ -149,14 +147,15 @@ module Graphiform
149
147
  argument_name,
150
148
  argument_type,
151
149
  required: false,
152
- as: scope_name
150
+ as: scope_name,
151
+ method_access: false
153
152
  )
154
153
  end
155
154
  end
156
155
 
157
- def graphql_field_to_sort(name)
158
- column_def = column(name)
159
- association_def = association(name)
156
+ def graphql_field_to_sort(name, as)
157
+ column_def = column(as || name)
158
+ association_def = association(as || name)
160
159
 
161
160
  type = ::Enums::Sort if column_def.present?
162
161
  type = association_def.klass.graphql_sort if Helpers.association_arguments_valid?(association_def, :graphql_sort)
@@ -169,7 +168,9 @@ module Graphiform
169
168
  argument(
170
169
  name,
171
170
  type,
172
- required: false
171
+ required: false,
172
+ as: as,
173
+ method_access: false
173
174
  )
174
175
  end
175
176
  end
@@ -181,18 +182,21 @@ module Graphiform
181
182
  description: nil,
182
183
  deprecation_reason: nil,
183
184
  method: nil,
185
+ as: nil,
184
186
  read_prepare: nil,
185
- **_options
187
+ read_resolve: nil,
188
+ **
186
189
  )
190
+ type = Helpers.graphql_type(type)
191
+ is_resolver = Helpers.resolver?(type)
192
+
187
193
  field_name = field_name.to_sym
188
194
  field_options = {
189
195
  description: description,
190
196
  deprecation_reason: deprecation_reason,
191
- method: method,
197
+ method: is_resolver ? nil : method || as,
192
198
  }
193
199
 
194
- type = Helpers.graphql_type(type)
195
-
196
200
  if Helpers.resolver?(type)
197
201
  field_options[:resolver] = type
198
202
  else
@@ -203,28 +207,42 @@ module Graphiform
203
207
  graphql_type.class_eval do
204
208
  added_field = field(field_name, **field_options)
205
209
 
206
- if read_prepare
210
+ if read_prepare || read_resolve
207
211
  define_method(
208
212
  added_field.method_sym,
209
- -> { 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
+ value = instance_exec(value, context, &read_prepare) if read_prepare
216
+
217
+ value
218
+ end
210
219
  )
211
220
  end
212
221
  end
213
222
  end
214
223
 
215
- def graphql_add_column_field(field_name, column_def, type: nil, null: nil, **options)
216
- is_enum = type.blank? && enum_attribute?(field_name)
224
+ def graphql_add_column_field(field_name, column_def, type: nil, null: nil, as: nil, **options)
225
+ is_enum = type.blank? && enum_attribute?(as || field_name)
217
226
  if is_enum
218
- enum = graphql_create_enum(field_name)
227
+ enum = graphql_create_enum(as || field_name)
219
228
  type = enum
220
229
  end
221
230
  type ||= column_def.type
222
231
  null = column_def.null if null.nil?
223
232
 
224
- graphql_add_field_to_type(field_name, type, null, **options)
233
+ graphql_add_field_to_type(field_name, type, null, as: as, **options)
225
234
  end
226
235
 
227
- def graphql_add_association_field(field_name, association_def, type: nil, null: nil, include_connection: true, read_prepare: nil, **options)
236
+ def graphql_add_association_field(
237
+ field_name,
238
+ association_def,
239
+ type: nil,
240
+ null: nil,
241
+ include_connection: true,
242
+ read_prepare: nil,
243
+ read_resolve: nil,
244
+ **options
245
+ )
228
246
  unless association_def.klass.respond_to?(:graphql_type)
229
247
  return Helpers.logger.warn(
230
248
  "Graphiform: `#{name}` trying to add association `#{field_name}` - `#{association_def.klass.name}` does not include Graphiform"
@@ -243,20 +261,42 @@ module Graphiform
243
261
  if include_connection && has_many
244
262
  graphql_add_field_to_type(
245
263
  "#{field_name}_connection",
246
- klass.graphql_create_resolver(association_def.name, klass.graphql_connection, read_prepare: read_prepare),
264
+ klass.graphql_create_resolver(
265
+ association_def.name,
266
+ klass.graphql_connection,
267
+ read_prepare: read_prepare,
268
+ read_resolve: read_resolve,
269
+ null: false
270
+ ),
247
271
  false,
248
272
  **options
249
273
  )
250
274
  end
251
275
 
252
276
  if type.nil?
253
- type = has_many ? klass.graphql_create_resolver(association_def.name, [klass.graphql_type], read_prepare: read_prepare) : klass.graphql_type
254
- read_prepare = nil if has_many
277
+ type = (
278
+ if has_many
279
+ klass.graphql_create_resolver(
280
+ association_def.name,
281
+ [klass.graphql_type],
282
+ read_prepare: read_prepare,
283
+ read_resolve: read_resolve,
284
+ null: false
285
+ )
286
+ else
287
+ klass.graphql_type
288
+ end
289
+ )
290
+
291
+ if has_many
292
+ read_prepare = nil
293
+ read_resolve = nil
294
+ end
255
295
  end
256
296
 
257
297
  null = association_def.macro != :has_many if null.nil?
258
298
 
259
- graphql_add_field_to_type(field_name, type, null, read_prepare: read_prepare, **options)
299
+ graphql_add_field_to_type(field_name, type, null, read_prepare: read_prepare, read_resolve: read_resolve, **options)
260
300
  end
261
301
 
262
302
  def graphql_add_method_field(field_name, type: nil, null: true, **options)
@@ -1,3 +1,3 @@
1
1
  module Graphiform
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.2.5'.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.0
4
+ version: 0.2.5
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-04-30 00:00:00.000000000 Z
11
+ date: 2020-08-02 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.2
47
+ version: 0.2.3
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.2
54
+ version: 0.2.3
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: appraisal
57
57
  requirement: !ruby/object:Gem::Requirement