graphql-rails-api 0.1.5 → 0.1.6

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: da727dde4584527a0597124ff476b0366f056ec29f46b51e0886a51f5c7fe00c
4
- data.tar.gz: 1ccc03fa9aaf3b81e0df7135571601df989a65b6e6b50d9940ca7329d2a69486
3
+ metadata.gz: b644695c5f738ce04ffbffc6751c67f43b5e3e5abe171e39ea98f5a526b50aaf
4
+ data.tar.gz: b59bdb2628478c676369f59a594181e1cc3a4d98d8ccdbb9ac9e4a52ebdb2f14
5
5
  SHA512:
6
- metadata.gz: d4fc5d6d2e90a155c8dc712b739dc2e2921d12121b46168a7549c2bb16108c5c77620a84c3f6fda467de239d8ced4ddf36db3f9bf8203cabc67a4989cd6248a3
7
- data.tar.gz: ca77d437d4f161d16555da1c8dabf16207d798291c883b1f33309467a7470d121f75314f0e8d37f6a4e998cbc0317118ec70d61d4eb6e751288390b1d902b3c2
6
+ metadata.gz: 7d9293440b05eb1bad81b54d1f980a1247947a19a1282a0713d34d64b440fcfb1f5fdf472ea11530abf2c0b56f94e826acd9cc5b3339bfc2543959a042c8628d
7
+ data.tar.gz: a8845eb6c68a3b49fe778144bbdcf2986a7ea5f2197b3fcb90217941dc683726508a51bc6898bfee325c0a532241cccf469d4986d8f646baa9976b2b69b0b353
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # GraphqlRailsApi
2
2
 
3
- `graphql-rails-api` is a gem that provide generators to describe easily your graphql API.
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
5
  ## Installation
6
6
  Add these lines to your application's Gemfile:
@@ -16,22 +16,69 @@ $ bundle
16
16
  $ rails generate graphql_rails_api:install
17
17
  ```
18
18
 
19
+ To disable PostgreSQL uuid extension, add the option `--no-pg-uuid`
19
20
 
20
- ## Usage
21
+ To disable ActionCable websocket subscriptions, add the option `--no-action-cable-subs`
22
+
23
+ To disable Apollo compatibility, add the option `--no-apollo-compatibility`
24
+
25
+ Automatically, `post '/graphql', to: 'graphql#execute'` will be added to your `config/route.rb`
26
+ To avoid this, you can just add the option `--no-generate-graphql-route`
27
+
28
+ # Usage
29
+
30
+ ## Resources generation
21
31
 
22
32
  ```bash
23
- $ rails generate graphql_resource account base_email:string auth_id:string
24
- $ rails generate graphql_resource user email:string first_name:string last_name:string has_many:users
25
- $ rails generate graphql_resource computer ref:string description:text belongs_to:user
26
- $ rails generate graphql_resource motherboard ref:string many_to_many:computers
33
+ $ rails generate graphql_resource resource_name field1:string field2:float belongs_to:other_resource_name has_many:other_resources_name many_to_many:other_resources_name
34
+ ```
35
+
36
+ To disable migration generation, add the option `--no-migration`
37
+
38
+ To disable model generation, add the option `--no-model`
39
+
40
+ To disable mutations generation, add the option `--no-mutations`
41
+
42
+ To disable service generation, add the option `--no-service`
43
+
44
+ To disable graphql-type generation, add the option `--no-graphql-type`
45
+
46
+ To disable graphql-input-type generation, add the option `--no-graphql-input-type`
47
+
48
+ To disable propagation (has_many creating the id in the other table, many to many creating the join table, and apply to the graphql types), add the option `--no-propagation`
27
49
 
50
+ I made the choice of migrate automatically after generating a resource, to avoid doing it each time.
51
+ You can of course disable the automatic migrate by adding the option `--no-migrate`
52
+
53
+ ## On generating resources
54
+
55
+ ```bash
56
+ $ rails generate graphql_resource computer code:string price:integer power_bench:float belongs_to:user has_many:hard_drives many_to_many:tags
28
57
  ```
29
58
 
59
+ This line will create the data migration, the model and the graphql type of the Computer resource.
60
+
61
+ It will automatically add `has_many :computers` to the User model
62
+
63
+ It will add a `computer_id` to the `HardDrive` model, and
64
+ respectively the `has_many :hard_drives` and `belongs_to :computer` to the `Computer` and `HardDrive` models.
65
+
66
+ The `many_to_many` option will make the `has_many through` association and create the join table between tag and computer.
67
+
68
+ All of these relations will be propagated to the graphql types.
69
+
70
+
71
+
72
+ ## Graphql API example
73
+
74
+ Example of a backend API: https://github.com/Poilon/graphql-rails-api-example
30
75
 
31
76
 
32
77
 
33
78
  ## Contributing
34
- Contribution directions go here.
79
+
80
+ You can post any issue, and contribute by making pull requests.
81
+ Don't hesitate, even the shortest pull request is great help. <3
35
82
 
36
83
  ## License
37
84
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -6,6 +6,7 @@ module GraphqlRailsApi
6
6
  class_option('apollo_compatibility', type: :boolean, default: true)
7
7
  class_option('action_cable_subs', type: :boolean, default: true)
8
8
  class_option('pg_uuid', type: :boolean, default: true)
9
+ class_option('generate_graphql_route', type: :boolean, default: true)
9
10
 
10
11
  def generate_files
11
12
  @app_name = File.basename(Rails.root.to_s).underscore
@@ -18,10 +19,13 @@ module GraphqlRailsApi
18
19
  write_mutation_type
19
20
  write_subscription_type
20
21
  write_controller
21
- write_channel if options.action_cable_subs?
22
+ if options.action_cable_subs?
23
+ write_websocket_connection
24
+ write_channel
25
+ end
22
26
  write_initializer
23
27
  write_require_application_rb
24
- write_route
28
+ write_route if options.generate_graphql_route?
25
29
  write_uuid_extensions_migration if options.pg_uuid?
26
30
  end
27
31
 
@@ -60,12 +64,10 @@ module GraphqlRailsApi
60
64
  end
61
65
 
62
66
  def write_require_application_rb
63
- File.write(
67
+ write_at(
64
68
  'config/application.rb',
65
- File.read('config/application.rb').gsub(
66
- "require 'rails/all'",
67
- "require 'rails/all'\nrequire 'graphql/hydrate_query'\nrequire 'graphql/visibility_hash'\n"
68
- )
69
+ 5,
70
+ "require 'graphql/hydrate_query'\nrequire 'graphql/visibility_hash'\n"
69
71
  )
70
72
  end
71
73
 
@@ -103,6 +105,26 @@ module GraphqlRailsApi
103
105
  )
104
106
  end
105
107
 
108
+ def write_websocket_connection
109
+ File.write(
110
+ 'app/channels/application_cable/connection.rb',
111
+ <<~'STRING'
112
+ module ApplicationCable
113
+ class Connection < ActionCable::Connection::Base
114
+
115
+ identified_by :current_user
116
+
117
+ def connect
118
+ # Check authentication, and define current user
119
+ self.current_user = nil
120
+ end
121
+
122
+ end
123
+ end
124
+ STRING
125
+ )
126
+ end
127
+
106
128
  def write_channel
107
129
  File.write(
108
130
  'app/channels/graphql_channel.rb',
@@ -181,7 +203,8 @@ module GraphqlRailsApi
181
203
  def authenticated_user
182
204
  # Here you need to authenticate the user.
183
205
  # You can use devise, then just write:
184
- current_user
206
+
207
+ # current_user
185
208
  end
186
209
 
187
210
  # Handle form data, JSON body, or a blank value
@@ -415,7 +438,6 @@ module GraphqlRailsApi
415
438
  )
416
439
  end
417
440
 
418
-
419
441
  def write_at(file_name, line, data)
420
442
  open(file_name, 'r+') do |f|
421
443
  while (line -= 1).positive?
@@ -7,11 +7,11 @@ class GraphqlResourceGenerator < Rails::Generators::NamedBase
7
7
  TYPES_MAPPING = {
8
8
  'id' => '!types.ID',
9
9
  'uuid' => '!types.String',
10
- 'text' => 'types.String',
11
- 'datetime' => 'types.String',
10
+ 'boolean' => 'types.Boolean',
11
+ 'float' => 'types.Float',
12
+ 'decimal' => 'types.Float',
12
13
  'integer' => 'types.Int',
13
- 'json' => 'types.String',
14
- 'jsonb' => 'types.String'
14
+ 'bigint' => 'types.Int'
15
15
  }.freeze
16
16
 
17
17
  def create_graphql_files
@@ -44,7 +44,7 @@ class GraphqlResourceGenerator < Rails::Generators::NamedBase
44
44
  private
45
45
 
46
46
  def types_mapping(type)
47
- TYPES_MAPPING[type] || "types.#{type.capitalize}"
47
+ TYPES_MAPPING[type] || "types.String"
48
48
  end
49
49
 
50
50
  def parse_args
@@ -1,7 +1,7 @@
1
1
  module Graphql
2
2
  module Rails
3
3
  module Api
4
- VERSION = '0.1.5'.freeze
4
+ VERSION = '0.1.6'.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.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - poilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-07 00:00:00.000000000 Z
11
+ date: 2018-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql