graphql-rails-api 0.4.5 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/generators/graphql_connections/graphql_all_connections_generator.rb +1 -1
- data/lib/generators/graphql_mutations/graphql_bulk_create_mutations_generator.rb +30 -0
- data/lib/generators/graphql_mutations/graphql_bulk_update_mutations_generator.rb +43 -0
- data/lib/generators/graphql_resource/graphql_resource_generator.rb +17 -7
- data/lib/graphql/hydrate_query.rb +24 -9
- data/lib/graphql/rails/api/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6402207e57c8de721a10386de5110da04b1bfdacc4b5c81c86acae75ef45e65
|
4
|
+
data.tar.gz: f06272fc40c35d3c7f27df5fcec19e6ed967296fa739559df0d90ab8b21e6847
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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}
|
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.
|
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
|
294
|
-
|
295
|
-
|
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}
|
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.
|
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(
|
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
|
-
|
34
|
-
next struct[k.to_sym] =
|
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
|
-
|
43
|
-
next if
|
44
|
+
visible_ids = model.constantize.visible_for(user: @user)&.pluck(:id)
|
45
|
+
next if visible_ids.blank?
|
44
46
|
|
45
|
-
hash[model.constantize] =
|
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
|
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
|
95
|
+
def fields_to_hash(fields)
|
81
96
|
fields.each_with_object({}) do |(k, v), h|
|
82
|
-
h[k] = v.scoped_children == {} ? nil :
|
97
|
+
h[k] = v.scoped_children == {} ? nil : fields_to_hash(v.scoped_children.values.first)
|
83
98
|
end
|
84
99
|
end
|
85
100
|
|
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
|
+
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-
|
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
|