graphql-rails-api 0.9.7 → 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -18
- data/lib/generators/auth0_authentication/auth0_authentication_generator.rb +4 -0
- data/lib/generators/graphql_add_fields/graphql_add_fields_generator.rb +36 -38
- data/lib/generators/graphql_rails_api/install_generator.rb +160 -162
- data/lib/graphql/hydrate_query.rb +5 -4
- data/lib/graphql/rails/api/version.rb +1 -1
- metadata +3 -6
- data/lib/generators/graphql_all_connections/graphql_all_connections_generator.rb +0 -26
- data/lib/generators/graphql_mutations/graphql_bulk_create_mutations_generator.rb +0 -30
- data/lib/generators/graphql_mutations/graphql_bulk_update_mutations_generator.rb +0 -30
- data/lib/generators/graphql_mutations/graphql_mutations_generator.rb +0 -92
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acf07a5b93e2cd63fd09cd0cd8081a5ee75f89e085f48405dfa197e5684f4b72
|
4
|
+
data.tar.gz: 7bb866a32483d31ba22a7283915484154b91f0720dab5fbb6b4477e298b8c670
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d413ded07452606a26e58d23e7b6e000cf48dfcf990e7b6d68329587ac5e19a0aa7ad6d336fb193ac90a67cf4a2d63573b5dbc9add3a804f05cb26df5ad26a4
|
7
|
+
data.tar.gz: 030c84889982ffc7dc9bbe498079aa418851c314b299f1bec681b56cd6cfc7f12660ddcb24c99d42bbf4fc956a3ecb8f843c57fd039391d3d4f419f52bae8304
|
data/README.md
CHANGED
@@ -227,24 +227,6 @@ mutation($id: String!) {
|
|
227
227
|
}
|
228
228
|
```
|
229
229
|
|
230
|
-
bulk_create mutation:
|
231
|
-
```gql
|
232
|
-
mutation($cities: [CityInputType]!) {
|
233
|
-
bulk_create_city(cities: $cities) {
|
234
|
-
id
|
235
|
-
}
|
236
|
-
}
|
237
|
-
```
|
238
|
-
|
239
|
-
bulk_update:
|
240
|
-
```gql
|
241
|
-
mutation($cities: [CityInputType]!) {
|
242
|
-
bulk_update_city(cities: $cities) {
|
243
|
-
id
|
244
|
-
}
|
245
|
-
}
|
246
|
-
```
|
247
|
-
|
248
230
|
You can override the default application service for all mutation by defining your own method into the corresponding graphql service:
|
249
231
|
|
250
232
|
Example:
|
@@ -1,20 +1,19 @@
|
|
1
1
|
class GraphqlAddFieldsGenerator < Rails::Generators::NamedBase
|
2
|
-
|
3
2
|
%i[
|
4
3
|
migration model mutations service graphql_input_type
|
5
|
-
graphql_type propagation
|
4
|
+
graphql_type propagation migrate
|
6
5
|
].each do |opt|
|
7
6
|
class_option(opt, type: :boolean, default: true)
|
8
7
|
end
|
9
8
|
|
10
9
|
TYPES_MAPPING = {
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
"id" => "String",
|
11
|
+
"uuid" => "String",
|
12
|
+
"boolean" => "Boolean",
|
13
|
+
"float" => "Float",
|
14
|
+
"decimal" => "Float",
|
15
|
+
"integer" => "Integer",
|
16
|
+
"bigint" => "Integer"
|
18
17
|
}.freeze
|
19
18
|
|
20
19
|
def create_graphql_files
|
@@ -41,7 +40,7 @@ class GraphqlAddFieldsGenerator < Rails::Generators::NamedBase
|
|
41
40
|
private
|
42
41
|
|
43
42
|
def types_mapping(type)
|
44
|
-
TYPES_MAPPING[type] ||
|
43
|
+
TYPES_MAPPING[type] || "String"
|
45
44
|
end
|
46
45
|
|
47
46
|
def complete_graphql_input_type
|
@@ -57,8 +56,8 @@ class GraphqlAddFieldsGenerator < Rails::Generators::NamedBase
|
|
57
56
|
end
|
58
57
|
|
59
58
|
def parse_args
|
60
|
-
@id_db_type =
|
61
|
-
@id_type =
|
59
|
+
@id_db_type = "uuid"
|
60
|
+
@id_type = "String"
|
62
61
|
|
63
62
|
@resource = file_name.singularize
|
64
63
|
@has_many = []
|
@@ -67,23 +66,23 @@ class GraphqlAddFieldsGenerator < Rails::Generators::NamedBase
|
|
67
66
|
@belongs_to_fields = {}
|
68
67
|
|
69
68
|
@args = args.each_with_object({}) do |f, hash|
|
70
|
-
next if f.split(
|
71
|
-
|
72
|
-
case f.split(
|
73
|
-
when
|
74
|
-
hash["#{f.split(
|
75
|
-
@belongs_to_fields["#{f.split(
|
76
|
-
when
|
77
|
-
when
|
69
|
+
next if f.split(":").count != 2
|
70
|
+
|
71
|
+
case f.split(":").first
|
72
|
+
when "belongs_to"
|
73
|
+
hash["#{f.split(":").last.singularize}_id"] = @id_db_type
|
74
|
+
@belongs_to_fields["#{f.split(":").last.singularize}_id"] = @id_db_type
|
75
|
+
when "has_many" then @has_many << f.split(":").last.pluralize
|
76
|
+
when "many_to_many" then @many_to_many << f.split(":").last.pluralize
|
78
77
|
else
|
79
|
-
hash[f.split(
|
78
|
+
hash[f.split(":").first] = f.split(":").last
|
80
79
|
end
|
81
80
|
end
|
82
81
|
|
83
82
|
@fields_to_migration = @args.map do |f|
|
84
|
-
"add_column :#{@resource.pluralize}, :#{f.join(
|
83
|
+
"add_column :#{@resource.pluralize}, :#{f.join(", :")}"
|
85
84
|
end.join("\n ")
|
86
|
-
@named_fields = @args.keys.join(
|
85
|
+
@named_fields = @args.keys.join("_")
|
87
86
|
end
|
88
87
|
|
89
88
|
def generate_migration(resource, fields)
|
@@ -159,8 +158,8 @@ class GraphqlAddFieldsGenerator < Rails::Generators::NamedBase
|
|
159
158
|
add_belongs_to_field_to_type(resource, f)
|
160
159
|
end
|
161
160
|
@belongs_to_fields.each do |f, _|
|
162
|
-
add_has_many_fields_to_type(f.gsub(
|
163
|
-
add_belongs_to_field_to_type(f.gsub(
|
161
|
+
add_has_many_fields_to_type(f.gsub("_id", ""), resource)
|
162
|
+
add_belongs_to_field_to_type(f.gsub("_id", ""), resource)
|
164
163
|
end
|
165
164
|
end
|
166
165
|
|
@@ -179,9 +178,9 @@ class GraphqlAddFieldsGenerator < Rails::Generators::NamedBase
|
|
179
178
|
@many_to_many.each do |field|
|
180
179
|
generate_create_migration(
|
181
180
|
"#{resource}_#{field}",
|
182
|
-
|
183
|
-
t.#{@id_db_type} :#{resource.underscore.singularize}_id
|
184
|
-
|
181
|
+
<<~STRING
|
182
|
+
t.#{@id_db_type} :#{resource.underscore.singularize}_id
|
183
|
+
t.#{@id_db_type} :#{field.underscore.singularize}_id
|
185
184
|
STRING
|
186
185
|
)
|
187
186
|
generate_empty_model("#{resource}_#{field.singularize}")
|
@@ -203,7 +202,7 @@ t.#{@id_db_type} :#{resource.underscore.singularize}_id
|
|
203
202
|
add_to_model(field, "belongs_to :#{resource.singularize}")
|
204
203
|
end
|
205
204
|
@belongs_to_fields.each do |k, _|
|
206
|
-
field = k.gsub(
|
205
|
+
field = k.gsub("_id", "")
|
207
206
|
add_to_model(field, "has_many :#{resource.pluralize}")
|
208
207
|
add_to_model(resource, "belongs_to :#{field.singularize}")
|
209
208
|
end
|
@@ -213,14 +212,14 @@ t.#{@id_db_type} :#{resource.underscore.singularize}_id
|
|
213
212
|
result = args&.map do |k, v|
|
214
213
|
field_name = k
|
215
214
|
field_type = types_mapping(v)
|
216
|
-
res = "#{input_type ?
|
217
|
-
if !input_type && field_name.ends_with?(
|
218
|
-
res += "\n field :#{field_name.gsub(
|
219
|
-
"#{field_name.gsub(
|
215
|
+
res = "#{input_type ? "argument" : "field"} :#{field_name}, #{field_type}, #{input_type ? "required: false" : "null: true"}"
|
216
|
+
if !input_type && field_name.ends_with?("_id") && @belongs_to_fields.key?(field_name)
|
217
|
+
res += "\n field :#{field_name.gsub("_id", "")}, " \
|
218
|
+
"#{field_name.gsub("_id", "").pluralize.camelize}::Type"
|
220
219
|
end
|
221
220
|
res
|
222
221
|
end&.join("\n " + (" " if input_type).to_s)
|
223
|
-
input_type ? result.gsub("field :id, #{@id_type}, null: false\n",
|
222
|
+
input_type ? result.gsub("field :id, #{@id_type}, null: false\n", "") : result
|
224
223
|
end
|
225
224
|
|
226
225
|
# Helpers methods
|
@@ -238,9 +237,9 @@ t.#{@id_db_type} :#{resource.underscore.singularize}_id
|
|
238
237
|
line_nb = 0
|
239
238
|
IO.readlines(file).each do |l|
|
240
239
|
line_nb += 1
|
241
|
-
break if l.include?(
|
240
|
+
break if l.include?("ApplicationRecord")
|
242
241
|
end
|
243
|
-
raise
|
242
|
+
raise "Your model must inherit from ApplicationRecord to make it work" if line_nb >= line_count
|
244
243
|
|
245
244
|
write_at(file_name, line_nb + 1, " #{line}\n")
|
246
245
|
end
|
@@ -285,7 +284,7 @@ t.#{@id_db_type} :#{resource.underscore.singularize}_id
|
|
285
284
|
end
|
286
285
|
|
287
286
|
def write_at(file_name, line, data)
|
288
|
-
open(file_name,
|
287
|
+
open(file_name, "r+") do |f|
|
289
288
|
while (line -= 1).positive?
|
290
289
|
f.readline
|
291
290
|
end
|
@@ -296,5 +295,4 @@ t.#{@id_db_type} :#{resource.underscore.singularize}_id
|
|
296
295
|
f.write(rest)
|
297
296
|
end
|
298
297
|
end
|
299
|
-
|
300
298
|
end
|
@@ -1,14 +1,13 @@
|
|
1
|
-
require
|
1
|
+
require "graphql/rails/api/config"
|
2
2
|
|
3
3
|
module GraphqlRailsApi
|
4
4
|
class InstallGenerator < Rails::Generators::Base
|
5
|
-
|
6
|
-
class_option('generate_graphql_route', type: :boolean, default: true)
|
5
|
+
class_option("generate_graphql_route", type: :boolean, default: true)
|
7
6
|
|
8
7
|
def generate_files
|
9
8
|
@app_name = File.basename(Rails.root.to_s).underscore
|
10
9
|
|
11
|
-
folder =
|
10
|
+
folder = "app/graphql/"
|
12
11
|
FileUtils.mkdir_p(folder) unless File.directory?(folder)
|
13
12
|
|
14
13
|
write_uuid_extensions_migration
|
@@ -21,22 +20,23 @@ module GraphqlRailsApi
|
|
21
20
|
|
22
21
|
write_controller
|
23
22
|
|
24
|
-
system
|
23
|
+
system "rails g graphql_resource user first_name:string last_name:string email:string"
|
25
24
|
|
26
25
|
write_application_record_methods
|
27
26
|
write_initializer
|
28
27
|
write_require_application_rb
|
28
|
+
|
29
29
|
write_route if options.generate_graphql_route?
|
30
30
|
end
|
31
31
|
|
32
32
|
private
|
33
33
|
|
34
34
|
def write_route
|
35
|
-
route_file = File.read(
|
36
|
-
return if route_file.include?(
|
35
|
+
route_file = File.read("config/routes.rb")
|
36
|
+
return if route_file.include?("graphql")
|
37
37
|
|
38
38
|
File.write(
|
39
|
-
|
39
|
+
"config/routes.rb",
|
40
40
|
route_file.gsub(
|
41
41
|
"Rails.application.routes.draw do\n",
|
42
42
|
"Rails.application.routes.draw do\n" \
|
@@ -47,7 +47,7 @@ module GraphqlRailsApi
|
|
47
47
|
|
48
48
|
def write_collection_ids_resolver
|
49
49
|
File.write(
|
50
|
-
|
50
|
+
"app/graphql/collection_ids_resolver.rb",
|
51
51
|
<<~STRING
|
52
52
|
class CollectionIdsResolver
|
53
53
|
|
@@ -65,14 +65,13 @@ module GraphqlRailsApi
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def write_application_record_methods
|
68
|
-
lines_count = File.read(
|
68
|
+
lines_count = File.read("app/models/application_record.rb").lines.count
|
69
69
|
|
70
|
-
return if File.read(
|
70
|
+
return if File.read("app/models/application_record.rb").include?("def self.visible_for")
|
71
71
|
write_at(
|
72
|
-
|
72
|
+
"app/models/application_record.rb",
|
73
73
|
lines_count,
|
74
74
|
<<-STRING
|
75
|
-
|
76
75
|
def self.visible_for(*)
|
77
76
|
all
|
78
77
|
end
|
@@ -82,17 +81,17 @@ module GraphqlRailsApi
|
|
82
81
|
end
|
83
82
|
|
84
83
|
|
85
|
-
|
86
|
-
|
87
|
-
end
|
84
|
+
STRING
|
85
|
+
)
|
86
|
+
end
|
88
87
|
|
89
88
|
def write_require_application_rb
|
90
|
-
write_at(
|
89
|
+
write_at("config/application.rb", 5, "require 'graphql/hydrate_query'\nrequire 'rkelly'\nrequire 'graphql'\n")
|
91
90
|
end
|
92
91
|
|
93
92
|
def write_uuid_extensions_migration
|
94
|
-
system(
|
95
|
-
migration_file = Dir.glob(
|
93
|
+
system("bundle exec rails generate migration uuid_pg_extensions --skip")
|
94
|
+
migration_file = Dir.glob("db/migrate/*uuid_pg_extensions*").last
|
96
95
|
File.write(
|
97
96
|
migration_file,
|
98
97
|
<<~STRING
|
@@ -109,7 +108,7 @@ end
|
|
109
108
|
|
110
109
|
def write_initializer
|
111
110
|
File.write(
|
112
|
-
|
111
|
+
"config/initializers/graphql_rails_api_config.rb",
|
113
112
|
<<~STRING
|
114
113
|
require 'graphql/rails/api/config'
|
115
114
|
|
@@ -120,7 +119,7 @@ end
|
|
120
119
|
|
121
120
|
def write_controller
|
122
121
|
File.write(
|
123
|
-
|
122
|
+
"app/controllers/graphql_controller.rb",
|
124
123
|
<<~STRING
|
125
124
|
class GraphqlController < ApplicationController
|
126
125
|
|
@@ -162,7 +161,7 @@ end
|
|
162
161
|
|
163
162
|
def write_mutation_type
|
164
163
|
File.write(
|
165
|
-
|
164
|
+
"app/graphql/mutation_type.rb",
|
166
165
|
<<~'STRING'
|
167
166
|
class MutationType < GraphQL::Schema::Object
|
168
167
|
graphql_name "Mutation"
|
@@ -183,7 +182,7 @@ end
|
|
183
182
|
|
184
183
|
def write_query_type
|
185
184
|
File.write(
|
186
|
-
|
185
|
+
"app/graphql/query_type.rb",
|
187
186
|
<<~'STRING'
|
188
187
|
class QueryType < GraphQL::Schema::Object
|
189
188
|
description "The query root of this schema"
|
@@ -263,152 +262,152 @@ end
|
|
263
262
|
File.write(
|
264
263
|
"app/graphql/#{@app_name}_schema.rb",
|
265
264
|
<<~STRING
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
265
|
+
class #{@app_name.camelize}Schema < GraphQL::Schema
|
266
|
+
query QueryType
|
267
|
+
mutation MutationType
|
268
|
+
max_depth 15
|
269
|
+
|
270
|
+
def self.type_error(err, query_ctx)
|
271
|
+
type_error_logger = Logger.new("#{Rails.root}/log/graphql_type_errors.log")
|
272
|
+
|
273
|
+
type_error_logger.error(
|
274
|
+
"#{err} for #{query_ctx.query.query_string} with #{query_ctx.query.provided_variables}"
|
275
|
+
)
|
276
|
+
end
|
277
|
+
end
|
278
|
+
<<~STRING
|
279
|
+
)
|
278
280
|
end
|
279
|
-
<<~STRING
|
280
|
-
)
|
281
|
-
end
|
282
|
-
|
283
|
-
def write_service
|
284
|
-
File.write(
|
285
|
-
'app/graphql/application_service.rb',
|
286
|
-
<<~'STRING'
|
287
|
-
class ApplicationService
|
288
281
|
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
282
|
+
def write_service
|
283
|
+
File.write(
|
284
|
+
'app/graphql/application_service.rb',
|
285
|
+
<<~'STRING'
|
286
|
+
class ApplicationService
|
287
|
+
|
288
|
+
attr_accessor :params, :object, :fields, :user
|
289
|
+
|
290
|
+
def initialize(params: {}, object: nil, object_id: nil, user: nil, context: nil)
|
291
|
+
@params = params.is_a?(Array) ? params.map { |p| p.to_h.symbolize_keys } : params.to_h.symbolize_keys
|
292
|
+
@context = context
|
293
|
+
@object = object || (object_id && model.visible_for(user: user).find_by(id: object_id))
|
294
|
+
@object_id = object_id
|
295
|
+
@user = user
|
296
|
+
end
|
297
|
+
|
298
|
+
def self.call(resource, meth)
|
299
|
+
lambda { |_obj, args, context|
|
300
|
+
params = args && args[resource] ? args[resource] : args
|
301
|
+
"#{resource.to_s.pluralize.camelize.constantize}::Service".constantize.new(
|
302
|
+
params: params, user: context[:current_user],
|
303
|
+
object_id: args[:id], context: context
|
304
|
+
).send(meth)
|
305
|
+
}
|
306
|
+
end
|
307
|
+
|
308
|
+
def index
|
309
|
+
Graphql::HydrateQuery.new(
|
310
|
+
model.all,
|
311
|
+
@context,
|
312
|
+
order_by: params[:order_by],
|
313
|
+
filter: params[:filter],
|
314
|
+
per_page: params[:per_page],
|
315
|
+
page: params[:page],
|
316
|
+
user: user
|
317
|
+
).run.compact
|
318
|
+
end
|
319
|
+
|
320
|
+
def show
|
321
|
+
object = Graphql::HydrateQuery.new(model.all, @context, user: user, id: params[:id]).run
|
322
|
+
return not_allowed if object.blank?
|
323
|
+
|
324
|
+
object
|
325
|
+
end
|
326
|
+
|
327
|
+
def create
|
328
|
+
object = model.new(params.select { |p| model.new.respond_to?(p) })
|
329
|
+
return not_allowed if not_allowed_to_create_resource(object)
|
330
|
+
|
331
|
+
if object.save
|
332
|
+
{ singular_resource => Graphql::HydrateQuery.new(model.all, @context, user: user, id: object.id).run }
|
333
|
+
else
|
334
|
+
graphql_error(object.errors.full_messages.join(', '))
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
def update
|
339
|
+
return not_allowed if write_not_allowed
|
340
|
+
|
341
|
+
if object.update_attributes(params)
|
342
|
+
{ singular_resource => Graphql::HydrateQuery.new(model.all, @context, user: user, id: object.id).run }
|
343
|
+
else
|
344
|
+
graphql_error(object.errors.full_messages.join(', '))
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
def destroy
|
349
|
+
object = model.find_by(id: params[:id])
|
350
|
+
return not_allowed if write_not_allowed
|
351
|
+
|
352
|
+
if object.destroy
|
353
|
+
{ singular_resource => object.attributes }
|
354
|
+
else
|
355
|
+
graphql_error(object.errors.full_messages.join(', '))
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
private
|
360
|
+
|
361
|
+
def write_not_allowed
|
362
|
+
!model.visible_for(user: user).include?(object) if object
|
363
|
+
end
|
364
|
+
|
365
|
+
def access_not_allowed
|
366
|
+
!model.visible_for(user: user).include?(object) if object
|
367
|
+
end
|
368
|
+
|
369
|
+
def not_allowed
|
370
|
+
graphql_error('403 - Not allowed')
|
371
|
+
end
|
372
|
+
|
373
|
+
def graphql_error(message)
|
374
|
+
GraphQL::ExecutionError.new(message)
|
375
|
+
end
|
376
|
+
|
377
|
+
def singular_resource
|
378
|
+
resource_name.singularize
|
379
|
+
end
|
380
|
+
|
381
|
+
def model
|
382
|
+
singular_resource.camelize.constantize
|
383
|
+
end
|
384
|
+
|
385
|
+
def resource_name
|
386
|
+
self.class.to_s.split(':').first.underscore
|
387
|
+
end
|
388
|
+
|
389
|
+
def not_allowed_to_create_resource(created_resource)
|
390
|
+
params.select { |k, _| k.to_s.end_with?('_id') }.each do |belongs_relation, rel_id|
|
391
|
+
klass = created_resource.class.reflect_on_association(belongs_relation.to_s.gsub('_id', '')).klass
|
392
|
+
return true if rel_id.present? && !klass.visible_for(user: user).pluck(:id).include?(rel_id)
|
393
|
+
end
|
394
|
+
|
395
|
+
params.select { |k, _| k.to_s.end_with?('_ids') }.each do |many_relation, rel_ids|
|
396
|
+
klass = created_resource.class.reflect_on_association(many_relation.to_s.gsub('_ids', '').pluralize).klass
|
397
|
+
ids = klass.visible_for(user: user).pluck(:id)
|
398
|
+
rel_ids.each { |id| return true if id.present? && !ids.include?(id) }
|
399
|
+
end
|
400
|
+
false
|
401
|
+
end
|
402
|
+
|
403
|
+
end
|
405
404
|
|
406
405
|
STRING
|
407
406
|
)
|
408
407
|
end
|
409
408
|
|
410
409
|
def write_at(file_name, line, data)
|
411
|
-
open(file_name,
|
410
|
+
open(file_name, "r+") do |f|
|
412
411
|
while (line -= 1).positive?
|
413
412
|
f.readline
|
414
413
|
end
|
@@ -419,6 +418,5 @@ end
|
|
419
418
|
f.write rest
|
420
419
|
end
|
421
420
|
end
|
422
|
-
|
423
421
|
end
|
424
422
|
end
|
@@ -451,11 +451,12 @@ module Graphql
|
|
451
451
|
end
|
452
452
|
|
453
453
|
def irep_node(name, paginated: false)
|
454
|
-
|
454
|
+
children = @context.query.lookahead.ast_nodes.first.children[@context[:query_index]].children
|
455
455
|
result = if paginated
|
456
|
-
|
456
|
+
children.find { |n| n.name == "data" }.children
|
457
457
|
else
|
458
|
-
|
458
|
+
prepend = children.find { |n| n.name == model_name.singularize }
|
459
|
+
prepend.present? ? prepend.children : children
|
459
460
|
end
|
460
461
|
@context[:query_index] += 1
|
461
462
|
result
|
@@ -471,7 +472,7 @@ module Graphql
|
|
471
472
|
|
472
473
|
return if fields.blank?
|
473
474
|
|
474
|
-
fields.each_with_object({}) do |node, h|
|
475
|
+
res = fields.each_with_object({}) do |node, h|
|
475
476
|
h[node.name.underscore] = node.children.blank? ? nil : parse_fields(node.children)
|
476
477
|
end
|
477
478
|
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.9.
|
4
|
+
version: 0.9.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- poilon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-11-
|
11
|
+
date: 2022-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|
@@ -83,11 +83,8 @@ files:
|
|
83
83
|
- MIT-LICENSE
|
84
84
|
- README.md
|
85
85
|
- Rakefile
|
86
|
+
- lib/generators/auth0_authentication/auth0_authentication_generator.rb
|
86
87
|
- lib/generators/graphql_add_fields/graphql_add_fields_generator.rb
|
87
|
-
- lib/generators/graphql_all_connections/graphql_all_connections_generator.rb
|
88
|
-
- lib/generators/graphql_mutations/graphql_bulk_create_mutations_generator.rb
|
89
|
-
- lib/generators/graphql_mutations/graphql_bulk_update_mutations_generator.rb
|
90
|
-
- lib/generators/graphql_mutations/graphql_mutations_generator.rb
|
91
88
|
- lib/generators/graphql_rails_api/install_generator.rb
|
92
89
|
- lib/generators/graphql_resource/USAGE
|
93
90
|
- lib/generators/graphql_resource/graphql_resource_generator.rb
|
@@ -1,26 +0,0 @@
|
|
1
|
-
class GraphqlAllConnectionsGenerator < Rails::Generators::NamedBase
|
2
|
-
|
3
|
-
def generate
|
4
|
-
Graphql::Rails::Api::Config.query_resources.each do |resource|
|
5
|
-
dir = "app/graphql/#{resource.pluralize}"
|
6
|
-
generate_connection(dir, resource) if Dir.exist?(dir)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def generate_connection(dir, resource)
|
11
|
-
FileUtils.mkdir_p(dir) unless File.directory?(dir)
|
12
|
-
File.write(
|
13
|
-
"#{dir}/connection.rb",
|
14
|
-
<<~STRING
|
15
|
-
#{resource.pluralize.camelize}::Connection = #{resource.pluralize.camelize}::Type.define_connection do
|
16
|
-
name '#{resource.camelize}Connection'
|
17
|
-
|
18
|
-
field :total_count, types.Int do
|
19
|
-
resolve ->(obj, _, _) { obj.nodes.count }
|
20
|
-
end
|
21
|
-
end
|
22
|
-
STRING
|
23
|
-
)
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
@@ -1,30 +0,0 @@
|
|
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
|
@@ -1,30 +0,0 @@
|
|
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
|
-
end
|
@@ -1,92 +0,0 @@
|
|
1
|
-
class GraphqlMutationsGenerator < Rails::Generators::NamedBase
|
2
|
-
def generate
|
3
|
-
resource = file_name.underscore.singularize
|
4
|
-
dir = "app/graphql/#{resource.pluralize}/mutations"
|
5
|
-
FileUtils.mkdir_p(dir) unless File.directory?(dir)
|
6
|
-
generate_create_mutation(dir, resource)
|
7
|
-
generate_update_mutation(dir, resource)
|
8
|
-
generate_destroy_mutation(dir, resource)
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def generate_create_mutation(dir, resource)
|
14
|
-
File.write(
|
15
|
-
"#{dir}/create.rb",
|
16
|
-
<<~STRING
|
17
|
-
module #{resource_class(resource)}
|
18
|
-
module Mutations
|
19
|
-
class Create < GraphQL::Schema::Mutation
|
20
|
-
graphql_name "Create#{resource_class(resource)}"
|
21
|
-
description "Create a #{resource_class(resource).singularize}"
|
22
|
-
|
23
|
-
field :errors, [String], null: true
|
24
|
-
field :#{resource}, #{resource_class(resource)}::Type, null: true
|
25
|
-
|
26
|
-
argument :attributes, #{resource_class(resource)}::Mutations::InputType, required: false
|
27
|
-
|
28
|
-
def resolve(attributes:)
|
29
|
-
ApplicationService.call(:#{resource}, :create, context, attributes)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
STRING
|
35
|
-
)
|
36
|
-
end
|
37
|
-
|
38
|
-
def generate_update_mutation(dir, resource)
|
39
|
-
File.write(
|
40
|
-
"#{dir}/update.rb",
|
41
|
-
<<~STRING
|
42
|
-
module #{resource_class(resource)}
|
43
|
-
module Mutations
|
44
|
-
class Update < GraphQL::Schema::Mutation
|
45
|
-
graphql_name "Update#{resource_class(resource)}"
|
46
|
-
description "Update a #{resource_class(resource).singularize}"
|
47
|
-
|
48
|
-
field :errors, [String], null: true
|
49
|
-
field :#{resource}, #{resource_class(resource)}::Type, null: true
|
50
|
-
|
51
|
-
argument :id, String, required: true
|
52
|
-
argument :attributes, #{resource_class(resource)}::Mutations::InputType, required: false
|
53
|
-
|
54
|
-
def resolve(id:, attributes:)
|
55
|
-
ApplicationService.call(:#{resource}, :update, context, id, attributes)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
STRING
|
61
|
-
)
|
62
|
-
end
|
63
|
-
|
64
|
-
def generate_destroy_mutation(dir, resource)
|
65
|
-
File.write(
|
66
|
-
"#{dir}/destroy.rb",
|
67
|
-
<<~STRING
|
68
|
-
module #{resource_class(resource)}
|
69
|
-
module Mutations
|
70
|
-
class Destroy < GraphQL::Schema::Mutation
|
71
|
-
graphql_name "Destroy#{resource_class(resource)}"
|
72
|
-
description "Destroy a #{resource_class(resource).singularize}"
|
73
|
-
|
74
|
-
field :errors, [String], null: true
|
75
|
-
field :#{resource}, #{resource_class(resource)}::Type, null: true
|
76
|
-
|
77
|
-
argument :id, String, required: true
|
78
|
-
|
79
|
-
def resolve(id:)
|
80
|
-
ApplicationService.call(:#{resource}, :destroy, context, id)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
STRING
|
86
|
-
)
|
87
|
-
end
|
88
|
-
|
89
|
-
def resource_class(resource)
|
90
|
-
@resource_class ||= resource.pluralize.camelize
|
91
|
-
end
|
92
|
-
end
|