souls 0.18.2 → 0.20.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/souls/init.rb CHANGED
@@ -23,25 +23,6 @@ module Souls
23
23
  puts error
24
24
  end
25
25
 
26
- def config_init app_name: "souls", strain: "api"
27
- file_dir = "#{__dir__}/config"
28
- FileUtils.mkdir_p file_dir unless Dir.exist? file_dir
29
- FileUtils.touch "#{__dir__}/config/souls.rb"
30
- file_path = "#{__dir__}/config/souls.rb"
31
- puts "Generating souls conf..."
32
- sleep(rand(0.1..0.3))
33
- puts "Generated!"
34
- puts "Let's Edit SOULs Conf: `#{file_path}`"
35
- File.open(file_path, "w") do |f|
36
- f.write <<~EOS
37
- Souls.configure do |config|
38
- config.app = "#{app_name}"
39
- config.strain = "#{strain}"
40
- end
41
- EOS
42
- end
43
- end
44
-
45
26
  def download_souls app_name: "souls", repository_name: "souls_api "
46
27
  version = get_version repository_name: repository_name
47
28
  system "curl -OL https://github.com/elsoul/#{repository_name}/archive/#{version}.tar.gz"
@@ -65,1137 +46,6 @@ module Souls
65
46
  puts "$ cd #{app_name}"
66
47
  puts "------------------------------"
67
48
  end
68
-
69
- def model class_name: "souls"
70
- file_path = "./app/models/#{class_name.singularize}.rb"
71
- return ["Model already exist! #{file_path}"] if File.exist? file_path
72
- File.open(file_path, "w") do |f|
73
- f.write <<~EOS
74
- class #{class_name.camelize} < ActiveRecord::Base
75
- end
76
- EOS
77
- end
78
- [file_path]
79
- end
80
-
81
- def test_dir
82
- FileUtils.mkdir_p "./app/graphql/mutations"
83
- FileUtils.mkdir_p "./app/graphql/queries"
84
- FileUtils.mkdir_p "./app/graphql/types"
85
- FileUtils.mkdir_p "./app/graphql/resolvers"
86
- FileUtils.mkdir_p "./app/jobs"
87
- FileUtils.mkdir_p "./app/models"
88
- FileUtils.mkdir_p "./spec/factories"
89
- FileUtils.mkdir_p "./spec/queries"
90
- FileUtils.mkdir_p "./spec/mutations"
91
- FileUtils.mkdir_p "./spec/models"
92
- FileUtils.mkdir_p "./db/"
93
- FileUtils.touch "./db/schema.rb"
94
- puts "test dir created!"
95
- end
96
-
97
- def type_check type
98
- {
99
- bigint: "Integer",
100
- string: "String",
101
- float: "Float",
102
- text: "String",
103
- datetime: "GraphQL::Types::ISO8601DateTime",
104
- date: "GraphQL::Types::ISO8601DateTime",
105
- boolean: "Boolean",
106
- integer: "Integer"
107
- }[type.to_sym]
108
- end
109
-
110
- def get_test_type type
111
- {
112
- bigint: 1,
113
- float: 4.2,
114
- string: '"MyString"',
115
- text: '"MyString"',
116
- datetime: "Time.now",
117
- date: "Time.now",
118
- boolean: false,
119
- integer: 1
120
- }[type.to_sym]
121
- end
122
-
123
- def table_check line: "", class_name: ""
124
- if line.include?("create_table") && (line.split(" ")[1].gsub("\"", "").gsub(",", "") == class_name.pluralize.to_s)
125
- return true
126
- end
127
- false
128
- end
129
-
130
- def create_mutation_head class_name: "souls"
131
- dir_name = "./app/graphql/mutations/#{class_name}"
132
- FileUtils.mkdir_p dir_name unless Dir.exist? dir_name
133
- file_path = "./app/graphql/mutations/#{class_name}/create_#{class_name}.rb"
134
- File.open(file_path, "w") do |new_line|
135
- new_line.write <<~EOS
136
- module Mutations
137
- module #{class_name.camelize}
138
- class Create#{class_name.camelize} < BaseMutation
139
- field :#{class_name}_edge, Types::#{class_name.camelize}NodeType, null: false
140
- field :error, String, null: true
141
-
142
- EOS
143
- end
144
- end
145
-
146
- def create_mutation_params class_name: "souls"
147
- file_path = "./app/graphql/mutations/#{class_name}/create_#{class_name}.rb"
148
- path = "./db/schema.rb"
149
- @on = false
150
- @user_exist = false
151
- @relation_params = []
152
- File.open(file_path, "a") do |new_line|
153
- File.open(path, "r") do |f|
154
- f.each_line.with_index do |line, i|
155
- if @on
156
- if line.include?("end") || line.include?("t.index")
157
- if @user_exist
158
- new_line.write <<-EOS
159
-
160
- def resolve **args
161
- args[:user_id] = context[:user].id
162
- EOS
163
- else
164
- new_line.write <<-EOS
165
-
166
- def resolve **args
167
- EOS
168
- end
169
- break
170
- end
171
- field = "[String]" if line.include?("array: true")
172
- type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
173
- field ||= type_check type
174
- case name
175
- when "user_id"
176
- @user_exist = true
177
- when /$*_id\z/
178
- @relation_params << name
179
- new_line.write " argument :#{name}, String, required: false\n"
180
- when "created_at", "updated_at"
181
- next
182
- else
183
- new_line.write " argument :#{name}, #{field}, required: false\n"
184
- end
185
- end
186
- @on = true if table_check(line: line, class_name: class_name)
187
- end
188
- end
189
- end
190
- @relation_params
191
- end
192
-
193
- def create_mutation_after_params class_name: "article", relation_params: []
194
- return false if relation_params.empty?
195
- file_path = "./app/graphql/mutations/#{class_name}/create_#{class_name}.rb"
196
- relation_params.each do |params_name|
197
- File.open(file_path, "a") do |new_line|
198
- new_line.write " _, args[:#{params_name}] = SoulsApiSchema.from_global_id(args[:#{params_name}])\n"
199
- end
200
- end
201
- true
202
- end
203
-
204
- def create_mutation_end class_name: "souls"
205
- file_path = "./app/graphql/mutations/#{class_name}/create_#{class_name}.rb"
206
- File.open(file_path, "a") do |new_line|
207
- new_line.write <<~EOS
208
- #{class_name} = ::#{class_name.camelize}.new args
209
- if #{class_name}.save
210
- { #{class_name}_edge: { node: #{class_name} } }
211
- else
212
- { error: #{class_name}.errors.full_messages }
213
- end
214
- rescue StandardError => error
215
- GraphQL::ExecutionError.new error
216
- end
217
- end
218
- end
219
- end
220
- EOS
221
- end
222
- file_path
223
- end
224
-
225
- def update_mutation_head class_name: "souls"
226
- file_path = "./app/graphql/mutations/#{class_name}/update_#{class_name}.rb"
227
- File.open(file_path, "w") do |new_line|
228
- new_line.write <<~EOS
229
- module Mutations
230
- module #{class_name.camelize}
231
- class Update#{class_name.camelize} < BaseMutation
232
- field :#{class_name}_edge, Types::#{class_name.camelize}NodeType, null: false
233
-
234
- argument :id, String, required: true
235
- EOS
236
- end
237
- end
238
-
239
- def update_mutation_params class_name: "souls"
240
- file_path = "./app/graphql/mutations/#{class_name}/update_#{class_name}.rb"
241
- path = "./db/schema.rb"
242
- @on = false
243
- @user_exist = false
244
- @relation_params = []
245
- File.open(file_path, "a") do |new_line|
246
- File.open(path, "r") do |f|
247
- f.each_line.with_index do |line, i|
248
- if @on
249
- if line.include?("end") || line.include?("t.index")
250
- if @user_exist
251
- new_line.write <<-EOS
252
-
253
- def resolve **args
254
- args[:user_id] = context[:user].id
255
- _, args[:id] = SoulsApiSchema.from_global_id(args[:id])
256
- EOS
257
- else
258
- new_line.write <<-EOS
259
-
260
- def resolve **args
261
- _, args[:id] = SoulsApiSchema.from_global_id(args[:id])
262
- EOS
263
- end
264
- break
265
- end
266
- field = "[String]" if line.include?("array: true")
267
- type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
268
- field ||= type_check type
269
- case name
270
- when "user_id"
271
- @user_exist = true
272
- when /$*_id\z/
273
- @relation_params << name
274
- new_line.write " argument :#{name}, String, required: false\n"
275
- when "created_at", "updated_at"
276
- next
277
- else
278
- new_line.write " argument :#{name}, #{field}, required: false\n"
279
- end
280
- end
281
- @on = true if table_check(line: line, class_name: class_name)
282
- end
283
- end
284
- end
285
- @relation_params
286
- end
287
-
288
- def update_mutation_after_params class_name: "article", relation_params: []
289
- return false if relation_params.empty?
290
- file_path = "./app/graphql/mutations/#{class_name}/update_#{class_name}.rb"
291
- relation_params.each do |params_name|
292
- File.open(file_path, "a") do |new_line|
293
- new_line.write " _, args[:#{params_name}] = SoulsApiSchema.from_global_id(args[:#{params_name}])\n"
294
- end
295
- end
296
- true
297
- end
298
-
299
- def update_mutation_end class_name: "souls"
300
- file_path = "./app/graphql/mutations/#{class_name}/update_#{class_name}.rb"
301
- File.open(file_path, "a") do |new_line|
302
- new_line.write <<~EOS
303
- #{class_name} = ::#{class_name.camelize}.find args[:id]
304
- #{class_name}.update args
305
- { #{class_name}_edge: { node: ::#{class_name.camelize}.find(args[:id]) } }
306
- rescue StandardError => error
307
- GraphQL::ExecutionError.new error
308
- end
309
- end
310
- end
311
- end
312
- EOS
313
- end
314
- file_path
315
- end
316
-
317
- def update_mutation class_name: "souls"
318
- update_mutation_head class_name: class_name
319
- relation_params = update_mutation_params class_name: class_name
320
- update_mutation_after_params class_name: class_name, relation_params: relation_params
321
- update_mutation_end class_name: class_name
322
- end
323
-
324
- def delete_mutation class_name: "souls"
325
- file_path = "./app/graphql/mutations/#{class_name}/delete_#{class_name}.rb"
326
- File.open(file_path, "w") do |f|
327
- f.write <<~EOS
328
- module Mutations
329
- module #{class_name.camelize}
330
- class Delete#{class_name.camelize} < BaseMutation
331
- field :#{class_name}, Types::#{class_name.camelize}Type, null: false
332
- argument :id, String, required: true
333
-
334
- def resolve **args
335
- _, data_id = SoulsApiSchema.from_global_id args[:id]
336
- #{class_name} = ::#{class_name.camelize}.find data_id
337
- #{class_name}.update(is_deleted: true)
338
- { #{class_name}: ::#{class_name.camelize}.find(data_id) }
339
- rescue StandardError => error
340
- GraphQL::ExecutionError.new error
341
- end
342
- end
343
- end
344
- end
345
- EOS
346
- end
347
- file_path
348
- end
349
-
350
- def destroy_delete_mutation class_name: "souls"
351
- file_path = "./app/graphql/mutations/#{class_name}/destroy_delete_#{class_name}.rb"
352
- File.open(file_path, "w") do |f|
353
- f.write <<~EOS
354
- module Mutations
355
- module #{class_name.camelize}
356
- class DestroyDelete#{class_name.camelize} < BaseMutation
357
- field :#{class_name}, Types::#{class_name.camelize}Type, null: false
358
- argument :id, String, required: true
359
-
360
- def resolve **args
361
- _, data_id = SoulsApiSchema.from_global_id args[:id]
362
- #{class_name} = ::#{class_name.camelize}.find data_id
363
- #{class_name}.destroy
364
- { #{class_name}: #{class_name} }
365
- rescue StandardError => error
366
- GraphQL::ExecutionError.new error
367
- end
368
- end
369
- end
370
- end
371
- EOS
372
- end
373
- file_path
374
- end
375
-
376
- def create_confirm dir_path: ""
377
- puts "Directory already exists, Overwrite?? (Y/N)\n#{dir_path}"
378
- input = STDIN.gets.chomp
379
- return true if input == "Y"
380
- raise StandardError.new "Directory Already Exist!\n#{dir_path}"
381
- end
382
-
383
- def mutation class_name: "souls"
384
- singularized_class_name = class_name.singularize
385
-
386
- create_mutation_head class_name: singularized_class_name
387
- relation_params = create_mutation_params class_name: singularized_class_name
388
- create_mutation_after_params class_name: singularized_class_name, relation_params: relation_params
389
- [
390
- create_mutation_end(class_name: singularized_class_name),
391
- update_mutation(class_name: singularized_class_name),
392
- delete_mutation(class_name: singularized_class_name),
393
- destroy_delete_mutation(class_name: singularized_class_name)
394
- ]
395
- end
396
-
397
- def create_queries class_name: "souls"
398
- file_path = "./app/graphql/queries/#{class_name.pluralize}.rb"
399
- File.open(file_path, "w") do |f|
400
- f.write <<~EOS
401
- module Queries
402
- class #{class_name.camelize.pluralize} < Queries::BaseQuery
403
- type [Types::#{class_name.camelize}Type], null: false
404
-
405
- def resolve
406
- ::#{class_name.camelize}.all
407
- rescue StandardError => error
408
- GraphQL::ExecutionError.new error
409
- end
410
- end
411
- end
412
- EOS
413
- end
414
- file_path
415
- end
416
-
417
- def create_query class_name: "souls"
418
- file_path = "./app/graphql/queries/#{class_name}.rb"
419
- File.open(file_path, "w") do |f|
420
- f.write <<~EOS
421
- module Queries
422
- class #{class_name.camelize} < Queries::BaseQuery
423
- type Types::#{class_name.camelize}Type, null: false
424
- argument :id, String, required: true
425
-
426
- def resolve **args
427
- _, data_id = SoulsApiSchema.from_global_id args[:id]
428
- ::#{class_name.camelize}.find(data_id)
429
- rescue StandardError => error
430
- GraphQL::ExecutionError.new error
431
- end
432
- end
433
- end
434
- EOS
435
- file_path
436
- end
437
- end
438
-
439
- def query class_name: "souls"
440
- singularized_class_name = class_name.singularize
441
- [create_query(class_name: singularized_class_name), create_queries(class_name: singularized_class_name)]
442
- end
443
-
444
- def create_type_head class_name: "souls"
445
- file_path = "./app/graphql/types/#{class_name}_type.rb"
446
- File.open(file_path, "w") do |f|
447
- f.write <<~EOS
448
- module Types
449
- class #{class_name.camelize}Type < GraphQL::Schema::Object
450
- implements GraphQL::Types::Relay::Node
451
-
452
- EOS
453
- end
454
- end
455
-
456
- def create_type_params class_name: "souls"
457
- file_path = "./app/graphql/types/#{class_name}_type.rb"
458
- path = "./db/schema.rb"
459
- @on = false
460
- File.open(file_path, "a") do |new_line|
461
- File.open(path, "r") do |f|
462
- f.each_line.with_index do |line, i|
463
- if @on
464
- new_line.write "\n" && break if line.include?("end") || line.include?("t.index")
465
- field = "[String]" if line.include?("array: true")
466
- type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
467
- field ||= type_check type
468
- case name
469
- when /$*_id\z/
470
- new_line.write " field :#{name.gsub("_id", "")}, Types::#{name.gsub("_id", "").singularize.camelize}Type, null: false\n"
471
- else
472
- new_line.write " field :#{name}, #{field}, null: true\n"
473
- end
474
- end
475
- if table_check(line: line, class_name: class_name)
476
- @on = true
477
- new_line.write " global_id_field :id\n"
478
- end
479
- end
480
- end
481
- end
482
- end
483
-
484
- def create_type_end class_name: "souls"
485
- file_path = "./app/graphql/types/#{class_name}_type.rb"
486
- File.open(file_path, "a") do |f|
487
- f.write <<~EOS
488
- end
489
- end
490
- EOS
491
- end
492
- [file_path]
493
- end
494
-
495
- def type class_name: "souls"
496
- singularized_class_name = class_name.singularize
497
- create_type_head class_name: singularized_class_name
498
- create_type_params class_name: singularized_class_name
499
- create_type_end class_name: singularized_class_name
500
- end
501
-
502
- def rspec_factory_head class_name: "souls"
503
- file_path = "./spec/factories/#{class_name.pluralize}.rb"
504
- File.open(file_path, "w") do |f|
505
- f.write <<~EOS
506
- FactoryBot.define do
507
- factory :#{class_name} do
508
- EOS
509
- end
510
- end
511
-
512
- def rspec_factory_params class_name: "souls"
513
- file_path = "./spec/factories/#{class_name.pluralize}.rb"
514
- path = "./db/schema.rb"
515
- @on = false
516
- File.open(file_path, "a") do |new_line|
517
- File.open(path, "r") do |f|
518
- f.each_line.with_index do |line, i|
519
- if @on
520
- new_line.write "\n" && break if line.include?("end") || line.include?("t.index")
521
- field = '["tag1", "tag2", "tag3"]' if line.include?("array: true")
522
- type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
523
- field ||= get_test_type type
524
- if type == "bigint" && name.include?("_id")
525
- id_name = name.gsub("_id", "")
526
- new_line.write " association :#{id_name}, factory: :#{id_name}\n"
527
- else
528
- new_line.write " #{name} { #{field} }\n"
529
- end
530
- end
531
- if table_check(line: line, class_name: class_name)
532
- @on = true
533
- end
534
- end
535
- end
536
- end
537
- end
538
-
539
- def rspec_factory_end class_name: "souls"
540
- file_path = "./spec/factories/#{class_name.pluralize}.rb"
541
- File.open(file_path, "a") do |f|
542
- f.write <<~EOS
543
- end
544
- end
545
- EOS
546
- end
547
- [file_path]
548
- end
549
-
550
- def rspec_factory class_name: "souls"
551
- file_path = "./spec/factories/#{class_name.pluralize}.rb"
552
- return ["Factory aleady exist! #{file_path}"] if File.exist? file_path
553
- singularized_class_name = class_name.singularize
554
- rspec_factory_head class_name: singularized_class_name
555
- rspec_factory_params class_name: singularized_class_name
556
- rspec_factory_end class_name: singularized_class_name
557
- end
558
-
559
- def rspec_model class_name: "souls"
560
- file_path = "./spec/models/#{class_name}_spec.rb"
561
- return ["Aleady Exist!"] if File.exist? file_path
562
- File.open(file_path, "w") do |f|
563
- f.write <<~EOS
564
- RSpec.describe "#{class_name.camelize} Model テスト", type: :model do
565
- describe "#{class_name.camelize} データを書き込む" do
566
- it "valid #{class_name.camelize} Model" do
567
- expect(FactoryBot.build(:#{class_name.singularize})).to be_valid
568
- end
569
- end
570
- end
571
- EOS
572
- end
573
- [file_path]
574
- end
575
-
576
- def rspec_mutation_head class_name: "souls"
577
- file_path = "./spec/mutations/#{class_name.singularize}_spec.rb"
578
- File.open(file_path, "w") do |f|
579
- f.write <<~EOS
580
- RSpec.describe \"#{class_name.camelize} Mutation テスト\" do
581
- describe "#{class_name.camelize} データを登録する" do
582
- EOS
583
- end
584
- end
585
-
586
- def rspec_mutation_after_head class_name: "souls"
587
- file_path = "./spec/mutations/#{class_name.singularize}_spec.rb"
588
- path = "./db/schema.rb"
589
- @on = false
590
- @user_exist = false
591
- @relation_params = []
592
- File.open(file_path, "a") do |new_line|
593
- File.open(path, "r") do |f|
594
- f.each_line.with_index do |line, i|
595
- if @on
596
- if line.include?("end") || line.include?("t.index")
597
- if @relation_params.empty?
598
- new_line.write <<-EOS
599
- let(:#{class_name}) { FactoryBot.attributes_for(:#{class_name}) }
600
-
601
- let(:mutation) do
602
- %(mutation {
603
- create#{class_name.camelize}(input: {
604
- EOS
605
- else
606
- new_line.write <<-EOS
607
- let(:#{class_name}) { FactoryBot.attributes_for(:#{class_name}, #{@relation_params.join(", ")}) }
608
-
609
- let(:mutation) do
610
- %(mutation {
611
- create#{class_name.camelize}(input: {
612
- EOS
613
- end
614
- break
615
- end
616
- _, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
617
- case name
618
- when "user_id"
619
- relation_col = name.gsub("_id", "")
620
- new_line.write " let(:#{relation_col}) { FactoryBot.create(:#{relation_col}) }\n"
621
- when /$*_id\z/
622
- relation_col = name.gsub("_id", "")
623
- @relation_params << "#{name}: get_global_key(\"#{name.singularize.camelize.gsub("Id", "")}\", #{relation_col}.id)"
624
- new_line.write " let(:#{relation_col}) { FactoryBot.create(:#{relation_col}) }\n"
625
- end
626
- end
627
- if table_check(line: line, class_name: class_name)
628
- @on = true
629
- end
630
- end
631
- end
632
- end
633
- end
634
-
635
- def rspec_mutation_params class_name: "souls"
636
- file_path = "./spec/mutations/#{class_name.singularize}_spec.rb"
637
- path = "./db/schema.rb"
638
- @on = false
639
- @user_exist = false
640
- File.open(file_path, "a") do |new_line|
641
- File.open(path, "r") do |f|
642
- f.each_line.with_index do |line, i|
643
- if @on
644
- if line.include?("end") || line.include?("t.index")
645
- new_line.write " }) {\n #{class_name.singularize.camelize(:lower)}Edge {\n node {\n"
646
- new_line.write " id\n"
647
- break
648
- end
649
- type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
650
- array_true = line.include?("array: true")
651
- case name
652
- when "created_at", "updated_at"
653
- next
654
- when "user_id"
655
- @user_exist = true
656
- when /$*_id\z/
657
- new_line.write " #{name.singularize.camelize(:lower)}: \"\#{#{class_name.singularize}[:#{name.singularize.underscore}]}\"\n"
658
- else
659
- case type
660
- when "string", "text", "date", "datetime"
661
- if array_true
662
- new_line.write " #{name.pluralize.camelize(:lower)}: \#{#{class_name.singularize}[:#{name.pluralize.underscore}]}\n"
663
- else
664
- new_line.write " #{name.singularize.camelize(:lower)}: \"\#{#{class_name.singularize}[:#{name.singularize.underscore}]}\"\n"
665
- end
666
- when "bigint", "integer", "float", "boolean"
667
- new_line.write " #{name.singularize.camelize(:lower)}: \#{#{class_name.singularize}[:#{name.singularize.underscore}]}\n"
668
- end
669
- end
670
- end
671
- if table_check(line: line, class_name: class_name)
672
- @on = true
673
- end
674
- end
675
- end
676
- end
677
- end
678
-
679
- def rspec_mutation_params_response class_name: "souls"
680
- file_path = "./spec/mutations/#{class_name.singularize}_spec.rb"
681
- path = "./db/schema.rb"
682
- @on = false
683
- File.open(file_path, "a") do |new_line|
684
- File.open(path, "r") do |f|
685
- f.each_line.with_index do |line, i|
686
- if @on
687
- if line.include?("end") || line.include?("t.index")
688
- if @user_exist
689
- new_line.write <<-EOS
690
- }
691
- }
692
- }
693
- }
694
- )
695
- end
696
-
697
- subject(:result) do
698
- context = {
699
- user: user
700
- }
701
- SoulsApiSchema.execute(mutation, context: context).as_json
702
- end
703
-
704
- it "return #{class_name.camelize} Data" do
705
- begin
706
- a1 = result.dig("data", "create#{class_name.singularize.camelize}", "#{class_name.singularize.camelize(:lower)}Edge", "node")
707
- raise unless a1.present?
708
- rescue
709
- raise StandardError, result
710
- end
711
- expect(a1).to include(
712
- "id" => be_a(String),
713
- EOS
714
- else
715
- new_line.write <<-EOS
716
- }
717
- }
718
- }
719
- }
720
- )
721
- end
722
-
723
- subject(:result) do
724
- SoulsApiSchema.execute(mutation).as_json
725
- end
726
-
727
- it "return #{class_name.camelize} Data" do
728
- begin
729
- a1 = result.dig("data", "create#{class_name.singularize.camelize}", "#{class_name.singularize.camelize(:lower)}Edge", "node")
730
- raise unless a1.present?
731
- rescue
732
- raise StandardError, result
733
- end
734
- expect(a1).to include(
735
- "id" => be_a(String),
736
- EOS
737
- end
738
- break
739
- end
740
- _, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
741
- array_true = line.include?("array: true")
742
- case name
743
- when "user_id", "created_at", "updated_at", /$*_id\z/
744
- next
745
- else
746
- if array_true
747
- new_line.write " #{name.pluralize.camelize(:lower)}\n"
748
- else
749
- new_line.write " #{name.singularize.camelize(:lower)}\n"
750
- end
751
- end
752
- end
753
- if table_check(line: line, class_name: class_name)
754
- @on = true
755
- end
756
- end
757
- end
758
- end
759
- end
760
-
761
- def rspec_mutation_end class_name: "souls"
762
- file_path = "./spec/mutations/#{class_name.singularize}_spec.rb"
763
- path = "./db/schema.rb"
764
- @on = false
765
- File.open(file_path, "a") do |new_line|
766
- File.open(path, "r") do |f|
767
- f.each_line.with_index do |line, i|
768
- if @on
769
- if line.include?("end") || line.include?("t.index")
770
- new_line.write <<~EOS
771
- )
772
- end
773
- end
774
- end
775
- EOS
776
- break
777
- end
778
- type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
779
- field ||= type_check type
780
- array_true = line.include?("array: true")
781
- case name
782
- when "user_id", "created_at", "updated_at", /$*_id\z/
783
- next
784
- else
785
- case type
786
- when "text", "date", "datetime"
787
- if array_true
788
- new_line.write " \"#{name.pluralize.camelize(:lower)}\" => be_all(String),\n"
789
- else
790
- new_line.write " \"#{name.singularize.camelize(:lower)}\" => be_a(String),\n"
791
- end
792
- when "boolean"
793
- new_line.write " \"#{name.singularize.camelize(:lower)}\" => be_in([true, false]),\n"
794
- when "string", "bigint", "integer", "float"
795
- new_line.write " \"#{name.singularize.camelize(:lower)}\" => be_a(#{field}),\n"
796
- end
797
- end
798
- end
799
- if table_check(line: line, class_name: class_name)
800
- @on = true
801
- end
802
- end
803
- end
804
- end
805
- [file_path]
806
- end
807
-
808
- def rspec_mutation class_name: "souls"
809
- singularized_class_name = class_name.singularize
810
- rspec_mutation_head class_name: singularized_class_name
811
- rspec_mutation_after_head class_name: singularized_class_name
812
- rspec_mutation_params class_name: singularized_class_name
813
- rspec_mutation_params_response class_name: singularized_class_name
814
- rspec_mutation_end class_name: singularized_class_name
815
- end
816
-
817
- def rspec_query_head class_name: "souls"
818
- file_path = "./spec/queries/#{class_name.singularize}_spec.rb"
819
- File.open(file_path, "w") do |f|
820
- f.write <<~EOS
821
- RSpec.describe \"#{class_name.camelize} Query テスト\" do
822
- describe "#{class_name.camelize} データを取得する" do
823
- EOS
824
- end
825
- end
826
-
827
- def rspec_query_after_head class_name: "souls"
828
- file_path = "./spec/queries/#{class_name.singularize}_spec.rb"
829
- path = "./db/schema.rb"
830
- @on = false
831
- @user_exist = false
832
- @relation_params = []
833
- File.open(file_path, "a") do |new_line|
834
- File.open(path, "r") do |f|
835
- f.each_line.with_index do |line, i|
836
- if @on
837
- if line.include?("end") || line.include?("t.index")
838
- if @relation_params.empty?
839
- new_line.write <<-EOS
840
- let!(:#{class_name}) { FactoryBot.create(:#{class_name}) }
841
-
842
- let(:query) do
843
- data_id = Base64.encode64("#{class_name.camelize}:\#{#{class_name.singularize.underscore}.id}")
844
- %(query {
845
- #{class_name.singularize.camelize(:lower)}(id: \\"\#{data_id}\\") {
846
- id
847
- EOS
848
- break
849
- else
850
- new_line.write <<-EOS
851
- let(:#{class_name}) { FactoryBot.create(:#{class_name}, #{@relation_params.join(", ")}) }
852
-
853
- let(:query) do
854
- data_id = Base64.encode64("#{class_name.camelize}:\#{#{class_name.singularize.underscore}.id}")
855
- %(query {
856
- #{class_name.singularize.camelize(:lower)}(id: \\"\#{data_id}\\") {
857
- id
858
- EOS
859
- break
860
- end
861
- end
862
- _, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
863
- case name
864
- when /$*_id\z/
865
- relation_col = name.gsub("_id", "")
866
- @relation_params << "#{name}: #{relation_col}.id"
867
- new_line.write " let(:#{relation_col}) { FactoryBot.create(:#{relation_col}) }\n"
868
- end
869
- end
870
- if table_check(line: line, class_name: class_name)
871
- @on = true
872
- end
873
- end
874
- end
875
- end
876
- end
877
-
878
- def rspec_query_params class_name: "souls"
879
- file_path = "./spec/queries/#{class_name.singularize}_spec.rb"
880
- path = "./db/schema.rb"
881
- @on = false
882
- File.open(file_path, "a") do |new_line|
883
- File.open(path, "r") do |f|
884
- f.each_line.with_index do |line, i|
885
- if @on
886
- if line.include?("end") || line.include?("t.index")
887
- new_line.write <<-EOS
888
- }
889
- }
890
- )
891
- end
892
-
893
- subject(:result) do
894
- SoulsApiSchema.execute(query).as_json
895
- end
896
-
897
- it "return #{class_name.camelize} Data" do
898
- begin
899
- a1 = result.dig("data", "#{class_name.singularize.camelize(:lower)}")
900
- raise unless a1.present?
901
- rescue
902
- raise StandardError, result
903
- end
904
- expect(a1).to include(
905
- "id" => be_a(String),
906
- EOS
907
- break
908
- end
909
- _, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
910
- case name
911
- when "user_id", "created_at", "updated_at", /$*_id\z/
912
- next
913
- else
914
- new_line.write " #{name.camelize(:lower)}\n"
915
- end
916
- end
917
- if table_check(line: line, class_name: class_name)
918
- @on = true
919
- end
920
- end
921
- end
922
- end
923
- end
924
-
925
- def rspec_query_end class_name: "souls"
926
- file_path = "./spec/queries/#{class_name.singularize}_spec.rb"
927
- path = "./db/schema.rb"
928
- @on = false
929
- File.open(file_path, "a") do |new_line|
930
- File.open(path, "r") do |f|
931
- f.each_line.with_index do |line, i|
932
- if @on
933
- if line.include?("end") || line.include?("t.index")
934
- new_line.write <<~EOS
935
- )
936
- end
937
- end
938
- end
939
- EOS
940
- break
941
- end
942
- type, name = line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
943
- field ||= type_check type
944
- array_true = line.include?("array: true")
945
- case name
946
- when "user_id", "created_at", "updated_at", /$*_id\z/
947
- next
948
- else
949
- case type
950
- when "text", "date", "datetime"
951
- if array_true
952
- new_line.write " \"#{name.camelize(:lower)}\" => be_all(String),\n"
953
- else
954
- new_line.write " \"#{name.camelize(:lower)}\" => be_a(String),\n"
955
- end
956
- when "boolean"
957
- new_line.write " \"#{name.singularize.camelize(:lower)}\" => be_in([true, false]),\n"
958
- when "string", "bigint", "integer", "float"
959
- new_line.write " \"#{name.singularize.camelize(:lower)}\" => be_a(#{field}),\n"
960
- end
961
- end
962
- end
963
- if table_check(line: line, class_name: class_name)
964
- @on = true
965
- end
966
- end
967
- end
968
- end
969
- [file_path]
970
- end
971
-
972
- def rspec_query class_name: "souls"
973
- singularized_class_name = class_name.singularize
974
- rspec_query_head class_name: singularized_class_name
975
- rspec_query_after_head class_name: singularized_class_name
976
- rspec_query_params class_name: singularized_class_name
977
- rspec_query_end class_name: singularized_class_name
978
- end
979
-
980
- def get_end_point
981
- file_path = "./app/graphql/types/mutation_type.rb"
982
- File.open(file_path, "r") do |f|
983
- f.each_line.with_index do |line, i|
984
- return i if line.include?("end")
985
- end
986
- end
987
- end
988
-
989
- def get_tables
990
- path = "./db/schema.rb"
991
- tables = []
992
- File.open(path, "r") do |f|
993
- f.each_line.with_index do |line, i|
994
- tables << line.split("\"")[1] if line.include?("create_table")
995
- end
996
- end
997
- tables
998
- end
999
-
1000
- def add_mutation_type class_name: "souls"
1001
- # let's do this later
1002
- end
1003
-
1004
- def add_query_type class_name: "souls"
1005
- # let's do this later
1006
- end
1007
-
1008
- def migrate class_name: "souls"
1009
- singularized_class_name = class_name.singularize
1010
- model_paths = model class_name: singularized_class_name
1011
- type_paths = type class_name: singularized_class_name
1012
- node_type_paths = node_type class_name: singularized_class_name
1013
- resolver_paths = resolver class_name: singularized_class_name
1014
- rspec_factory_paths = rspec_factory class_name: singularized_class_name
1015
- rspec_model_paths = rspec_model class_name: singularized_class_name
1016
- rspec_mutation_paths = rspec_mutation class_name: singularized_class_name
1017
- rspec_query_paths = rspec_query class_name: singularized_class_name
1018
- rspec_resolver_paths = rspec_resolver class_name: singularized_class_name
1019
- query_path = query class_name: singularized_class_name
1020
- mutation_path = mutation class_name: singularized_class_name
1021
- [
1022
- model: model_paths,
1023
- type: type_paths,
1024
- resolver: resolver_paths,
1025
- node_type: node_type_paths,
1026
- rspec_factory: rspec_factory_paths,
1027
- rspec_model: rspec_model_paths,
1028
- rspec_mutation: rspec_mutation_paths,
1029
- rspec_query: rspec_query_paths,
1030
- rspec_resolver: rspec_resolver_paths,
1031
- query: query_path,
1032
- mutation: mutation_path,
1033
- add_query_type: [
1034
- " field :#{singularized_class_name}, resolver: Queries::#{singularized_class_name.camelize}",
1035
- " field :#{singularized_class_name.pluralize}, Types::#{singularized_class_name.camelize}Type.connection_type, null: true"
1036
- ],
1037
- add_mutation_type: [
1038
- " field :create_#{singularized_class_name}, mutation: Mutations::#{singularized_class_name.camelize}::Create#{singularized_class_name.camelize}",
1039
- " field :update_#{singularized_class_name}, mutation: Mutations::#{singularized_class_name.camelize}::Update#{singularized_class_name.camelize}",
1040
- " field :delete_#{singularized_class_name}, mutation: Mutations::#{singularized_class_name.camelize}::Delete#{singularized_class_name.camelize}",
1041
- " field :destroy_delete_#{singularized_class_name}, mutation: Mutations::#{singularized_class_name.camelize}::DestroyDelete#{singularized_class_name.camelize}"
1042
- ],
1043
- add_resolver: [
1044
- " field :#{singularized_class_name}_search, resolver: Resolvers::#{singularized_class_name.camelize}Search"
1045
- ]
1046
- ]
1047
- end
1048
-
1049
- def single_migrate class_name: "user"
1050
- puts "◆◆◆ Let's Auto Generate CRUD API ◆◆◆\n"
1051
- result = migrate class_name: class_name
1052
- puts result[0][:model]
1053
- puts result[0][:type]
1054
- puts result[0][:resolver]
1055
- puts result[0][:node_type]
1056
- puts result[0][:rspec_factory]
1057
- puts result[0][:rspec_model]
1058
- puts result[0][:rspec_mutation]
1059
- puts result[0][:rspec_query]
1060
- puts result[0][:rspec_resolver]
1061
- puts result[0][:query]
1062
- puts result[0][:mutation]
1063
-
1064
- puts "\nAll files created from ./db/schema.rb"
1065
- puts "\n\n"
1066
- puts "##########################################################\n"
1067
- puts "# #\n"
1068
- puts "# Add These Lines at ./app/graphql/types/query_type.rb #\n"
1069
- puts "# #\n"
1070
- puts "##########################################################\n\n\n"
1071
- result[0][:add_query_type].each do |path|
1072
- puts path
1073
- end
1074
- puts "\n ## Resolvers\n\n"
1075
- result[0][:add_resolver].each do |path|
1076
- puts path
1077
- end
1078
- puts "\n ## Connection Type\n\n"
1079
- puts " def #{class_name.pluralize}"
1080
- puts " #{class_name.singularize.camelize}.all.order(id: :desc)"
1081
- puts " end\n\n\n"
1082
-
1083
- puts "#############################################################\n"
1084
- puts "# #\n"
1085
- puts "# Add These Lines at ./app/graphql/types/mutation_type.rb #\n"
1086
- puts "# #\n"
1087
- puts "#############################################################\n\n\n"
1088
- result[0][:add_mutation_type].each do |path|
1089
- puts path
1090
- end
1091
- end
1092
-
1093
- def migrate_all
1094
- puts "◆◆◆ Let's Auto Generate CRUD API ◆◆◆\n"
1095
- paths = get_tables.map do |class_name|
1096
- migrate class_name: class_name.singularize
1097
- end
1098
- puts "\n============== Model ======================\n\n"
1099
- paths.each do |class_name|
1100
- class_name.each do |path|
1101
- path[:model].each { |line| puts line }
1102
- end
1103
- end
1104
- puts "\n============== Type =======================\n\n"
1105
- paths.each do |class_name|
1106
- class_name.each do |path|
1107
- path[:type].each { |line| puts line }
1108
- end
1109
- end
1110
- puts "\n============== Resolver =======================\n\n"
1111
- paths.each do |class_name|
1112
- class_name.each do |path|
1113
- path[:resolver].each { |line| puts line }
1114
- end
1115
- end
1116
- puts "\n============== NodeType =======================\n\n"
1117
- paths.each do |class_name|
1118
- class_name.each do |path|
1119
- path[:node_type].each { |line| puts line }
1120
- end
1121
- end
1122
- puts "\n============== FactoryBot =================\n\n"
1123
- paths.each do |class_name|
1124
- class_name.each do |path|
1125
- path[:rspec_factory].each { |line| puts line }
1126
- end
1127
- end
1128
- puts "\n============== RspecModel =================\n\n"
1129
- paths.each do |class_name|
1130
- class_name.each do |path|
1131
- path[:rspec_model].each { |line| puts line }
1132
- end
1133
- end
1134
- puts "\n============== RspecMutation =================\n\n"
1135
- paths.each do |class_name|
1136
- class_name.each do |path|
1137
- path[:rspec_mutation].each { |line| puts line }
1138
- end
1139
- end
1140
- puts "\n============== RspecQuery =================\n\n"
1141
- paths.each do |class_name|
1142
- class_name.each do |path|
1143
- path[:rspec_query].each { |line| puts line }
1144
- end
1145
- end
1146
- puts "\n============== RspecResolver =================\n\n"
1147
- paths.each do |class_name|
1148
- class_name.each do |path|
1149
- path[:rspec_resolver].each { |line| puts line }
1150
- end
1151
- end
1152
- puts "\n============== Query ======================\n\n"
1153
- paths.each do |class_name|
1154
- class_name.each do |path|
1155
- path[:query].each { |line| puts line }
1156
- end
1157
- end
1158
- puts "\n============== Mutation ===================\n\n"
1159
- paths.each do |class_name|
1160
- class_name.each do |path|
1161
- path[:mutation].each { |line| puts line }
1162
- end
1163
- end
1164
- puts "\nAll files created from ./db/schema.rb"
1165
- puts "\n\n"
1166
- puts "\n##########################################################\n"
1167
- puts "# #\n"
1168
- puts "# Add These Lines at ./app/graphql/types/query_type.rb #\n"
1169
- puts "# #\n"
1170
- puts "##########################################################\n\n\n"
1171
- paths.each do |class_name|
1172
- class_name.each do |path|
1173
- path[:add_query_type].each { |line| puts line }
1174
- end
1175
- end
1176
- puts "\n ## Resolvers\n\n"
1177
- paths.each do |class_name|
1178
- class_name.each do |path|
1179
- path[:add_resolver].each { |line| puts line }
1180
- end
1181
- end
1182
- puts "\n ## Connection Type\n\n"
1183
- get_tables.each do |class_name|
1184
- puts " def #{class_name.pluralize}"
1185
- puts " #{class_name.singularize.camelize}.all.order(id: :desc)"
1186
- puts " end\n\n"
1187
- end
1188
- puts "\n#############################################################\n"
1189
- puts "# #\n"
1190
- puts "# Add These Lines at ./app/graphql/types/mutation_type.rb #\n"
1191
- puts "# #\n"
1192
- puts "#############################################################\n\n\n"
1193
- paths.each do |class_name|
1194
- class_name.each do |path|
1195
- path[:add_mutation_type].each { |line| puts line }
1196
- end
1197
- end
1198
- end
1199
49
  end
1200
50
  end
1201
51
  end