graphiform 0.1.4 → 0.2.4

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: 5ad540c4e174f075ab85b85f40e4dd63ebb4568ff0617b3c5dd6785410f55c63
4
- data.tar.gz: a6b5a49c31a806ff942a87fef6ef4d1554f2cb9fc9867ad442f99501fc736025
3
+ metadata.gz: ad1663df91a2cb043ecca554ce73964f977b6a5af2878334df72105177bddb04
4
+ data.tar.gz: f70a256ef76ce3ba4d91d25d2194bc1481d3c89e601a1b48a9f1ef6fe09bafd4
5
5
  SHA512:
6
- metadata.gz: 2bb21cd22cdcecb06d9ff58c32ba0697cecf604bc8a0eb162fe846d0cd37dec604c082bd3d39bc744f8f418372629b7883d71da2e8cf5fc35bfe68e8a26b2639
7
- data.tar.gz: d2abcff1ee1e4dd8e2b7703b5d73864082ce554895e0b6d34fc118afb2b11d942789de11af26410b6c82230dcfd71a1a96d22f7503dd3f3d7b46dc5a209bd324
6
+ metadata.gz: 2e77bbf7632faeb956acd2f7aa067428b7d2375f3c96b1ca71af95bd1d5ac12b5a8aa2d4229a9718c5dce7b566cd077f1018c84b5a251969a6a6579654cf07bb
7
+ data.tar.gz: 8ecfa9e57aa9b7692236114ff695aa5fad197a7c608127e93fca500b1a94a7ff7156cd093f1ac2a5d28438f93b86e4d1cdda4deb61186a5802212df216434d29
@@ -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,19 +128,18 @@ 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
- camelized_attribute = scope_def.attribute.to_s.camelize(:lower)
139
+ argument_attribute = name
142
140
  argument_prefix = scope_def.prefix
143
- argument_suffix = scope_def.suffix
144
- argument_suffix = argument_suffix == '_is' ? '' : argument_suffix
145
- argument_name = "#{argument_prefix}#{camelized_attribute}#{argument_suffix}"
141
+ argument_suffix = scope_def.suffix == '_is' ? '' : scope_def.suffix
142
+ argument_name = "#{argument_prefix}#{argument_attribute}#{argument_suffix}".underscore
146
143
  scope_name = scope_def.name
147
144
 
148
145
  graphql_filter.class_eval do
@@ -150,15 +147,15 @@ module Graphiform
150
147
  argument_name,
151
148
  argument_type,
152
149
  required: false,
153
- camelize: false,
154
- as: scope_name
150
+ as: scope_name,
151
+ method_access: false
155
152
  )
156
153
  end
157
154
  end
158
155
 
159
- def graphql_field_to_sort(name)
160
- column_def = column(name)
161
- association_def = association(name)
156
+ def graphql_field_to_sort(name, as)
157
+ column_def = column(as || name)
158
+ association_def = association(as || name)
162
159
 
163
160
  type = ::Enums::Sort if column_def.present?
164
161
  type = association_def.klass.graphql_sort if Helpers.association_arguments_valid?(association_def, :graphql_sort)
@@ -171,7 +168,9 @@ module Graphiform
171
168
  argument(
172
169
  name,
173
170
  type,
174
- required: false
171
+ required: false,
172
+ as: as,
173
+ method_access: false
175
174
  )
176
175
  end
177
176
  end
@@ -183,18 +182,21 @@ module Graphiform
183
182
  description: nil,
184
183
  deprecation_reason: nil,
185
184
  method: nil,
185
+ as: nil,
186
186
  read_prepare: nil,
187
- **_options
187
+ read_resolve: nil,
188
+ **
188
189
  )
190
+ type = Helpers.graphql_type(type)
191
+ is_resolver = Helpers.resolver?(type)
192
+
189
193
  field_name = field_name.to_sym
190
194
  field_options = {
191
195
  description: description,
192
196
  deprecation_reason: deprecation_reason,
193
- method: method,
197
+ method: is_resolver ? nil : method || as,
194
198
  }
195
199
 
196
- type = Helpers.graphql_type(type)
197
-
198
200
  if Helpers.resolver?(type)
199
201
  field_options[:resolver] = type
200
202
  else
@@ -205,28 +207,42 @@ module Graphiform
205
207
  graphql_type.class_eval do
206
208
  added_field = field(field_name, **field_options)
207
209
 
208
- if read_prepare
210
+ if read_prepare || read_resolve
209
211
  define_method(
210
212
  added_field.method_sym,
211
- -> { 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
212
219
  )
213
220
  end
214
221
  end
215
222
  end
216
223
 
217
- def graphql_add_column_field(field_name, column_def, type: nil, null: nil, **options)
218
- 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)
219
226
  if is_enum
220
- enum = graphql_create_enum(field_name)
227
+ enum = graphql_create_enum(as || field_name)
221
228
  type = enum
222
229
  end
223
230
  type ||= column_def.type
224
231
  null = column_def.null if null.nil?
225
232
 
226
- graphql_add_field_to_type(field_name, type, null, **options)
233
+ graphql_add_field_to_type(field_name, type, null, as: as, **options)
227
234
  end
228
235
 
229
- 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
+ )
230
246
  unless association_def.klass.respond_to?(:graphql_type)
231
247
  return Helpers.logger.warn(
232
248
  "Graphiform: `#{name}` trying to add association `#{field_name}` - `#{association_def.klass.name}` does not include Graphiform"
@@ -245,20 +261,42 @@ module Graphiform
245
261
  if include_connection && has_many
246
262
  graphql_add_field_to_type(
247
263
  "#{field_name}_connection",
248
- 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
+ ),
249
271
  false,
250
272
  **options
251
273
  )
252
274
  end
253
275
 
254
276
  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
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
257
295
  end
258
296
 
259
297
  null = association_def.macro != :has_many if null.nil?
260
298
 
261
- 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)
262
300
  end
263
301
 
264
302
  def graphql_add_method_field(field_name, type: nil, null: true, **options)
@@ -1,3 +1,3 @@
1
1
  module Graphiform
2
- VERSION = '0.1.4'.freeze
2
+ VERSION = '0.2.4'.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.4
4
+ version: 0.2.4
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-29 00:00:00.000000000 Z
11
+ date: 2020-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord