graphql_devise 0.11.4 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/CHANGELOG.md +8 -0
  4. data/README.md +187 -20
  5. data/app/controllers/graphql_devise/application_controller.rb +4 -5
  6. data/app/controllers/graphql_devise/concerns/set_user_by_token.rb +23 -0
  7. data/app/controllers/graphql_devise/graphql_controller.rb +2 -0
  8. data/app/helpers/graphql_devise/mailer_helper.rb +2 -2
  9. data/config/routes.rb +2 -0
  10. data/graphql_devise.gemspec +3 -3
  11. data/lib/generators/graphql_devise/install_generator.rb +28 -5
  12. data/lib/graphql_devise.rb +16 -4
  13. data/lib/graphql_devise/mount_method/operation_preparer.rb +2 -2
  14. data/lib/graphql_devise/mount_method/operation_preparers/resource_name_setter.rb +1 -1
  15. data/lib/graphql_devise/mutations/sign_up.rb +1 -5
  16. data/lib/graphql_devise/rails/routes.rb +5 -72
  17. data/lib/graphql_devise/resource_loader.rb +87 -0
  18. data/lib/graphql_devise/schema_plugin.rb +87 -0
  19. data/lib/graphql_devise/version.rb +1 -1
  20. data/spec/dummy/app/controllers/api/v1/graphql_controller.rb +6 -2
  21. data/spec/dummy/app/graphql/dummy_schema.rb +9 -0
  22. data/spec/dummy/app/graphql/interpreter_schema.rb +9 -0
  23. data/spec/dummy/app/graphql/types/mutation_type.rb +1 -1
  24. data/spec/dummy/app/graphql/types/query_type.rb +10 -0
  25. data/spec/dummy/config/routes.rb +1 -0
  26. data/spec/generators/graphql_devise/install_generator_spec.rb +21 -0
  27. data/spec/rails_helper.rb +0 -1
  28. data/spec/requests/graphql_controller_spec.rb +80 -0
  29. data/spec/requests/user_controller_spec.rb +180 -24
  30. data/spec/services/mount_method/operation_preparer_spec.rb +2 -2
  31. data/spec/services/mount_method/operation_preparers/custom_operation_preparer_spec.rb +1 -1
  32. data/spec/services/mount_method/operation_preparers/default_operation_preparer_spec.rb +1 -1
  33. data/spec/services/mount_method/operation_preparers/resource_name_setter_spec.rb +1 -1
  34. data/spec/services/resource_loader_spec.rb +82 -0
  35. data/spec/services/schema_plugin_spec.rb +26 -0
  36. metadata +31 -5
  37. data/spec/support/generators/file_helpers.rb +0 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77e78a89405d9294b587abd9684c4448a4c08f89386d332dd73386ffe4a6f60b
4
- data.tar.gz: d9451d952032587e3ea983ffa6edd34347f3227bb4ea038d0c5da4e8c35ffaa1
3
+ metadata.gz: 6929ccf18a8a9f8e7cd9b3f4f5e8388cf2b6d19617ed163f8b1c769820c14e57
4
+ data.tar.gz: fb23c4d077deb50757905b0d28ed92764e8442b696273035d8a925c917e69fb4
5
5
  SHA512:
6
- metadata.gz: f963bf770985c5ce4f4d533efb9a15b468ba4c6827dca1ad7b64eecc9afb4cd3944ab383452cc897367d156f950d0f19f84d501e3ae6d6b34d3a528fe4d2473f
7
- data.tar.gz: 4d49b3cbb3050db885d18265394e7e6acbc5c2208c4913dcc988aae13865dbc6b6f57e3f079cbb8edb943c4de025ca2816d6b18562ce9b625972aae82cdc4c3a
6
+ metadata.gz: 7f76369100a4ad9f5e1814a759e5a358dd9a38548a105ee685baf38cd5b5a42f18c58f28f320fe2d607e1061c2cc12ed64bd82085b616179004e93720b4e3baa
7
+ data.tar.gz: cce3f1d55751b40d6089e3a0f0b77563dbecf0298004eec3a9d40df2890ebf80b28fc8e8c95aeefb46878e7c71296c1444bff8145885f1aec014ba030253b1da
data/.gitignore CHANGED
@@ -15,7 +15,8 @@ README.md.*
15
15
  /spec/dummy/tmp/
16
16
  /Gemfile.lock
17
17
  *.gemfile.lock
18
- /*.sqlite3
18
+ *.sqlite3
19
+ *.sqlite3-journal
19
20
  /spec/dummy/db/development.sqlite3
20
21
  /spec/dummy/db/test.sqlite3
21
22
  /*.gem
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [v0.12.0](https://github.com/graphql-devise/graphql_devise/tree/v0.12.0) (2020-06-11)
4
+
5
+ [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.11.4...v0.12.0)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - Mount auth operations in main GQL schema [\#96](https://github.com/graphql-devise/graphql_devise/pull/96) ([mcelicalderon](https://github.com/mcelicalderon))
10
+
3
11
  ## [v0.11.4](https://github.com/graphql-devise/graphql_devise/tree/v0.11.4) (2020-05-23)
4
12
 
5
13
  [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.11.3...v0.11.4)
data/README.md CHANGED
@@ -12,25 +12,34 @@ GraphQL interface on top of the [Devise Token Auth](https://github.com/lynndylan
12
12
  * [Table of Contents](#table-of-contents)
13
13
  * [Introduction](#introduction)
14
14
  * [Installation](#installation)
15
+ * [Running the Generator](#running-the-generator)
16
+ * [Mounting the Schema in a Separate Route](#mounting-the-schema-in-a-separate-route)
17
+ * [Mounting Operations in Your Own Schema](#mounting-operations-in-your-own-schema)
18
+ * [Important](#important)
15
19
  * [Usage](#usage)
16
- * [Mounting Routes manually](#mounting-routes-manually)
17
- * [Available Operations](#available-operations)
20
+ * [Mounting Auth Schema on a Separate Route](#mounting-auth-schema-on-a-separate-route)
21
+ * [Mounting Operations Into Your Own Schema](#mounting-operations-into-your-own-schema)
22
+ * [Available Mount Options](#available-mount-options)
23
+ * [Available Operations](#available-operations)
18
24
  * [Configuring Model](#configuring-model)
19
25
  * [Customizing Email Templates](#customizing-email-templates)
20
26
  * [I18n](#i18n)
21
27
  * [Authenticating Controller Actions](#authenticating-controller-actions)
28
+ * [Authenticate Before Reaching Your GQL Schema](#authenticate-before-reaching-your-gql-schema)
29
+ * [Authenticate in Your GQL Schema](#authenticate-in-your-gql-schema)
22
30
  * [Making Requests](#making-requests)
23
31
  * [Mutations](#mutations)
24
32
  * [Queries](#queries)
25
33
  * [More Configuration Options](#more-configuration-options)
26
34
  * [Devise Token Auth Initializer](#devise-token-auth-initializer)
27
35
  * [Devise Initializer](#devise-initializer)
36
+ * [GraphQL Interpreter](#graphql-interpreter)
28
37
  * [Using Alongside Standard Devise](#using-alongside-standard-devise)
29
38
  * [Future Work](#future-work)
30
39
  * [Contributing](#contributing)
31
40
  * [License](#license)
32
41
 
33
- <!-- Added by: mcelicalderon, at: Tue Apr 28 21:43:30 -05 2020 -->
42
+ <!-- Added by: mcelicalderon, at: Wed Jun 10 22:10:26 -05 2020 -->
34
43
 
35
44
  <!--te-->
36
45
 
@@ -54,13 +63,11 @@ gem 'graphql_devise'
54
63
  ```
55
64
 
56
65
  And then execute:
66
+ ```bash
67
+ $ bundle
68
+ ```
57
69
 
58
- $ bundle
59
-
60
- Next, you need to run the generator:
61
-
62
- $ bundle exec rails generate graphql_devise:install
63
-
70
+ ### Running the Generator
64
71
  Graphql Devise generator will execute `Devise` and `Devise Token Auth`
65
72
  generators for you. These will make the required changes for the gems to
66
73
  work correctly. All configurations for [Devise](https://github.com/plataformatec/devise) and
@@ -68,6 +75,11 @@ work correctly. All configurations for [Devise](https://github.com/plataformatec
68
75
  so you can read the docs there to customize your options.
69
76
  Configurations are done via initializer files as usual, one per gem.
70
77
 
78
+ #### Mounting the Schema in a Separate Route
79
+ ```bash
80
+ $ bundle exec rails generate graphql_devise:install
81
+ ```
82
+
71
83
  The generator accepts 2 params: `user_class` and `mount_path`. The params
72
84
  will be used to mount the route in `config/routes.rb`. For instance the executing:
73
85
 
@@ -87,14 +99,32 @@ Will do the following:
87
99
  `Admin` could be any model name you are going to be using for authentication,
88
100
  and `api/auth` could be any mount path you would like to use for auth.
89
101
 
90
- **Important:** Remember this gem mounts a completely separate GraphQL schema on a separate controller in the route
102
+ #### Mounting Operations in Your Own Schema
103
+ Now you can provide to the generator an option specifying
104
+ the name of your GQL schema. Doing this will skip the insertion of the mount method in the
105
+ routes file and will also add our `SchemaPlugin` to the specified schema. `user_class` param is still optional (`Admin`) in the following example.
106
+
107
+ ```bash
108
+ $ bundle exec rails g graphql_devise:install Admin --mount MySchema
109
+ ```
110
+
111
+ ### Important
112
+ Remember that by default this gem mounts a completely separate GraphQL schema on a separate controller in the route
91
113
  provided by the `at` option in the `mount_graphql_devise_for` method in the `config/routes.rb` file. If no `at`
92
- option is provided, the route will be `/graphql_auth`. This has no effect on your own application schema.
93
- More on this in the next section.
114
+ option is provided, the route will be `/graphql_auth`.
115
+
116
+ **Starting with `v0.12.0`** you can opt-in to load this gem's queries and mutations into your
117
+ own application's schema. You can actually mount a resource's auth schema in a separate route
118
+ and in your app's schema at the same time, but that's probably not a common scenario. More on
119
+ this in the next section.
94
120
 
95
121
  ## Usage
96
- ### Mounting Routes manually
97
- Routes can be added using the initializer or manually.
122
+ ### Mounting Auth Schema on a Separate Route
123
+ The generator can do this step for you by default. Remember now you can mount this gem's
124
+ auth operations into your own schema as described in [this section](#mounting-operations-into-your-own-schema).
125
+
126
+
127
+ Routes can be added using the generator or manually.
98
128
  You can mount this gem's GraphQL auth schema in your routes file like this:
99
129
 
100
130
  ```ruby
@@ -120,11 +150,83 @@ Rails.application.routes.draw do
120
150
  )
121
151
  end
122
152
  ```
153
+ The second argument of the `mount_graphql_devise` method is a hash of options where you can
154
+ customize how the queries and mutations are mounted into the schema. For a list of available
155
+ options go [here](#available-mount-options)
156
+
157
+ ### Mounting Operations Into Your Own Schema
158
+ Starting with `v0.12.0` you can now mount the GQL operations provided by this gem into your
159
+ app's main schema.
160
+
161
+ ```ruby
162
+ # app/graphql/dummy_schema.rb
163
+
164
+ class DummySchema < GraphQL::Schema
165
+ # It's important that this line goes before setting the query and mutation type on your
166
+ # schema in graphql versions < 1.10.0
167
+ use GraphqlDevise::SchemaPlugin.new(
168
+ query: Types::QueryType,
169
+ mutation: Types::MutationType,
170
+ resource_loaders: [
171
+ GraphqlDevise::ResourceLoader.new('User', only: [:login, :confirm_account])
172
+ ]
173
+ )
174
+
175
+ mutation(Types::MutationType)
176
+ query(Types::QueryType)
177
+ end
178
+ ```
179
+ The example above describes just one of the possible scenarios you might need.
180
+ The second argument of the `GraphqlDevise::ResourceLoader` initializer is a hash of
181
+ options where you can customize how the queries and mutations are mounted into the schema.
182
+ For a list of available options go [here](#available-mount-options).
183
+
184
+ It's important to use the plugin in your schema before assigning the mutation and query type to
185
+ it in graphql versions `< 1.10.0`. Otherwise the auth operations won't be available.
186
+
187
+ You can provide as many resource loaders as you need to the `resource_loaders` option, and each
188
+ of those will be loaded into your schema. These are the options you can initialize the
189
+ `SchemaPlugin` with:
190
+
191
+ 1. `query`: This param is mandatory unless you skip all queries via the resource loader
192
+ options. This should be the same `QueryType` you provide to the `query` method
193
+ in your schema.
194
+ 1. `mutation`: This param mandatory unless you skip all mutations via the resource loader
195
+ options. This should be the same `MutationType` you provide to the `mutation` method
196
+ in your schema.
197
+ 1. `resource_loaders`: This is an optional array of `GraphqlDevise::ResourceLoader` instances.
198
+ Here is where you specify the operations that you want to load into your app's schema.
199
+ If no loader is provided, no operations will be added to your schema, but you will still be
200
+ able to authenticate queries and mutations selectively. More on this in the controller
201
+ authentication [section](#authenticating-controller-actions).
202
+ 1. `authenticate_default`: This is a boolean value which is `true` by default. This value
203
+ defines what is the default behavior for authentication in your schema fields. `true` means
204
+ every root level field requires authentication unless specified otherwise using the
205
+ `authenticate: false` option on the field. `false` means your root level fields won't require
206
+ authentication unless specified otherwise using the `authenticate: true` option on the field.
207
+ 1. `unauthenticated_proc`: This param is optional. Here you can provide a proc that receives
208
+ one argument (field name) and is called whenever a field that requires authentication
209
+ is called without an authenticated resource. By default a `GraphQL::ExecutionError` will be
210
+ raised if authentication fails. This will provide a GQL like error message on the response.
211
+
212
+ ### Available Mount Options
213
+ Both the `mount_graphql_devise_for` method and the `GraphqlDevise::ResourceLoader` class
214
+ take the same options. So, wether you decide to mount this gem in a separate route
215
+ from your main application's schema or you use our `GraphqlDevise::SchemaPlugin` to load
216
+ this gem's auth operation into your schema, these are the options you can provide as a hash.
123
217
 
124
- Here are the options for the mount method:
218
+ ```ruby
219
+ # Using the mount method in your config/routes.rb file
220
+ mount_graphql_devise_for('User', {})
221
+
222
+ # Providing options to a GraphqlDevise::ResourceLoader
223
+ GraphqlDevise::ResourceLoader.new('User', {})
224
+ ```
125
225
 
126
- 1. `at`: Route where the GraphQL schema will be mounted on the Rails server. In this example your API will have these two routes: `POST /api/v1/graphql_auth` and `GET /api/v1/graphql_auth`.
127
- If this option is not specified, the schema will be mounted at `/graphql_auth`.
226
+ 1. `at`: Route where the GraphQL schema will be mounted on the Rails server.
227
+ In [this example](#mounting-auth-schema-on-a-separate-route) your API will have
228
+ these two routes: `POST /api/v1/graphql_auth` and `GET /api/v1/graphql_auth`.
229
+ If this option is not specified, the schema will be mounted at `/graphql_auth`. **This option only works if you are using the mount method.**
128
230
  1. `operations`: Specifying this is optional. Here you can override default
129
231
  behavior by specifying your own mutations and queries for every GraphQL operation.
130
232
  Check available operations in this file [mutations](https://github.com/graphql-devise/graphql_devise/blob/b5985036e01ea064e43e457b4f0c8516f172471c/lib/graphql_devise/rails/routes.rb#L19)
@@ -163,7 +265,7 @@ or [base resolver](https://github.com/graphql-devise/graphql_devise/blob/master/
163
265
  respectively, to take advantage of some of the methods provided by devise
164
266
  just like with `devise_scope`
165
267
 
166
- #### Available Operations
268
+ ### Available Operations
167
269
  The following is a list of the symbols you can provide to the `operations`, `skip` and `only` options of the mount method:
168
270
  ```ruby
169
271
  :login
@@ -175,7 +277,6 @@ The following is a list of the symbols you can provide to the `operations`, `ski
175
277
  :check_password_token
176
278
  ```
177
279
 
178
-
179
280
  ### Configuring Model
180
281
  Just like with Devise and DTA, you need to include a module in your authenticatable model,
181
282
  so with our example, your user model will have to look like this:
@@ -216,6 +317,9 @@ Keep in mind that if your app uses multiple locales, you should set the `I18n.lo
216
317
 
217
318
  ### Authenticating Controller Actions
218
319
  Just like with Devise or DTA, you will need to authenticate users in your controllers.
320
+ For this you have two alternatives.
321
+
322
+ #### Authenticate Before Reaching Your GQL Schema
219
323
  For this you need to call `authenticate_<model>!` in a before_action hook of your controller.
220
324
  In our example our model is `User`, so it would look like this:
221
325
  ```ruby
@@ -234,6 +338,62 @@ end
234
338
 
235
339
  The install generator can do this for you because it executes DTA installer.
236
340
  See [Installation](#Installation) for details.
341
+ If authentication fails for the request for whatever reason, execution of the request is halted
342
+ and an error is returned in a REST format as the request never reaches your GQL schema.
343
+
344
+ #### Authenticate in Your GQL Schema
345
+ For this you will need to add the `GraphqlDevise::SchemaPlugin` to your schema as described
346
+ [here](#mounting-operations-into-your-own-schema) and also set the authenticated resource
347
+ in a `before_action` hook.
348
+
349
+ ```ruby
350
+ # app/controllers/my_controller.rb
351
+
352
+ class MyController < ApplicationController
353
+ include GraphqlDevise::Concerns::SetUserByToken
354
+
355
+ before_action -> { set_resource_by_token(:user) }
356
+
357
+ def my_action
358
+ render json: DummySchema.execute(params[:query], context: graphql_context)
359
+ end
360
+ end
361
+
362
+ # @resource.to_s.underscore.tr('/', '_').to_sym
363
+ ```
364
+ The `set_resource_by_token` method receives a symbol identifying the resource you are trying
365
+ to authenticate. So if you mounted the `'User'` resource, the symbol is `:user`. You can use
366
+ this snippet to find the symbol for more complex scenarios
367
+ `resource_klass.to_s.underscore.tr('/', '_').to_sym`.
368
+
369
+ The `graphql_context` method is simply a helper method that returns a hash like this
370
+ ```ruby
371
+ { current_resource: @resource, controller: self }
372
+ ```
373
+ These are the two values the gem needs to check if a user is authenticated and to perform
374
+ other auth operations. All `set_resource_by_token` does is set the `@resource` variable if
375
+ the provided authentication headers are valid. If authentication fails, resource will be `nil`
376
+ and this is how `GraphqlDevise::SchemaPlugin` knows if a user is authenticated or not in
377
+ each query.
378
+
379
+ Please note that by using this mechanism your GQL schema will be in control of what queries are
380
+ restricted to authenticated users and you can only do this at the root level fields of your GQL
381
+ schema. Configure the plugin as explained [here](#mounting-operations-into-your-own-schema)
382
+ so this can work.
383
+
384
+ In you main app's schema this is how you might specify if a field needs to be authenticated or not:
385
+ ```ruby
386
+ module Types
387
+ class QueryType < Types::BaseObject
388
+ # user field used the default set in the Plugin's initializer
389
+ field :user, resolver: Resolvers::UserShow
390
+ # this field will never require authentication
391
+ field :public_field, String, null: false, authenticate: false
392
+ # this field requires authentication
393
+ field :private_field, String, null: false, authenticate: true
394
+ end
395
+ end
396
+ ```
237
397
 
238
398
  ### Making Requests
239
399
  Here is a list of the available mutations and queries assuming your mounted model is `User`.
@@ -308,6 +468,14 @@ In this section the most important configurations will be highlighted.
308
468
 
309
469
  **Note:** Remember this gem adds a layer on top of Devise, so some configurations might not apply.
310
470
 
471
+ ### GraphQL Interpreter
472
+ GraphQL-Ruby `>= 1.9.0` includes a new runtime module which you may use for your schema.
473
+ Eventually, it will become the default. You can read more about it
474
+ [here](https://graphql-ruby.org/queries/interpreter).
475
+
476
+ This gem supports schemas using the interpreter and it is recommended as it introduces several
477
+ improvements which focus mainly on performance.
478
+
311
479
  ### Using Alongside Standard Devise
312
480
  The DeviseTokenAuth gem allows experimental use of the standard Devise gem to be configured at the same time, for more
313
481
  information you can check [this answer here](https://github.com/lynndylanhurley/devise_token_auth/blob/2a32f18ccce15638a74e72f6cfde5cf15a808d3f/docs/faq.md#can-i-use-this-gem-alongside-standard-devise).
@@ -318,7 +486,6 @@ standard Devise templates.
318
486
  ## Future Work
319
487
  We will continue to improve the gem and add better docs.
320
488
 
321
- 1. Add mount option that will create a separate schema for the mounted resource.
322
489
  1. Make sure this gem can correctly work alongside DTA and the original Devise gem.
323
490
  1. Improve DOCS.
324
491
  1. Add support for unlockable and other Devise modules.
@@ -1,8 +1,7 @@
1
1
  module GraphqlDevise
2
- class ApplicationController < DeviseTokenAuth::ApplicationController
3
- private
4
-
5
- def verify_authenticity_token
6
- end
2
+ ApplicationController = if Rails::VERSION::MAJOR >= 5
3
+ Class.new(ActionController::API)
4
+ else
5
+ Class.new(ActionController::Base)
7
6
  end
8
7
  end
@@ -1,5 +1,28 @@
1
1
  module GraphqlDevise
2
2
  module Concerns
3
3
  SetUserByToken = DeviseTokenAuth::Concerns::SetUserByToken
4
+
5
+ SetUserByToken.module_eval do
6
+ attr_accessor :client_id, :token, :resource
7
+
8
+ alias_method :set_resource_by_token, :set_user_by_token
9
+
10
+ def graphql_context
11
+ {
12
+ current_resource: @resource,
13
+ controller: self
14
+ }
15
+ end
16
+
17
+ def build_redirect_headers(access_token, client, redirect_header_options = {})
18
+ {
19
+ DeviseTokenAuth.headers_names[:"access-token"] => access_token,
20
+ DeviseTokenAuth.headers_names[:client] => client,
21
+ :config => params[:config],
22
+ :client_id => client,
23
+ :token => access_token
24
+ }.merge(redirect_header_options)
25
+ end
26
+ end
4
27
  end
5
28
  end
@@ -2,6 +2,8 @@ require_dependency 'graphql_devise/application_controller'
2
2
 
3
3
  module GraphqlDevise
4
4
  class GraphqlController < ApplicationController
5
+ include GraphqlDevise::Concerns::SetUserByToken
6
+
5
7
  def auth
6
8
  result = if params[:_json]
7
9
  GraphqlDevise::Schema.multiplex(
@@ -1,7 +1,7 @@
1
1
  module GraphqlDevise
2
2
  module MailerHelper
3
3
  def confirmation_query(resource_name:, token:, redirect_url:)
4
- name = "#{resource_name.camelize(:lower)}ConfirmAccount"
4
+ name = "#{resource_name.underscore.tr('/', '_').camelize(:lower)}ConfirmAccount"
5
5
  raw = <<-GRAPHQL
6
6
  query($token:String!,$redirectUrl:String!){
7
7
  #{name}(confirmationToken:$token,redirectUrl:$redirectUrl){
@@ -17,7 +17,7 @@ module GraphqlDevise
17
17
  end
18
18
 
19
19
  def password_reset_query(token:, redirect_url:, resource_name:)
20
- name = "#{resource_name.camelize(:lower)}CheckPasswordToken"
20
+ name = "#{resource_name.underscore.tr('/', '_').camelize(:lower)}CheckPasswordToken"
21
21
  raw = <<-GRAPHQL
22
22
  query($token:String!,$redirectUrl:String!){
23
23
  #{name}(resetPasswordToken:$token,redirectUrl:$redirectUrl){
@@ -12,5 +12,7 @@ GraphqlDevise::Engine.routes.draw do
12
12
  GraphqlDevise::Schema.query(GraphqlDevise::Types::QueryType)
13
13
 
14
14
  GraphqlDevise.load_schema
15
+
16
+ Devise.mailer.helper(GraphqlDevise::MailerHelper)
15
17
  end
16
18
  end
@@ -25,9 +25,9 @@ Gem::Specification.new do |spec|
25
25
 
26
26
  spec.required_ruby_version = '>= 2.2.0'
27
27
 
28
- spec.add_dependency 'devise_token_auth', '>= 0.1.43'
29
- spec.add_dependency 'graphql', '>= 1.8'
30
- spec.add_dependency 'rails', '>= 4.2'
28
+ spec.add_dependency 'devise_token_auth', '>= 0.1.43', '< 2.0'
29
+ spec.add_dependency 'graphql', '>= 1.8', '< 1.11.0'
30
+ spec.add_dependency 'rails', '>= 4.2', '< 6.2'
31
31
 
32
32
  spec.add_development_dependency 'appraisal'
33
33
  spec.add_development_dependency 'coveralls'
@@ -5,6 +5,8 @@ module GraphqlDevise
5
5
  argument :user_class, type: :string, default: 'User'
6
6
  argument :mount_path, type: :string, default: 'auth'
7
7
 
8
+ class_option :mount, type: :string, default: 'separate_route'
9
+
8
10
  def execute_devise_installer
9
11
  generate 'devise:install'
10
12
  end
@@ -29,15 +31,20 @@ module GraphqlDevise
29
31
 
30
32
  def mount_resource_route
31
33
  routes_file = 'config/routes.rb'
32
- gem_route = "mount_graphql_devise_for '#{user_class}', at: '#{mount_path}'"
33
34
  dta_route = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
34
35
 
35
- if file_contains_str?(routes_file, gem_route)
36
+ if options['mount'] != 'separate_route'
36
37
  gsub_file(routes_file, /^\s+#{Regexp.escape(dta_route + "\n")}/i, '')
37
-
38
- say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}")
39
38
  else
40
- gsub_file(routes_file, /#{Regexp.escape(dta_route)}/i, gem_route)
39
+ gem_route = "mount_graphql_devise_for '#{user_class}', at: '#{mount_path}'"
40
+
41
+ if file_contains_str?(routes_file, gem_route)
42
+ gsub_file(routes_file, /^\s+#{Regexp.escape(dta_route + "\n")}/i, '')
43
+
44
+ say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}")
45
+ else
46
+ gsub_file(routes_file, /#{Regexp.escape(dta_route)}/i, gem_route)
47
+ end
41
48
  end
42
49
  end
43
50
 
@@ -65,6 +72,22 @@ module GraphqlDevise
65
72
  )
66
73
  end
67
74
 
75
+ def mount_in_schema
76
+ return if options['mount'] == 'separate_route'
77
+
78
+ inject_into_file "app/graphql/#{options['mount'].underscore}.rb", after: "< GraphQL::Schema\n" do
79
+ <<-RUBY
80
+ use GraphqlDevise::SchemaPlugin.new(
81
+ query: Types::QueryType,
82
+ mutation: Types::MutationType,
83
+ resource_loaders: [
84
+ GraphqlDevise::ResourceLoader.new('#{user_class}'),
85
+ ]
86
+ )
87
+ RUBY
88
+ end
89
+ end
90
+
68
91
  private
69
92
 
70
93
  def file_contains_str?(filename, regex_str)