hq-graphql 1.0.0 → 1.0.1

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
  SHA1:
3
- metadata.gz: b25a73fce273a48a00a185766cae51a9ec2e3150
4
- data.tar.gz: 7c325eaef387c340778954fb83dbb3d0ccac9c03
3
+ metadata.gz: ab6e40470755691d1fe10f268c3652f19ad894ec
4
+ data.tar.gz: fb75a2bfa83aed220ef40b5461429923dd5f56f5
5
5
  SHA512:
6
- metadata.gz: da27072503a7751c1eefa68ebdf6a0fcb77b28a6213fb8fb6a4d7fdec32d9f0bd14dfb585d0196f4d1119cd2d5ecb8d33e54c7c1e902051612e3fb7d9e2ab815
7
- data.tar.gz: c5b52d4d4d8af7fe53b1c1aa50ad28b62eb36b66c2f39de2e6b4a51add6505c110b3e7d6182f0efcb302d53ca70c78ccf1d5817206eef413635d033832cc10af
6
+ metadata.gz: cce3a3348a0cee0faf0c4edd5055296cdd4907aadfb953e36615bca342e203fcf70a05f83a84754cc05fef1c3756d94e2c3e97addce552adbc535c733f1035e2
7
+ data.tar.gz: 9641b93d8845695db06a950b9236f0566df6371ba5c1c960ddd4ec6422cb60ecd369be6b3678595529bbeae1495402a69868e9107b5f7591055d9e226eec4a63
@@ -38,7 +38,7 @@ module HQ
38
38
  # validate removed_attributes exist
39
39
  removed_attributes.each { |attr| column_from_model(attr) }
40
40
 
41
- model_columns.reject { |c| removed_attributes.include?(c.name.to_sym) }
41
+ model_columns.reject { |c| removed_attributes.include?(c.name.to_sym) }.sort_by(&:name)
42
42
  end
43
43
 
44
44
  def model_associations
@@ -52,7 +52,7 @@ module HQ
52
52
  # validate removed_associations exist
53
53
  removed_associations.each { |association| association_from_model(association) }
54
54
 
55
- model_associations.reject { |a| removed_associations.include?(a.name.to_sym) }
55
+ model_associations.reject { |a| removed_associations.include?(a.name.to_sym) }.sort_by(&:name)
56
56
  end
57
57
 
58
58
  private
@@ -1,15 +1,78 @@
1
1
  module HQ
2
2
  module GraphQL
3
3
  class InputObject < ::GraphQL::Schema::InputObject
4
- include ::HQ::GraphQL::InputExtensions
4
+ include Scalars
5
+ include ::HQ::GraphQL::ActiveRecordExtensions
6
+
7
+ # Recursively format attributes so that they are compatible with `accepts_nested_attributes_for`
8
+ def format_nested_attributes
9
+ self.each.inject({}) do |formatted_attrs, (key, value) |
10
+ if self.class.nested_attributes.include?(key.to_s)
11
+ formatted_attrs["#{key}_attributes"] = value.format_nested_attributes
12
+ else
13
+ formatted_attrs[key] = value
14
+ end
15
+ formatted_attrs
16
+ end
17
+ end
18
+
19
+ def with_indifferent_access
20
+ to_h.with_indifferent_access
21
+ end
22
+
23
+ #### Class Methods ####
24
+
25
+ def self.with_model(model_name, attributes: true, associations: false)
26
+ self.model_name = model_name
27
+ self.auto_load_attributes = attributes
28
+ self.auto_load_associations = associations
29
+
30
+ lazy_load do
31
+ model_columns.each do |column|
32
+ argument_from_column(column)
33
+ end
34
+
35
+ model_associations.each do |association|
36
+ argument_from_association association
37
+ end
38
+ end
39
+ end
40
+
41
+ def self.nested_attributes
42
+ @nested_attributes ||= Set.new
43
+ end
5
44
 
6
45
  def self.to_graphql
7
46
  lazy_load!
8
47
  super
9
48
  end
10
49
 
11
- def with_indifferent_access
12
- to_h.with_indifferent_access
50
+ class << self
51
+ private
52
+
53
+ def argument_from_association(association)
54
+ input = ::HQ::GraphQL::Inputs[association.klass]
55
+ name = association.name
56
+
57
+ case association.macro
58
+ when :has_many
59
+ argument name, [input], required: false
60
+ else
61
+ argument name, input, required: false
62
+ end
63
+
64
+ if !model_klass.nested_attributes_options.keys.include?(name.to_sym)
65
+ model_klass.accepts_nested_attributes_for name, allow_destroy: true
66
+ end
67
+
68
+ nested_attributes << name.to_s
69
+ rescue ::HQ::GraphQL::Inputs::Error
70
+ nil
71
+ end
72
+
73
+ def argument_from_column(column)
74
+ argument column.name, ::HQ::GraphQL::Types.type_from_column(column), required: false
75
+ end
13
76
  end
14
77
 
15
78
  end
@@ -1,7 +1,8 @@
1
1
  module HQ
2
2
  module GraphQL
3
3
  class Mutation < ::GraphQL::Schema::Mutation
4
- include ::HQ::GraphQL::InputExtensions
4
+ include Scalars
5
+ include ::HQ::GraphQL::ActiveRecordExtensions
5
6
 
6
7
  def self.generate_payload_type
7
8
  lazy_load!
@@ -8,7 +8,7 @@ module HQ
8
8
  graphql_name graphql_name
9
9
 
10
10
  lazy_load do
11
- field :errors, [String], null: false
11
+ field :errors, ::HQ::GraphQL::Types::JSON, null: false
12
12
  field :resource, ::HQ::GraphQL::Types[model_name], null: true
13
13
  end
14
14
 
@@ -23,6 +23,10 @@ module HQ
23
23
  argument primary_key, ::HQ::GraphQL::Types.type_from_column(pk_column), required: true
24
24
  end
25
25
  end
26
+
27
+ def errors_from_resource(resource)
28
+ resource.errors.to_h.deep_transform_keys { |k| k.to_s.camelize(:lower) }
29
+ end
26
30
  end
27
31
  end
28
32
 
@@ -6,7 +6,8 @@ module HQ
6
6
 
7
7
  def self.included(base)
8
8
  ::HQ::GraphQL.types << base
9
- base.extend(ClassMethods)
9
+ base.include Scalars
10
+ base.extend ClassMethods
10
11
  end
11
12
 
12
13
  module ClassMethods
@@ -27,14 +28,7 @@ module HQ
27
28
  end
28
29
 
29
30
  def input_klass
30
- @input_klass ||= begin
31
- scoped_model_name = model_name
32
- scoped_inputs_proc = inputs_proc
33
- Class.new(::HQ::GraphQL::InputObject) do
34
- graphql_name "#{scoped_model_name.demodulize}Input"
35
- instance_eval(&scoped_inputs_proc)
36
- end
37
- end
31
+ @input_klass ||= build_input_object
38
32
  end
39
33
 
40
34
  def query_klass
@@ -43,43 +37,39 @@ module HQ
43
37
 
44
38
  protected
45
39
 
46
- def inputs_proc
47
- @inputs_proc ||= build_input_proc
48
- end
49
-
50
40
  def default_scope(&block)
51
41
  @default_scope = block
52
42
  end
53
43
 
54
44
  def input(**options, &block)
55
- @inputs_proc = build_input_proc(**options, &block)
45
+ @input_klass = build_input_object(**options, &block)
56
46
  end
57
47
 
58
48
  def mutations(create: true, update: true, destroy: true)
49
+ scoped_model_name = model_name
59
50
  model_display_name = model_name.demodulize
60
51
  scoped_self = self
61
- delayed_execute_inputs_proc = proc { scoped_self.inputs_proc }
62
52
 
63
53
  if create
64
54
  create_mutation = ::HQ::GraphQL::Resource::Mutation.build(model_name, graphql_name: "#{model_display_name}Create") do
65
- define_method(:resolve) do |**attrs|
55
+ define_method(:resolve) do |**args|
66
56
  resource = scoped_self.model_klass.new
67
- resource.assign_attributes(attrs)
57
+ resource.assign_attributes(args[:attributes].format_nested_attributes)
68
58
  if resource.save
69
59
  {
70
60
  resource: resource,
71
- errors: [],
61
+ errors: {},
72
62
  }
73
63
  else
74
64
  {
75
65
  resource: nil,
76
- errors: resource.errors.full_messages
66
+ errors: errors_from_resource(resource)
77
67
  }
78
68
  end
79
69
  end
80
70
 
81
71
  lazy_load do
82
- instance_eval(&delayed_execute_inputs_proc.call)
72
+ argument :attributes, ::HQ::GraphQL::Inputs[scoped_model_name], required: true
83
73
  end
84
74
  end
85
75
 
@@ -92,32 +82,32 @@ module HQ
92
82
  graphql_name: "#{model_display_name}Update",
93
83
  require_primary_key: true
94
84
  ) do
95
- define_method(:resolve) do |**attrs|
96
- resource = scoped_self.find_record(attrs, context)
85
+ define_method(:resolve) do |**args|
86
+ resource = scoped_self.find_record(args, context)
97
87
 
98
88
  if resource
99
- resource.assign_attributes(attrs)
89
+ resource.assign_attributes(args[:attributes].format_nested_attributes)
100
90
  if resource.save
101
91
  {
102
92
  resource: resource,
103
- errors: [],
93
+ errors: {},
104
94
  }
105
95
  else
106
96
  {
107
97
  resource: resource,
108
- errors: resource.errors.full_messages
98
+ errors: errors_from_resource(resource)
109
99
  }
110
100
  end
111
101
  else
112
102
  {
113
103
  resource: nil,
114
- errors: ["Unable to find #{model_display_name}"],
104
+ errors: { resource: "Unable to find #{model_display_name}" }
115
105
  }
116
106
  end
117
107
  end
118
108
 
119
109
  lazy_load do
120
- instance_eval(&delayed_execute_inputs_proc.call)
110
+ argument :attributes, ::HQ::GraphQL::Inputs[scoped_model_name], required: true
121
111
  end
122
112
  end
123
113
 
@@ -142,13 +132,13 @@ module HQ
142
132
  else
143
133
  {
144
134
  resource: resource,
145
- errors: resource.errors.full_messages
135
+ errors: errors_from_resource(resource)
146
136
  }
147
137
  end
148
138
  else
149
139
  {
150
140
  resource: nil,
151
- errors: ["Unable to find #{model_display_name}"],
141
+ errors: { resource: "Unable to find #{model_display_name}" }
152
142
  }
153
143
  end
154
144
  end
@@ -181,15 +171,18 @@ module HQ
181
171
 
182
172
  with_model scoped_model_name, **options
183
173
 
184
- instance_eval(&block) if block
174
+ class_eval(&block) if block
185
175
  end
186
176
  end
187
177
 
188
- def build_input_proc(**options, &block)
178
+ def build_input_object(**options, &block)
189
179
  scoped_model_name = model_name
190
- proc do
180
+ Class.new(::HQ::GraphQL::InputObject) do
181
+ graphql_name "#{scoped_model_name.demodulize}Input"
182
+
191
183
  with_model scoped_model_name, **options
192
- instance_eval(&block) if block
184
+
185
+ class_eval(&block) if block
193
186
  end
194
187
  end
195
188
 
@@ -1,8 +1,10 @@
1
+ require "hq/graphql/types/json"
1
2
  require "hq/graphql/types/uuid"
2
3
 
3
4
  module HQ
4
5
  module GraphQL
5
6
  module Scalars
7
+ JSON = ::HQ::GraphQL::Types::JSON
6
8
  UUID = ::HQ::GraphQL::Types::UUID
7
9
  end
8
10
  end
@@ -0,0 +1,20 @@
1
+ module HQ
2
+ module GraphQL
3
+ module Types
4
+ class JSON < ::GraphQL::Schema::Scalar
5
+ description "JSON"
6
+
7
+ class << self
8
+ def coerce_input(value, context)
9
+ JSON.parse(value)
10
+ end
11
+
12
+ def coerce_result(value, context)
13
+ value
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  module HQ
2
2
  module GraphQL
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
5
5
  end
data/lib/hq/graphql.rb CHANGED
@@ -35,7 +35,6 @@ end
35
35
 
36
36
  require "hq/graphql/active_record_extensions"
37
37
  require "hq/graphql/scalars"
38
- require "hq/graphql/input_extensions"
39
38
 
40
39
  require "hq/graphql/inputs"
41
40
  require "hq/graphql/input_object"
@@ -1,6 +1,6 @@
1
1
  require 'rails_helper'
2
2
 
3
- describe ::HQ::GraphQL::Mutation do
3
+ describe ::HQ::GraphQL::InputObject do
4
4
 
5
5
  describe ".with_model" do
6
6
  let(:hq_input_object) do
@@ -27,7 +27,7 @@ describe ::HQ::GraphQL::Mutation do
27
27
  end
28
28
 
29
29
  expect(hq_input_object.arguments.keys).to be_empty
30
- hq_input_object.payload_type
30
+ hq_input_object.graphql_definition
31
31
  expected = ["createdAt", "id", "name", "organizationId", "updatedAt"]
32
32
  expect(hq_input_object.arguments.keys).to contain_exactly(*expected)
33
33
  end
@@ -40,7 +40,7 @@ describe ::HQ::GraphQL::Mutation do
40
40
  end
41
41
 
42
42
  expect(hq_input_object.arguments.keys).to be_empty
43
- hq_input_object.payload_type
43
+ hq_input_object.graphql_definition
44
44
  expected = ["name", "updatedAt"]
45
45
  expect(hq_input_object.arguments.keys).to contain_exactly(*expected)
46
46
  end
@@ -51,7 +51,7 @@ describe ::HQ::GraphQL::Mutation do
51
51
  with_model "Advisor"
52
52
  end
53
53
 
54
- expect { hq_input_object.payload_type }.to raise_error(described_class::Error)
54
+ expect { hq_input_object.graphql_definition }.to raise_error(described_class::Error)
55
55
  end
56
56
 
57
57
  it "raises an error when not connected to a model" do
@@ -59,7 +59,7 @@ describe ::HQ::GraphQL::Mutation do
59
59
  remove_attributes :created_at
60
60
  end
61
61
 
62
- expect { hq_input_object.payload_type }.to raise_error(described_class::Error)
62
+ expect { hq_input_object.graphql_definition }.to raise_error(described_class::Error)
63
63
  end
64
64
  end
65
65
 
@@ -71,7 +71,7 @@ describe ::HQ::GraphQL::Mutation do
71
71
  end
72
72
 
73
73
  expect(hq_input_object.arguments.keys).to be_empty
74
- hq_input_object.payload_type
74
+ hq_input_object.graphql_definition
75
75
  expected = ["createdAt", "id", "name", "organizationId", "updatedAt"]
76
76
  expect(hq_input_object.arguments.keys).to contain_exactly(*expected)
77
77
  end
@@ -82,7 +82,7 @@ describe ::HQ::GraphQL::Mutation do
82
82
  with_model "Advisor"
83
83
  end
84
84
 
85
- expect { hq_input_object.payload_type }.to raise_error(described_class::Error)
85
+ expect { hq_input_object.graphql_definition }.to raise_error(described_class::Error)
86
86
  end
87
87
 
88
88
  it "raises an error when not connected to a model" do
@@ -90,13 +90,13 @@ describe ::HQ::GraphQL::Mutation do
90
90
  remove_associations :organization
91
91
  end
92
92
 
93
- expect { hq_input_object.payload_type }.to raise_error(described_class::Error)
93
+ expect { hq_input_object.graphql_definition }.to raise_error(described_class::Error)
94
94
  end
95
95
  end
96
96
 
97
97
  context "with attributes and associations turned off" do
98
98
  it "doesn't have any arguments by default" do
99
- hq_input_object.payload_type
99
+ hq_input_object.graphql_definition
100
100
  expect(hq_input_object.arguments.keys).to be_empty
101
101
  end
102
102
 
@@ -104,7 +104,7 @@ describe ::HQ::GraphQL::Mutation do
104
104
  hq_input_object.class_eval do
105
105
  with_model "Advisor", attributes: false, associations: false
106
106
  end
107
- hq_input_object.payload_type
107
+ hq_input_object.graphql_definition
108
108
  expect(hq_input_object.arguments.keys).to be_empty
109
109
  end
110
110
 
@@ -116,7 +116,7 @@ describe ::HQ::GraphQL::Mutation do
116
116
  end
117
117
 
118
118
  expect(hq_input_object.arguments.keys).to be_empty
119
- hq_input_object.payload_type
119
+ hq_input_object.graphql_definition
120
120
  expect(hq_input_object.arguments.keys).to contain_exactly("name")
121
121
  end
122
122
 
@@ -126,7 +126,7 @@ describe ::HQ::GraphQL::Mutation do
126
126
  with_model "Advisor", attributes: false, associations: false
127
127
  end
128
128
 
129
- expect { hq_input_object.payload_type }.to raise_error(described_class::Error)
129
+ expect { hq_input_object.graphql_definition }.to raise_error(described_class::Error)
130
130
  end
131
131
 
132
132
  it "raises an error when not connected to a model" do
@@ -134,7 +134,7 @@ describe ::HQ::GraphQL::Mutation do
134
134
  add_attributes :name
135
135
  end
136
136
 
137
- expect { hq_input_object.payload_type }.to raise_error(described_class::Error)
137
+ expect { hq_input_object.graphql_definition }.to raise_error(described_class::Error)
138
138
  end
139
139
  end
140
140
 
@@ -146,8 +146,8 @@ describe ::HQ::GraphQL::Mutation do
146
146
  end
147
147
 
148
148
  expect(hq_input_object.arguments.keys).to be_empty
149
- hq_input_object.payload_type
150
- expect(hq_input_object.arguments.keys).to contain_exactly("organizationAttributes")
149
+ hq_input_object.graphql_definition
150
+ expect(hq_input_object.arguments.keys).to contain_exactly("organization")
151
151
  end
152
152
 
153
153
  it "raises an error when adding an association that doesn't exist" do
@@ -156,7 +156,7 @@ describe ::HQ::GraphQL::Mutation do
156
156
  with_model "Advisor", attributes: false, associations: false
157
157
  end
158
158
 
159
- expect { hq_input_object.payload_type }.to raise_error(described_class::Error)
159
+ expect { hq_input_object.graphql_definition }.to raise_error(described_class::Error)
160
160
  end
161
161
 
162
162
  it "raises an error when not connected to a model" do
@@ -164,7 +164,7 @@ describe ::HQ::GraphQL::Mutation do
164
164
  add_associations :organization
165
165
  end
166
166
 
167
- expect { hq_input_object.payload_type }.to raise_error(described_class::Error)
167
+ expect { hq_input_object.graphql_definition }.to raise_error(described_class::Error)
168
168
  end
169
169
  end
170
170
  end
@@ -179,8 +179,14 @@ describe ::HQ::GraphQL::Resource do
179
179
  update_mutation = advisor_type.mutation_klasses[:update_advisor]
180
180
  update_mutation.payload_type
181
181
 
182
+ input_object = advisor_type.input_klass
183
+ input_object.graphql_definition
184
+
182
185
  aggregate_failures do
183
- expected_arguments = ["id", "organizationId", "organizationAttributes", "createdAt", "updatedAt"]
186
+ expected_arguments = ["id", "organizationId", "organization", "createdAt", "updatedAt"]
187
+ expect(input_object.arguments.keys).to contain_exactly(*expected_arguments)
188
+
189
+ expected_arguments = ["id", "attributes"]
184
190
  expect(update_mutation.arguments.keys).to contain_exactly(*expected_arguments)
185
191
 
186
192
  expected_fields = ["errors", "resource"]
@@ -207,8 +213,8 @@ describe ::HQ::GraphQL::Resource do
207
213
 
208
214
  let(:create_mutation) {
209
215
  <<-gql
210
- mutation createAdvisor($name: String!, $organizationId: UUID!){
211
- createAdvisor(name: $name, organizationId: $organizationId) {
216
+ mutation createAdvisor($attributes: AdvisorInput!){
217
+ createAdvisor(attributes: $attributes) {
212
218
  errors
213
219
  resource {
214
220
  id
@@ -221,8 +227,8 @@ describe ::HQ::GraphQL::Resource do
221
227
 
222
228
  let(:update_mutation) {
223
229
  <<-gql
224
- mutation updateAdvisor($id: UUID!, $name: String!, $organizationAttributes: OrganizationInput){
225
- updateAdvisor(id: $id, name: $name, organizationAttributes: $organizationAttributes) {
230
+ mutation updateAdvisor($id: UUID!, $attributes: AdvisorInput!){
231
+ updateAdvisor(id: $id, attributes: $attributes) {
226
232
  errors
227
233
  resource {
228
234
  name
@@ -275,7 +281,7 @@ describe ::HQ::GraphQL::Resource do
275
281
  it "creates" do
276
282
  organization = FactoryBot.create(:organization)
277
283
  name = "Bob"
278
- results = schema.execute(create_mutation, variables: { name: name, organizationId: organization.id })
284
+ results = schema.execute(create_mutation, variables: { attributes: { name: name, organizationId: organization.id } })
279
285
 
280
286
  data = results["data"]
281
287
  aggregate_failures do
@@ -292,8 +298,10 @@ describe ::HQ::GraphQL::Resource do
292
298
 
293
299
  results = schema.execute(update_mutation, variables: {
294
300
  id: advisor.id,
295
- name: name,
296
- organizationAttributes: { id: advisor.organization_id, name: organization_name }
301
+ attributes: {
302
+ name: name,
303
+ organization: { id: advisor.organization_id, name: organization_name }
304
+ }
297
305
  })
298
306
 
299
307
  data = results["data"]
@@ -332,7 +340,12 @@ describe ::HQ::GraphQL::Resource do
332
340
 
333
341
  it "returns an error on a mutation" do
334
342
  advisor = FactoryBot.create(:advisor)
335
- results = schema.execute(update_mutation, variables: { id: advisor.id, name: "Bob" })
343
+ results = schema.execute(update_mutation, variables: {
344
+ id: advisor.id,
345
+ attributes: {
346
+ name: "Bob"
347
+ }
348
+ })
336
349
 
337
350
  data = results["data"]
338
351
  aggregate_failures do
@@ -360,7 +373,12 @@ describe ::HQ::GraphQL::Resource do
360
373
 
361
374
  it "returns an error on a mutation" do
362
375
  advisor = FactoryBot.create(:advisor)
363
- results = schema.execute(update_mutation, variables: { id: advisor.id, name: "Bob" })
376
+ results = schema.execute(update_mutation, variables: {
377
+ id: advisor.id,
378
+ attributes: {
379
+ name: "Bob"
380
+ }
381
+ })
364
382
 
365
383
  data = results["data"]
366
384
  aggregate_failures do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Jones
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-15 00:00:00.000000000 Z
11
+ date: 2018-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -98,7 +98,6 @@ files:
98
98
  - lib/hq/graphql.rb
99
99
  - lib/hq/graphql/active_record_extensions.rb
100
100
  - lib/hq/graphql/engine.rb
101
- - lib/hq/graphql/input_extensions.rb
102
101
  - lib/hq/graphql/input_object.rb
103
102
  - lib/hq/graphql/inputs.rb
104
103
  - lib/hq/graphql/mutation.rb
@@ -109,6 +108,7 @@ files:
109
108
  - lib/hq/graphql/root_query.rb
110
109
  - lib/hq/graphql/scalars.rb
111
110
  - lib/hq/graphql/types.rb
111
+ - lib/hq/graphql/types/json.rb
112
112
  - lib/hq/graphql/types/uuid.rb
113
113
  - lib/hq/graphql/version.rb
114
114
  - spec/factories/advisors.rb
@@ -121,8 +121,8 @@ files:
121
121
  - spec/internal/config/database.yml
122
122
  - spec/internal/db/schema.rb
123
123
  - spec/lib/graphql/active_record_extensions_spec.rb
124
+ - spec/lib/graphql/input_object_spec.rb
124
125
  - spec/lib/graphql/inputs_spec.rb
125
- - spec/lib/graphql/mutation_spec.rb
126
126
  - spec/lib/graphql/object_spec.rb
127
127
  - spec/lib/graphql/resource_spec.rb
128
128
  - spec/lib/graphql/types/uuid_spec.rb
@@ -169,6 +169,6 @@ test_files:
169
169
  - spec/lib/graphql/object_spec.rb
170
170
  - spec/lib/graphql/inputs_spec.rb
171
171
  - spec/lib/graphql/resource_spec.rb
172
+ - spec/lib/graphql/input_object_spec.rb
172
173
  - spec/lib/graphql/types_spec.rb
173
- - spec/lib/graphql/mutation_spec.rb
174
174
  - spec/rails_helper.rb
@@ -1,56 +0,0 @@
1
- module HQ
2
- module GraphQL
3
- module InputExtensions
4
-
5
- def self.included(base)
6
- base.include Scalars
7
- base.include ::HQ::GraphQL::ActiveRecordExtensions
8
- base.extend(ClassMethods)
9
- end
10
-
11
- module ClassMethods
12
-
13
- def with_model(model_name, attributes: true, associations: false)
14
- self.model_name = model_name
15
- self.auto_load_attributes = attributes
16
- self.auto_load_associations = associations
17
-
18
- lazy_load do
19
- model_columns.each do |column|
20
- argument_from_column(column)
21
- end
22
-
23
- model_associations.each do |association|
24
- argument_from_association association
25
- end
26
- end
27
- end
28
-
29
- private
30
-
31
- def argument_from_association(association)
32
- input = ::HQ::GraphQL::Inputs[association.klass]
33
- name = association.name
34
- name_attributes = "#{name}_attributes"
35
- case association.macro
36
- when :has_many
37
- argument name_attributes, [input], required: false
38
- else
39
- argument name_attributes, input, required: false
40
- end
41
-
42
- if !model_klass.nested_attributes_options.keys.include?(name.to_sym)
43
- model_klass.accepts_nested_attributes_for name, allow_destroy: true
44
- end
45
- rescue ::HQ::GraphQL::Inputs::Error
46
- nil
47
- end
48
-
49
- def argument_from_column(column)
50
- argument column.name, ::HQ::GraphQL::Types.type_from_column(column), required: false
51
- end
52
-
53
- end
54
- end
55
- end
56
- end