graphql_devise 0.11.3 → 0.12.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.travis.yml +7 -0
  4. data/Appraisals +14 -0
  5. data/CHANGELOG.md +48 -1
  6. data/README.md +181 -20
  7. data/app/controllers/graphql_devise/application_controller.rb +4 -1
  8. data/app/controllers/graphql_devise/concerns/set_user_by_token.rb +25 -0
  9. data/app/controllers/graphql_devise/graphql_controller.rb +2 -0
  10. data/app/helpers/graphql_devise/mailer_helper.rb +2 -2
  11. data/app/views/graphql_devise/mailer/confirmation_instructions.html.erb +1 -1
  12. data/app/views/graphql_devise/mailer/reset_password_instructions.html.erb +1 -1
  13. data/config/locales/en.yml +1 -0
  14. data/config/routes.rb +2 -0
  15. data/graphql_devise.gemspec +6 -4
  16. data/lib/generators/graphql_devise/install_generator.rb +28 -5
  17. data/lib/graphql_devise.rb +24 -10
  18. data/lib/graphql_devise/default_operations/mutations.rb +6 -6
  19. data/lib/graphql_devise/default_operations/resolvers.rb +2 -2
  20. data/lib/graphql_devise/errors/authentication_error.rb +7 -0
  21. data/lib/graphql_devise/{detailed_user_error.rb → errors/detailed_user_error.rb} +1 -1
  22. data/lib/graphql_devise/errors/error_codes.rb +6 -0
  23. data/lib/graphql_devise/errors/execution_error.rb +4 -0
  24. data/lib/graphql_devise/{user_error.rb → errors/user_error.rb} +1 -1
  25. data/lib/graphql_devise/mount_method/operation_preparer.rb +2 -2
  26. data/lib/graphql_devise/mount_method/operation_preparers/default_operation_preparer.rb +6 -2
  27. data/lib/graphql_devise/mount_method/operation_preparers/gql_name_setter.rb +1 -1
  28. data/lib/graphql_devise/mount_method/operation_preparers/mutation_field_setter.rb +3 -2
  29. data/lib/graphql_devise/mount_method/operation_preparers/resolver_type_setter.rb +1 -1
  30. data/lib/graphql_devise/mount_method/operation_preparers/resource_name_setter.rb +2 -2
  31. data/lib/graphql_devise/mutations/resend_confirmation.rb +3 -5
  32. data/lib/graphql_devise/mutations/send_password_reset.rb +5 -2
  33. data/lib/graphql_devise/mutations/sign_up.rb +3 -6
  34. data/lib/graphql_devise/rails/routes.rb +5 -72
  35. data/lib/graphql_devise/resource_loader.rb +87 -0
  36. data/lib/graphql_devise/schema_plugin.rb +106 -0
  37. data/lib/graphql_devise/version.rb +1 -1
  38. data/spec/dummy/app/controllers/api/v1/graphql_controller.rb +41 -3
  39. data/spec/dummy/app/controllers/application_controller.rb +1 -0
  40. data/spec/dummy/app/graphql/dummy_schema.rb +18 -0
  41. data/spec/dummy/app/graphql/interpreter_schema.rb +9 -0
  42. data/spec/dummy/app/graphql/types/mutation_type.rb +1 -1
  43. data/spec/dummy/app/graphql/types/query_type.rb +10 -0
  44. data/spec/dummy/config/routes.rb +3 -0
  45. data/spec/generators/graphql_devise/install_generator_spec.rb +21 -0
  46. data/spec/rails_helper.rb +4 -1
  47. data/spec/requests/graphql_controller_spec.rb +80 -0
  48. data/spec/requests/mutations/resend_confirmation_spec.rb +44 -29
  49. data/spec/requests/mutations/send_password_reset_spec.rb +40 -12
  50. data/spec/requests/queries/confirm_account_spec.rb +7 -1
  51. data/spec/requests/user_controller_spec.rb +189 -24
  52. data/spec/services/mount_method/operation_preparer_spec.rb +8 -3
  53. data/spec/services/mount_method/operation_preparers/custom_operation_preparer_spec.rb +1 -1
  54. data/spec/services/mount_method/operation_preparers/default_operation_preparer_spec.rb +15 -8
  55. data/spec/services/mount_method/operation_preparers/mutation_field_setter_spec.rb +18 -4
  56. data/spec/services/mount_method/operation_preparers/resource_name_setter_spec.rb +1 -1
  57. data/spec/services/resource_loader_spec.rb +82 -0
  58. data/spec/services/schema_plugin_spec.rb +26 -0
  59. metadata +107 -87
  60. data/lib/graphql_devise/error_codes.rb +0 -5
  61. data/spec/support/generators/file_helpers.rb +0 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd40a4f584377ba8e3cbac60b40a6cc5451ca9ed9cbaaf2d958f07e6d684b36b
4
- data.tar.gz: ff5087145891c784cce89cf304dad94adc919ffc926f7db9030c15914e97bcec
3
+ metadata.gz: 3ddce4ffb15dd4eac50fd0756808ce5fdef81a2f5fcc3405e8c00e5a7c51b689
4
+ data.tar.gz: b47284dc35df29df8a9bd2ef17cb68dde81e4a183cae904220167a37efbd6a05
5
5
  SHA512:
6
- metadata.gz: 6d0415cf1b11e0b5f80966710fbe8b15ad373dfaea322e4acbb5bea7020cdf8ae85fe2fe2a47b3a236677f1a49e1a9ad2440de1ba65135020839e85e295fa263
7
- data.tar.gz: b7d62d5a5607462f9486b5403370c3f62898b009d1519fb1ddc302d1d8d5ea2e6e601e1fb71510ab17f47c5414e091a2e0ca955782288eafad42637ade300f03
6
+ metadata.gz: 7dfb908c7284d5aac400dac4424ab7b053cd9c2f8da6b684028d19fa651eb0bd9b76ac911063676bb923c71a882930aefb65c66f2081d441b0461fc226dddf7b
7
+ data.tar.gz: 5f49d0b6b687b32de66836f9617eeb344b6c7dd77045b7a3e936f570799f9076c88f6d1abf152ba5ae54d034b5a31a0f106b0593359b742da5b7898578675633
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
@@ -24,6 +24,7 @@ gemfile:
24
24
  - gemfiles/rails5.2_graphql1.8.gemfile
25
25
  - gemfiles/rails5.2_graphql1.9.gemfile
26
26
  - gemfiles/rails5.2_graphql1.10.gemfile
27
+ - gemfiles/rails5.2_graphql1.11.gemfile
27
28
 
28
29
  jobs:
29
30
  include:
@@ -45,6 +46,12 @@ jobs:
45
46
  rvm: 2.6.6
46
47
  - gemfile: gemfiles/rails6.0_graphql1.10.gemfile
47
48
  rvm: 2.7.1
49
+ - gemfile: gemfiles/rails6.0_graphql1.11.gemfile
50
+ rvm: 2.5.8
51
+ - gemfile: gemfiles/rails6.0_graphql1.11.gemfile
52
+ rvm: 2.6.6
53
+ - gemfile: gemfiles/rails6.0_graphql1.11.gemfile
54
+ rvm: 2.7.1
48
55
  - gemfile: gemfiles/rails6.0_graphql_edge.gemfile
49
56
  rvm: 2.6.6
50
57
  env:
data/Appraisals CHANGED
@@ -61,6 +61,13 @@ appraise 'rails5.2-graphql1.10' do
61
61
  gem 'rspec-rails', '< 4.0'
62
62
  end
63
63
 
64
+ appraise 'rails5.2-graphql1.11' do
65
+ gem 'sqlite3', '~> 1.3.6'
66
+ gem 'rails', github: 'rails/rails', branch: '5-2-stable'
67
+ gem 'graphql', '~> 1.11.0'
68
+ gem 'rspec-rails', '< 4.0'
69
+ end
70
+
64
71
  appraise 'rails6.0-graphql1.8' do
65
72
  gem 'sqlite3', '~> 1.4'
66
73
  gem 'devise', '>= 4.7'
@@ -82,6 +89,13 @@ appraise 'rails6.0-graphql1.10' do
82
89
  gem 'graphql', '~> 1.10.0'
83
90
  end
84
91
 
92
+ appraise 'rails6.0-graphql1.11' do
93
+ gem 'sqlite3', '~> 1.4'
94
+ gem 'devise', '>= 4.7'
95
+ gem 'rails', github: 'rails/rails', branch: '6-0-stable'
96
+ gem 'graphql', '~> 1.11.0'
97
+ end
98
+
85
99
  appraise 'rails6.0-graphql_edge' do
86
100
  gem 'sqlite3', '~> 1.4'
87
101
  gem 'devise_token_auth', github: 'lynndylanhurley/devise_token_auth', branch: 'master'
@@ -1,6 +1,53 @@
1
1
  # Changelog
2
2
 
3
- ## [v0.11.3](https://github.com/graphql-devise/graphql_devise/tree/v0.11.3) (2020-05-22)
3
+ ## [v0.12.3](https://github.com/graphql-devise/graphql_devise/tree/v0.12.3) (2020-06-19)
4
+
5
+ [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.12.2...v0.12.3)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - Add support for graphql 1.11 [\#108](https://github.com/graphql-devise/graphql_devise/pull/108) ([mcelicalderon](https://github.com/mcelicalderon))
10
+
11
+ ## [v0.12.2](https://github.com/graphql-devise/graphql_devise/tree/v0.12.2) (2020-06-17)
12
+
13
+ [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.12.1...v0.12.2)
14
+
15
+ **Breaking changes:**
16
+
17
+ - Fix set\_resource\_by\_token no mapping error in no eager load envs [\#107](https://github.com/graphql-devise/graphql_devise/pull/107) ([mcelicalderon](https://github.com/mcelicalderon))
18
+
19
+ **Fixed bugs:**
20
+
21
+ - Separate endpoint url for mailers even if mounting the gem in your own schema [\#105](https://github.com/graphql-devise/graphql_devise/issues/105)
22
+ - Devise mapping error [\#103](https://github.com/graphql-devise/graphql_devise/issues/103)
23
+ - Use the url where the schema is mounted in emails links [\#106](https://github.com/graphql-devise/graphql_devise/pull/106) ([00dav00](https://github.com/00dav00))
24
+
25
+ ## [v0.12.1](https://github.com/graphql-devise/graphql_devise/tree/v0.12.1) (2020-06-12)
26
+
27
+ [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.12.0...v0.12.1)
28
+
29
+ **Security fixes:**
30
+
31
+ - Insecure send password reset mutation? [\#98](https://github.com/graphql-devise/graphql_devise/issues/98)
32
+ - Avoid returning user information on password reset mutation [\#100](https://github.com/graphql-devise/graphql_devise/pull/100) ([00dav00](https://github.com/00dav00))
33
+
34
+ ## [v0.12.0](https://github.com/graphql-devise/graphql_devise/tree/v0.12.0) (2020-06-12)
35
+
36
+ [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.11.4...v0.12.0)
37
+
38
+ **Implemented enhancements:**
39
+
40
+ - Mount auth operations in main GQL schema [\#96](https://github.com/graphql-devise/graphql_devise/pull/96) ([mcelicalderon](https://github.com/mcelicalderon))
41
+
42
+ ## [v0.11.4](https://github.com/graphql-devise/graphql_devise/tree/v0.11.4) (2020-05-23)
43
+
44
+ [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.11.3...v0.11.4)
45
+
46
+ **Implemented enhancements:**
47
+
48
+ - Do nothing if forgery protection enabled in ApplicationController [\#93](https://github.com/graphql-devise/graphql_devise/pull/93) ([mcelicalderon](https://github.com/mcelicalderon))
49
+
50
+ ## [v0.11.3](https://github.com/graphql-devise/graphql_devise/tree/v0.11.3) (2020-05-23)
4
51
 
5
52
  [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.11.2...v0.11.3)
6
53
 
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)
123
156
 
124
- Here are the options for the mount method:
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.
217
+
218
+ ```ruby
219
+ # Using the mount method in your config/routes.rb file
220
+ mount_graphql_devise_for('User', {})
125
221
 
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`.
222
+ # Providing options to a GraphqlDevise::ResourceLoader
223
+ GraphqlDevise::ResourceLoader.new('User', {})
224
+ ```
225
+
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,56 @@ 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).
347
+
348
+ ```ruby
349
+ # app/controllers/my_controller.rb
350
+
351
+ class MyController < ApplicationController
352
+ include GraphqlDevise::Concerns::SetUserByToken
353
+
354
+ def my_action
355
+ render json: DummySchema.execute(params[:query], context: graphql_context(:user))
356
+ end
357
+ end
358
+ ```
359
+ The `graphql_context` method receives a symbol identifying the resource you are trying
360
+ to authenticate. So if you mounted the `'User'` resource, the symbol is `:user`. You can use
361
+ this snippet to find the symbol for more complex scenarios
362
+ `resource_klass.to_s.underscore.tr('/', '_').to_sym`. `graphql_context` can also take an
363
+ array of resources if you mounted more than one into your schema. The gem will try to
364
+ authenticate a resource for each element on the array until it finds one.
365
+
366
+ Internally in your own mutations and queries a key `current_resource` will be available in
367
+ the context if a resource was successfully authenticated or `nil` otherwise.
368
+
369
+ Keep in mind that sending multiple values to the `graphql_context` method means that depending
370
+ on who makes the request, the context value `current_resource` might contain instances of the
371
+ different models you might have mounted into the schema.
372
+
373
+ Please note that by using this mechanism your GQL schema will be in control of what queries are
374
+ restricted to authenticated users and you can only do this at the root level fields of your GQL
375
+ schema. Configure the plugin as explained [here](#mounting-operations-into-your-own-schema)
376
+ so this can work.
377
+
378
+ In you main app's schema this is how you might specify if a field needs to be authenticated or not:
379
+ ```ruby
380
+ module Types
381
+ class QueryType < Types::BaseObject
382
+ # user field used the default set in the Plugin's initializer
383
+ field :user, resolver: Resolvers::UserShow
384
+ # this field will never require authentication
385
+ field :public_field, String, null: false, authenticate: false
386
+ # this field requires authentication
387
+ field :private_field, String, null: false, authenticate: true
388
+ end
389
+ end
390
+ ```
237
391
 
238
392
  ### Making Requests
239
393
  Here is a list of the available mutations and queries assuming your mounted model is `User`.
@@ -308,6 +462,14 @@ In this section the most important configurations will be highlighted.
308
462
 
309
463
  **Note:** Remember this gem adds a layer on top of Devise, so some configurations might not apply.
310
464
 
465
+ ### GraphQL Interpreter
466
+ GraphQL-Ruby `>= 1.9.0` includes a new runtime module which you may use for your schema.
467
+ Eventually, it will become the default. You can read more about it
468
+ [here](https://graphql-ruby.org/queries/interpreter).
469
+
470
+ This gem supports schemas using the interpreter and it is recommended as it introduces several
471
+ improvements which focus mainly on performance.
472
+
311
473
  ### Using Alongside Standard Devise
312
474
  The DeviseTokenAuth gem allows experimental use of the standard Devise gem to be configured at the same time, for more
313
475
  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 +480,6 @@ standard Devise templates.
318
480
  ## Future Work
319
481
  We will continue to improve the gem and add better docs.
320
482
 
321
- 1. Add mount option that will create a separate schema for the mounted resource.
322
483
  1. Make sure this gem can correctly work alongside DTA and the original Devise gem.
323
484
  1. Improve DOCS.
324
485
  1. Add support for unlockable and other Devise modules.
@@ -1,4 +1,7 @@
1
1
  module GraphqlDevise
2
- class ApplicationController < DeviseTokenAuth::ApplicationController
2
+ ApplicationController = if Rails::VERSION::MAJOR >= 5
3
+ Class.new(ActionController::API)
4
+ else
5
+ Class.new(ActionController::Base)
3
6
  end
4
7
  end
@@ -1,5 +1,30 @@
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
+ def set_resource_by_token(resource)
9
+ set_user_by_token(resource)
10
+ end
11
+
12
+ def graphql_context(resource_name)
13
+ {
14
+ resource_name: resource_name,
15
+ controller: self
16
+ }
17
+ end
18
+
19
+ def build_redirect_headers(access_token, client, redirect_header_options = {})
20
+ {
21
+ DeviseTokenAuth.headers_names[:"access-token"] => access_token,
22
+ DeviseTokenAuth.headers_names[:client] => client,
23
+ :config => params[:config],
24
+ :client_id => client,
25
+ :token => access_token
26
+ }.merge(redirect_header_options)
27
+ end
28
+ end
4
29
  end
5
30
  end