graphql-rails-api 0.7.3 → 0.9.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 604acd369144467291ab0c98d7139371958c3cfc921ee239f2bfafcc3472823b
4
- data.tar.gz: b097612e5e1c44c88b8ea129f2e6099da4e773b87a8d6d802556bffedfc5c0c5
3
+ metadata.gz: 9c789b39c99efe639f6a7d035d012d28b4d6cff882c4bcfb7c1145ec03ffa7d9
4
+ data.tar.gz: 27c85cd1193f34216fe52d9dde5f05e0a5ca87bec25efb24ffc9a72c0be447c0
5
5
  SHA512:
6
- metadata.gz: 37a0f2ad866b8aa8a9b51fe51ee140f68ea86d0c9172fc6c51b5fd0d75ede5c2facafa7a6b498df0c9afd0e90b5f977bfbf0cc4a6b0247f0436b0666beb8b130
7
- data.tar.gz: 314e29495ca20ec06f89b7c55ab806a22fb78f2851472c9fb483004df12bbbf885621bbf6e282f717112bdb53d9fbfc1034b5635c7ac9c255079c35400970a61
6
+ metadata.gz: 8c73593751c6aff011ae081788c1072fbeca7a8b86c0dd736c00c635f4956db56a178dfe3518a6c39a5a7c286cb939675fc67862cfa36b09617a396998e086a9
7
+ data.tar.gz: '069beded1ed21d5c0392e4daf48ceae733a6e95f8f022eb5341fd0541e89a5849a8acd3db0f8b59355c08d9ec3479f4860ddb96071fdd95acdd66bc2b8bd110a'
data/README.md CHANGED
@@ -2,14 +2,20 @@
2
2
 
3
3
  `graphql-rails-api` is a gem that provides generators to describe easily your graphql API in a domain driven design way.
4
4
 
5
- Need any help or wanna talk with me about it on slack ? Don't hesitate,
6
- https://bit.ly/2KvV8Pk
5
+ Need any help or wanna talk with me about it on discord : Poilon#5412
7
6
 
8
7
  ## Installation
9
- Add these lines to your application's Gemfile:
10
8
 
9
+ Create a rails app via
10
+ ```
11
+ rails new project-name --api --database=postgresql
12
+ cd project-name
13
+ bundle
14
+ rails db:create
15
+ ```
16
+
17
+ Add these lines to your application's Gemfile:
11
18
  ```ruby
12
- gem 'graphql'
13
19
  gem 'graphql-rails-api'
14
20
  ```
15
21
 
@@ -245,10 +245,10 @@ t.#{@id_db_type} :#{resource.underscore.singularize}_id
245
245
  file_name = "app/models/#{model.underscore.singularize}.rb"
246
246
  return if !File.exist?(file_name) || File.read(file_name).include?(line)
247
247
 
248
- line_count = `wc -l "#{file_name}"`.strip.split(' ')[0].to_i
249
-
248
+ file = open(file_name)
249
+ line_count = file.readlines.size
250
250
  line_nb = 0
251
- File.open(file_name).each do |l|
251
+ file.each do |l|
252
252
  line_nb += 1
253
253
  break if l.include?('ApplicationRecord')
254
254
  end
@@ -8,6 +8,7 @@ class GraphqlAllConnectionsGenerator < Rails::Generators::NamedBase
8
8
  end
9
9
 
10
10
  def generate_connection(dir, resource)
11
+ FileUtils.mkdir_p(dir) unless File.directory?(dir)
11
12
  File.write(
12
13
  "#{dir}/connection.rb",
13
14
  <<~STRING
@@ -3,7 +3,7 @@ class GraphqlMutationsGenerator < Rails::Generators::NamedBase
3
3
  def generate
4
4
  resource = file_name.underscore.singularize
5
5
  dir = "app/graphql/#{resource.pluralize}/mutations"
6
- system("mkdir -p #{dir}")
6
+ FileUtils.mkdir_p(dir) unless File.directory?(dir)
7
7
  generate_create_mutation(dir, resource)
8
8
  generate_update_mutation(dir, resource)
9
9
  generate_destroy_mutation(dir, resource)
@@ -8,14 +8,17 @@ module GraphqlRailsApi
8
8
 
9
9
  def generate_files
10
10
  @app_name = File.basename(Rails.root.to_s).underscore
11
- system('mkdir -p app/graphql/')
12
-
11
+
12
+ folder = 'app/graphql/'
13
+ FileUtils.mkdir_p(folder) unless File.directory?(folder)
14
+
13
15
  write_uuid_extensions_migration
14
16
 
15
17
  write_service
16
18
  write_schema
17
19
  write_query_type
18
20
  write_mutation_type
21
+ write_collection_ids_resolver
19
22
 
20
23
  write_controller
21
24
 
@@ -52,6 +55,25 @@ module GraphqlRailsApi
52
55
  )
53
56
  end
54
57
 
58
+ def write_collection_ids_resolver
59
+ File.write(
60
+ 'app/graphql/collection_ids_resolver.rb',
61
+ <<~STRING
62
+ class CollectionIdsResolver
63
+
64
+ def self.call(obj, _args, ctx)
65
+ if obj.is_a?(OpenStruct)
66
+ obj[ctx.field.name.gsub('_ids', '').pluralize]&.map(&:id)
67
+ else
68
+ obj.send(ctx.field.name)
69
+ end
70
+ end
71
+
72
+ end
73
+ STRING
74
+ )
75
+ end
76
+
55
77
  def write_application_record_methods
56
78
  lines_count = File.read('app/models/application_record.rb').lines.count
57
79
 
@@ -267,6 +289,8 @@ module GraphqlRailsApi
267
289
  type !types[!"#{resource.camelize}::Type".constantize]
268
290
  argument :page, types.Int
269
291
  argument :per_page, types.Int
292
+ argument :filter, types.String
293
+ argument :order_by, types.String
270
294
  resolve ApplicationService.call(resource, :index)
271
295
  end
272
296
 
@@ -362,6 +386,8 @@ module GraphqlRailsApi
362
386
  @context,
363
387
  order_by: params[:order_by],
364
388
  filter: params[:filter],
389
+ per_page: params[:per_page],
390
+ page: params[:page],
365
391
  user: user
366
392
  ).run.compact
367
393
  end
@@ -479,11 +505,6 @@ module GraphqlRailsApi
479
505
  )
480
506
  end
481
507
 
482
-
483
- def append(file_name)
484
-
485
- end
486
-
487
508
  def write_at(file_name, line, data)
488
509
  open(file_name, 'r+') do |f|
489
510
  while (line -= 1).positive?
@@ -31,9 +31,6 @@ class GraphqlResourceGenerator < Rails::Generators::NamedBase
31
31
  # Graphql Type
32
32
  generate_graphql_type(@resource) if options.graphql_type?
33
33
 
34
- # Graphql Connection
35
- # generate_graphql_connection(@resource) if options.connection?
36
-
37
34
  # Model
38
35
  generate_model(@resource) if options.model?
39
36
 
@@ -106,7 +103,7 @@ class GraphqlResourceGenerator < Rails::Generators::NamedBase
106
103
  end
107
104
 
108
105
  def generate_basic_mutations(resource)
109
- system("mkdir -p #{@mutations_directory}")
106
+ FileUtils.mkdir_p(@mutations_directory) unless File.directory?(@mutations_directory)
110
107
  system("rails generate graphql_mutations #{resource}")
111
108
 
112
109
  # Graphql Input Type
@@ -129,7 +126,8 @@ class GraphqlResourceGenerator < Rails::Generators::NamedBase
129
126
  end
130
127
 
131
128
  def generate_graphql_input_type(resource)
132
- system("mkdir -p #{@mutations_directory}")
129
+ FileUtils.mkdir_p(@mutations_directory) unless File.directory?(@mutations_directory)
130
+
133
131
  File.write(
134
132
  "#{@mutations_directory}/input_type.rb",
135
133
  <<~STRING
@@ -166,7 +164,7 @@ class GraphqlResourceGenerator < Rails::Generators::NamedBase
166
164
  def add_has_many_fields_to_type(field, resource)
167
165
  file_name = "app/graphql/#{field.pluralize}/type.rb"
168
166
  if File.read(file_name).include?("field :#{resource.singularize}_ids") ||
169
- File.read(file_name).include?("field :#{resource.pluralize}") ||
167
+ File.read(file_name).include?("field :#{resource.pluralize}")
170
168
  return
171
169
  end
172
170
  write_at(
@@ -244,6 +242,9 @@ class GraphqlResourceGenerator < Rails::Generators::NamedBase
244
242
  end
245
243
 
246
244
  def generate_service(resource)
245
+
246
+ FileUtils.mkdir_p("app/graphql/#{resource.pluralize}/") unless File.directory?("app/graphql/#{resource.pluralize}/")
247
+
247
248
  File.write(
248
249
  "app/graphql/#{resource.pluralize}/service.rb",
249
250
  <<~STRING
@@ -311,13 +312,13 @@ t.#{@id_db_type} :#{resource.underscore.singularize}_id
311
312
  end
312
313
 
313
314
  def add_to_model(model, line)
314
- file_name = "app/models/#{model.underscore.singularize}.rb"
315
+ file_name = File.join("app","models","#{model.underscore.singularize}.rb")
315
316
  return if !File.exist?(file_name) || File.read(file_name).include?(line)
316
317
 
317
- line_count = `wc -l "#{file_name}"`.strip.split(' ')[0].to_i
318
-
318
+ file = open(file_name)
319
+ line_count = file.readlines.size
319
320
  line_nb = 0
320
- File.open(file_name).each do |l|
321
+ file.each do |l|
321
322
  line_nb += 1
322
323
  break if l.include?('ApplicationRecord')
323
324
  end
@@ -4,7 +4,7 @@ require 'rkelly'
4
4
  module Graphql
5
5
  class HydrateQuery
6
6
 
7
- def initialize(model, context, order_by: nil, filter: nil, id: nil, user: nil)
7
+ def initialize(model, context, order_by: nil, filter: nil, id: nil, user: nil, page: nil, per_page: 10)
8
8
  @context = context
9
9
  @filter = filter
10
10
  @order_by = order_by
@@ -12,11 +12,17 @@ module Graphql
12
12
  @models = [model_name.singularize.camelize]
13
13
  @id = id
14
14
  @user = user
15
+ @page = page
16
+ @per_page = per_page
15
17
  end
16
18
 
17
19
  def run
18
20
  @model = @model.where(transform_filter(@filter)) if @filter
19
21
  @model = @model.order(@order_by) if @order_by
22
+
23
+ @model = @model.limit(@per_page) if @per_page
24
+ @model = @model.offset(@per_page * (@page - 1)) if @page
25
+
20
26
  @model = @model.where(id: @id) if @id
21
27
  plucked = DeepPluck::Model.new(@model.visible_for(user: @user), user: @user).add(
22
28
  hash_to_array_of_hashes(parse_fields(@context&.irep_node), @model)
@@ -1,7 +1,7 @@
1
1
  module Graphql
2
2
  module Rails
3
3
  module Api
4
- VERSION = '0.7.3'.freeze
4
+ VERSION = '0.9.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.7.3
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - poilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-26 00:00:00.000000000 Z
11
+ date: 2022-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -30,34 +30,34 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.1.2
33
+ version: 1.1.3
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 1.1.2
40
+ version: 1.1.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: 5.2.1
48
45
  - - "~>"
49
46
  - !ruby/object:Gem::Version
50
- version: 5.2.1
47
+ version: 7.0.0
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 6.1.4
51
51
  type: :runtime
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
54
54
  requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- version: 5.2.1
58
55
  - - "~>"
59
56
  - !ruby/object:Gem::Version
60
- version: 5.2.1
57
+ version: 7.0.0
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 6.1.4
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rkelly-remix
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  - !ruby/object:Gem::Version
115
115
  version: '0'
116
116
  requirements: []
117
- rubygems_version: 3.0.4
117
+ rubygems_version: 3.1.2
118
118
  signing_key:
119
119
  specification_version: 4
120
120
  summary: Graphql rails api framework to create easily graphql api with rails