graphql-rails-api 0.4.5 → 0.5.0

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: f0d802c95081098c189f0b5ee0fb282169533e275028429879ba4ceb218dca17
4
- data.tar.gz: ae724c83294886154d99c56d711c1aaa5a64e333ee5a8def9c021b819a3fcbc8
3
+ metadata.gz: f6402207e57c8de721a10386de5110da04b1bfdacc4b5c81c86acae75ef45e65
4
+ data.tar.gz: f06272fc40c35d3c7f27df5fcec19e6ed967296fa739559df0d90ab8b21e6847
5
5
  SHA512:
6
- metadata.gz: cf4529de02e31bb8a97b78c94fcc04a7b0e2230ceabcd8e32a49a72ac67233aa1f982ff542b01464038245429584f2ba7edbf1e351a940ba03d72cc84c42fcce
7
- data.tar.gz: ea107d69a51fa296378a07864f4b05bb7aa46452e0d2fd63edabb9a93394cae6e185737f873e6f3bf304deae2f3016c9ddf83b836ac112b6ecce009b2888b6f2
6
+ metadata.gz: 403a79ef9705821684ed7d00283542db925d41ad6cfd40741c5d954d0cb9e0b682286745d01fc70ebf893ba002502133c56b58a21aa72e773e1b4119c96b3163
7
+ data.tar.gz: e388d99bd961ca380843d8d65b33beba46249373c70ef44d3793797bd5e7a63799b51eb59a62a730b535a879babd1f54033a3ea6bc33d2e75ab12c1d1844ce47
@@ -3,7 +3,7 @@ class GraphqlAllConnectionsGenerator < Rails::Generators::NamedBase
3
3
  def generate
4
4
  Graphql::Rails::Api::Config.query_resources.each do |resource|
5
5
  dir = "app/graphql/#{resource.pluralize}"
6
- generate_connection(dir, resource)
6
+ generate_connection(dir, resource) if Dir.exist?(dir)
7
7
  end
8
8
  end
9
9
 
@@ -0,0 +1,30 @@
1
+ class GraphqlBulkCreateMutationsGenerator < Rails::Generators::NamedBase
2
+
3
+ def generate
4
+ Graphql::Rails::Api::Config.query_resources.each do |resource|
5
+ dir = "#{Rails.root}/app/graphql/#{resource.pluralize}/mutations"
6
+ generate_bulk_create_mutation(dir, resource) if Dir.exist?(dir)
7
+ end
8
+ end
9
+
10
+ def generate_bulk_create_mutation(dir, resource)
11
+ File.write(
12
+ "#{dir}/bulk_create.rb",
13
+ <<~STRING
14
+ #{resource_class(resource)}::Mutations::BulkCreate = GraphQL::Field.define do
15
+ description 'creates some #{resource_class(resource).pluralize}'
16
+ type types[#{resource_class(resource)}::Type]
17
+
18
+ argument :#{resource}, types[#{resource_class(resource)}::Mutations::InputType]
19
+
20
+ resolve ApplicationService.call(:#{resource}, :bulk_create)
21
+ end
22
+ STRING
23
+ )
24
+ end
25
+
26
+ def resource_class(resource)
27
+ resource.pluralize.camelize
28
+ end
29
+
30
+ end
@@ -0,0 +1,43 @@
1
+ class GraphqlBulkUpdateMutationsGenerator < Rails::Generators::NamedBase
2
+
3
+ def generate
4
+ Graphql::Rails::Api::Config.query_resources.each do |resource|
5
+ dir = "#{Rails.root}/app/graphql/#{resource.pluralize}/mutations"
6
+ generate_bulk_update_mutation(dir, resource) if Dir.exist?(dir)
7
+ end
8
+ end
9
+
10
+ def generate_bulk_update_mutation(dir, resource)
11
+ File.write(
12
+ "#{dir}/bulk_update.rb",
13
+ <<~STRING
14
+ #{resource_class(resource)}::Mutations::BulkUpdate = GraphQL::Field.define do
15
+ description 'Updates some #{resource_class(resource).pluralize}'
16
+ type types[#{resource_class(resource)}::Type]
17
+
18
+ argument :#{resource}, types[#{resource_class(resource)}::Mutations::InputType]
19
+
20
+ resolve ApplicationService.call(:#{resource}, :bulk_update)
21
+ end
22
+ STRING
23
+ )
24
+ end
25
+
26
+ def resource_class(resource)
27
+ resource.pluralize.camelize
28
+ end
29
+
30
+ def write_at(file_name, line, data)
31
+ open(file_name, 'r+') do |f|
32
+ while (line -= 1).positive?
33
+ f.readline
34
+ end
35
+ pos = f.pos
36
+ rest = f.read
37
+ f.seek pos
38
+ f.write data
39
+ f.write rest
40
+ end
41
+ end
42
+
43
+ end
@@ -86,11 +86,11 @@ class GraphqlResourceGenerator < Rails::Generators::NamedBase
86
86
 
87
87
  def generate_create_migration(resource, fields)
88
88
  system("bundle exec rails generate migration create_#{resource} --skip")
89
- migration_file = Dir.glob("db/migrate/*create_#{resource}*").last
89
+ migration_file = Dir.glob("db/migrate/*create_#{resource}.rb").last
90
90
  File.write(
91
91
  migration_file,
92
92
  <<~STRING
93
- class Create#{resource.camelize} < ActiveRecord::Migration[5.1]
93
+ class Create#{resource.camelize} < ActiveRecord::Migration[5.2]
94
94
  def change
95
95
  create_table :#{resource.pluralize}, #{'id: :uuid ' if Graphql::Rails::Api::Config.instance.id_type == :uuid}do |t|
96
96
  #{fields}
@@ -290,19 +290,29 @@ t.#{@id_db_type} :#{resource.underscore.singularize}_id
290
290
 
291
291
  def add_to_model(model, line)
292
292
  file_name = "app/models/#{model.underscore.singularize}.rb"
293
- return unless File.exist?(file_name)
294
- return if File.read(file_name).include?(line)
295
- write_at(file_name, 3, " #{line}\n")
293
+ return if !File.exist?(file_name) || File.read(file_name).include?(line)
294
+
295
+ line_count = `wc -l "#{file_name}"`.strip.split(' ')[0].to_i
296
+
297
+ line_nb = 0
298
+ File.open(file_name).each do |l|
299
+ line_nb += 1
300
+ break if l.include?('ApplicationRecord')
301
+ end
302
+ raise 'Your model must inherit from ApplicationRecord to make it work' if line_nb >= line_count
303
+
304
+ write_at(file_name, line_nb + 2, " #{line}\n")
296
305
  end
297
306
 
298
307
  def generate_has_many_migration(resource, has_many:)
299
308
  return if has_many.singularize.camelize.constantize.new.respond_to?("#{resource.singularize}_id")
309
+
300
310
  system("bundle exec rails generate migration add_#{resource.singularize}_id_to_#{has_many}")
301
- migration_file = Dir.glob("db/migrate/*add_#{resource.singularize}_id_to_#{has_many}*").last
311
+ migration_file = Dir.glob("db/migrate/*add_#{resource.singularize}_id_to_#{has_many}.rb").last
302
312
  File.write(
303
313
  migration_file,
304
314
  <<~STRING
305
- class Add#{resource.singularize.camelize}IdTo#{has_many.camelize} < ActiveRecord::Migration[5.1]
315
+ class Add#{resource.singularize.camelize}IdTo#{has_many.camelize} < ActiveRecord::Migration[5.2]
306
316
  def change
307
317
  add_column :#{has_many.pluralize}, :#{resource.singularize}_id, :#{@id_db_type}
308
318
  end
@@ -14,7 +14,7 @@ module Graphql
14
14
 
15
15
  def run
16
16
  @model = @model.where(id: @id) if @id
17
- plucked = @model.deep_pluck(*hash_to_array_of_hashes(parse_fields(@fields), @model))
17
+ plucked = @model.deep_pluck(*hash_to_array_of_hashes(fields_to_hash(@fields), @model))
18
18
  result = plucked_attr_to_structs(plucked, model_name.singularize.camelize.constantize)&.compact
19
19
  @id ? result.first : result
20
20
  end
@@ -30,8 +30,10 @@ module Graphql
30
30
  end
31
31
 
32
32
  hash.each_with_object(OpenStruct.new) do |(k, v), struct|
33
- next struct[k.to_sym] = plucked_attr_to_structs(v, evaluate_model(parent_model, k)) if v.is_a?(Array)
34
- next struct[k.to_sym] = hash_to_struct(v, evaluate_model(parent_model, k)) if v.is_a?(Hash)
33
+ m = evaluate_model(parent_model, k)
34
+ next struct[k.to_sym] = plucked_attr_to_structs(v, m) if v.is_a?(Array) && m
35
+
36
+ next struct[k.to_sym] = hash_to_struct(v, m) if v.is_a?(Hash) && m
35
37
 
36
38
  struct[k.to_sym] = v
37
39
  end
@@ -39,10 +41,10 @@ module Graphql
39
41
 
40
42
  def visibility_hash
41
43
  @visibility_hash ||= @models.reject(&:blank?).each_with_object({}) do |model, hash|
42
- visible = model.constantize.visible_for(user: @user)
43
- next if visible.blank?
44
+ visible_ids = model.constantize.visible_for(user: @user)&.pluck(:id)
45
+ next if visible_ids.blank?
44
46
 
45
- hash[model.constantize] = visible.pluck(:id)
47
+ hash[model.constantize] = visible_ids
46
48
  end
47
49
  end
48
50
 
@@ -50,8 +52,10 @@ module Graphql
50
52
  return if parent_class.nil?
51
53
 
52
54
  hash['id'] = nil if hash['id'].blank?
55
+ fetch_ids_from_relation(hash)
56
+
53
57
  hash.each_with_object([]) do |(k, v), arr|
54
- next arr << k if v.blank? && parent_class.new.attributes.key?(k)
58
+ next arr << k if parent_class.new.attributes.key?(k)
55
59
 
56
60
  klass = evaluate_model(parent_class, k)
57
61
  @models << klass.to_s unless @models.include?(klass.to_s)
@@ -59,6 +63,17 @@ module Graphql
59
63
  end
60
64
  end
61
65
 
66
+ def fetch_ids_from_relation(hash)
67
+ hash.select { |k, _| k.ends_with?('_ids') }.each do |(k, _)|
68
+ collection_name = k.gsub('_ids', '').pluralize
69
+ if hash[collection_name].blank?
70
+ hash[collection_name] = { 'id' => nil }
71
+ else
72
+ hash[collection_name]['id'] = nil
73
+ end
74
+ end
75
+ end
76
+
62
77
  def activerecord_model?(name)
63
78
  class_name = name.to_s.singularize.camelize
64
79
  begin
@@ -77,9 +92,9 @@ module Graphql
77
92
  parent_class_name.constantize.reflections[child.to_s.underscore]&.klass
78
93
  end
79
94
 
80
- def parse_fields(fields)
95
+ def fields_to_hash(fields)
81
96
  fields.each_with_object({}) do |(k, v), h|
82
- h[k] = v.scoped_children == {} ? nil : parse_fields(v.scoped_children.values.first)
97
+ h[k] = v.scoped_children == {} ? nil : fields_to_hash(v.scoped_children.values.first)
83
98
  end
84
99
  end
85
100
 
@@ -1,7 +1,7 @@
1
1
  module Graphql
2
2
  module Rails
3
3
  module Api
4
- VERSION = '0.4.5'.freeze
4
+ VERSION = '0.5.0'.freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-rails-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - poilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-04 00:00:00.000000000 Z
11
+ date: 2018-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -76,6 +76,8 @@ files:
76
76
  - README.md
77
77
  - Rakefile
78
78
  - lib/generators/graphql_connections/graphql_all_connections_generator.rb
79
+ - lib/generators/graphql_mutations/graphql_bulk_create_mutations_generator.rb
80
+ - lib/generators/graphql_mutations/graphql_bulk_update_mutations_generator.rb
79
81
  - lib/generators/graphql_mutations/graphql_mutations_generator.rb
80
82
  - lib/generators/graphql_rails_api/install_generator.rb
81
83
  - lib/generators/graphql_resource/USAGE