7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# File 'lib/generators/ntq_tools/graphql_scaffold_generator.rb', line 7
def create_type_file
singular_name = plural_name.singularize
context_name = plural_name.camelcase
class_name = singular_name.camelcase
return unless defined?(class_name.constantize) && class_name.constantize.respond_to?(:columns_hash)
definition = []
class_name.constantize.columns_hash.map do |k, v|
next if SKIPPED_ATTRIBUTE_NAMES.include?(k)
case v.type
when :string, :text
definition << "field :#{k}, String"
when :integer
definition << if k.include?('_id') || k == 'id'
"field :#{k}, ID"
else
"field :#{k}, Integer"
end
when :boolean
definition << "field :#{k}, Boolean"
when :decimal
definition << "field :#{k}, Float"
when :date, :datetime, :time
definition << "field :#{k}, Types::TimeType"
else
raise "Type non défini: #{v.type.inspect}"
end
end
attributes = []
class_name.constantize.columns_hash.map do |k, v|
next if SKIPPED_ATTRIBUTE_NAMES.include?(k)
case v.type
when :string, :text
attributes << "argument :#{k}, String, required: false"
when :integer
attributes << if k.include?('_id') || k == 'id'
"argument :#{k}, ID, required: false"
else
"argument :#{k}, Integer, required: false"
end
when :boolean
attributes << "argument :#{k}, Boolean, required: false"
when :decimal
attributes << "argument :#{k}, Float, required: false"
when :date, :datetime
attributes << "argument :#{k}, Types::TimeType, required: false"
else
raise "Type non défini: #{v.type.inspect}"
end
end
associations = []
%i[belongs_to has_one].each do |association_type|
class_name.constantize.reflect_on_all_associations(association_type).each do |asso|
asso_class_name = asso.options.dig(:class_name) || asso.name.to_s.singularize.camelcase
associations << "field :#{asso.name}, Types::#{asso_class_name.pluralize.camelcase}::#{asso_class_name}Type"
end
end
[:has_many].each do |association_type|
class_name.constantize.reflect_on_all_associations(association_type).each do |asso|
asso_class_name = asso.options.dig(:class_name) || asso.name.to_s.singularize.camelcase
next if asso_class_name == 'PaperTrail::Version'
associations << "field :#{asso.name}, [Types::#{asso_class_name.pluralize.camelcase}::#{asso_class_name}Type]"
end
end
create_file "app/graphql/types/#{plural_name}/#{singular_name}_type.rb", <<~FILE
module Types
module #{plural_name.camelcase}
class #{singular_name.camelcase}Type < BaseObject
include Resolvers::TimestampsFields
include Resolvers::AuthorizationFields
# authorize_resource
# authorize_attributes
#{definition.join('
')}
## Associations
#{associations.join('
')}
end
end
end
FILE
create_file "app/graphql/input_object/#{singular_name}_attributes.rb", <<~FILE
module InputObject
class #{singular_name.camelcase}Attributes < AttributesInputObject
#{attributes.join('
')}
end
end
FILE
create_file "app/graphql/mutations/#{plural_name}/create_#{singular_name}.rb", <<~FILE
module Mutations
class #{plural_name.camelcase}::Create#{singular_name.camelcase} < BaseMutation
field :#{singular_name}, Types::#{plural_name.camelcase}::#{singular_name.camelcase}Type, null: false
field :flash_messages, [Types::JsonType], null: false
argument :attributes, InputObject::#{singular_name.camelcase}Attributes, required: true
def authorized?(attributes:)
context[:current_user].can?(:create, #{singular_name.camelcase})
end
def resolve(attributes:)
#{singular_name} = ::#{singular_name.camelcase}.new
#{singular_name}.attributes = attributes
#{singular_name}.save!
{
#{singular_name}: #{singular_name},
flash_messages: [
{
type: 'success',
message: I18n.t(:'flashes.#{plural_name}.create.success')
}
]
}
end
end
end
FILE
create_file "app/graphql/mutations/#{plural_name}/update_#{singular_name}.rb", <<~FILE
module Mutations
class #{plural_name.camelcase}::Update#{singular_name.camelcase} < BaseMutation
field :#{singular_name}, Types::#{plural_name.camelcase}::#{singular_name.camelcase}Type, null: false
field :flash_messages, [Types::JsonType], null: true
argument :id, ID, required: true
argument :attributes, InputObject::#{singular_name.camelcase}Attributes, required: true
def authorized?(attributes:, id:)
#{singular_name} = ::#{singular_name.camelcase}.find id
context[:current_user].can?(:update, #{singular_name})
end
def resolve(attributes:, id:)
#{singular_name} = ::#{singular_name.camelcase}.find id
#{singular_name}.attributes = attributes
#{singular_name}.save!
{
#{singular_name}: #{singular_name},
flash_messages: [
{
type: 'success',
message: I18n.t(:'flashes.#{plural_name}.update.success')
}
]
}
end
end
end
FILE
create_file "app/graphql/mutations/#{plural_name}/delete_#{singular_name}.rb", <<~FILE
module Mutations
class #{plural_name.camelcase}::Delete#{singular_name.camelcase} < BaseMutation
field :#{singular_name}, Types::#{plural_name.camelcase}::#{singular_name.camelcase}Type
field :flash_messages, [Types::JsonType], null: true
argument :id, ID, required: true
def authorized?(id:)
#{singular_name} = ::#{singular_name.camelcase}.find id
context[:current_user].can?(:destroy, #{singular_name})
end
def resolve(id:)
#{singular_name} = ::#{singular_name.camelcase}.find id
#{singular_name}.destroy!
{
#{singular_name}: #{singular_name},
flash_messages: [
{
type: 'success',
message: I18n.t(:'flashes.#{plural_name}.delete.success')
}
]
}
end
end
end
FILE
inject_into_file 'app/graphql/types/mutation_type.rb', after: "Types::BaseObject" do <<-FILE
field :create_#{singular_name}, mutation: Mutations::#{plural_name.camelcase}::Create#{singular_name.camelcase}
field :update_#{singular_name}, mutation: Mutations::#{plural_name.camelcase}::Update#{singular_name.camelcase}
field :delete_#{singular_name}, mutation: Mutations::#{plural_name.camelcase}::Delete#{singular_name.camelcase}
FILE
end
puts 'Do you want to create the payload file ? (y/n)'
input = $stdin.gets.strip
if input == 'y'
create_file "app/graphql/types/#{plural_name}/#{singular_name}_list_payload.rb", <<~FILE
module Types
module #{plural_name.camelcase}
class #{singular_name.camelcase}ListPayload < BaseObject
field :#{plural_name}, [#{plural_name.camelcase}::#{singular_name.camelcase}Type], null: true
field :pagination, PaginationType, null: true
end
end
end
FILE
create_file "app/graphql/input_object/search_#{plural_name}_attributes.rb", <<~FILE
module InputObject
class Search#{plural_name.camelcase}Attributes < AttributesInputObject
end
end
FILE
if defined?(Interactor)
create_file "app/interactors/#{plural_name}/search_#{plural_name}.rb", <<-FILE
module #{context_name}
class Search#{plural_name.camelcase}
include Interactor::Organizer
organize Get#{plural_name.camelcase}, PaginateRecords
end
end
FILE
create_file "app/interactors/#{plural_name}/get_#{plural_name}.rb", <<-FILE
module #{context_name}
class Get#{plural_name.camelcase}
include Interactor
def call
search = context.search || {}
records = ::#{class_name}.ransack(search).result
context.records = records
end
end
end
FILE
end
inject_into_file 'app/graphql/types/query_type.rb', after: "# Fields\n" do <<-FILE
field :#{plural_name}, Types::#{plural_name.camelcase}::#{singular_name.camelcase}ListPayload do
argument :search, InputObject::Search#{plural_name.camelcase}Attributes, required: false
argument :page, Integer, required: false
argument :per_page, Integer, required: false
end
def #{plural_name}(search: {}, page: 1, per_page: 25)
ctx = ::#{plural_name.camelcase}::Search#{plural_name.camelcase}.call(search: search, pagination_params: { page: page, per_page: per_page })
{
#{plural_name}: ctx.records,
pagination: ctx.pagination
}
end\n
FILE
end
inject_into_file 'app/graphql/types/query_type.rb', after: "# Fields\n" do <<-FILE
field :#{singular_name}, Types::#{plural_name.camelcase}::#{singular_name.camelcase}Type do
argument :id, ID, required: true
end
def #{singular_name}(id:)
record = ::#{singular_name.camelcase}.find(id)
context[:current_user].authorize!(:show, record) if context[:current_user]
record
end\n
FILE
end
end
end
|