graphql_devise 1.1.1 → 1.2.0

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: 74d9d933b61c35ed0c51283a9455dea4cf57c9752ee80bd9622695ff07e2788f
4
- data.tar.gz: 0b3bf0a42e3ace013b4709076c731a06c4e2955eec852d9d5d2d8288c634f114
3
+ metadata.gz: 15d08b0a23b7eff91dd05234ff0ac29499cba590a6378b2d0163642750bbb549
4
+ data.tar.gz: 0fc6f85822e4def4610f69f4fe2a48e81ba98446ca9969ab3479169d2888e64f
5
5
  SHA512:
6
- metadata.gz: 168ab031a4a2028f06e6bc0177fa04ce2c5341aede479cb502f99cc0738246c6d90d13cb849d763110820b6a98840b2a6a0677a8b8ea7596ff58d3e545d87b1e
7
- data.tar.gz: c1051fbfe8b466c5818232afafff64d17fab8880e4c9395f84e86280c0355928b59ef2c31331ae6bc78a92f0cda5bdb8dafba03eae1f50483b8fa07d4848c39f
6
+ metadata.gz: 62163b8e19fcd257f1740840e87f71f739ba50d498ad5403ea57da143b43230afcb75c6a708d1b326a32d62e5629bef4bb74674674edb8c7359c8bf27950fb09
7
+ data.tar.gz: 91c43a208b02129ecaaeaf6fc5317aeda1d8a8366ccf618130bcb883eafee540f2bb6c0208c6890b5ed7e7174bc35d0de038e0318d8971aabb736503d9d7ff7d
data/.circleci/config.yml CHANGED
@@ -13,22 +13,14 @@ jobs:
13
13
  - image: 'ruby:<< parameters.ruby-version >>'
14
14
  environment:
15
15
  BUNDLE_GEMFILE: << parameters.gemfile >>
16
- BUNDLE_PATH: ../vendor/bundle
17
16
  COVERALLS_PARALLEL: true
18
17
  EAGER_LOAD: 'true'
19
18
  steps:
20
19
  - checkout
21
- - restore_cache:
22
- keys:
23
- - v2.0-<< parameters.gemfile >>-<< parameters.ruby-version >>
24
20
  - run: gem install bundler -v '1.17'
25
21
  - run:
26
22
  name: Install dependencies
27
23
  command: bundle install
28
- - save_cache:
29
- key: v2.0-<< parameters.gemfile >>-<< parameters.ruby-version >>
30
- paths:
31
- - vendor/bundle
32
24
  - run:
33
25
  name: Run Specs
34
26
  command:
data/.rubocop.yml CHANGED
@@ -4,7 +4,7 @@ require:
4
4
  - rubocop-rails
5
5
 
6
6
  AllCops:
7
- TargetRubyVersion: 2.3
7
+ TargetRubyVersion: 2.4.4
8
8
  DisplayCopNames: true
9
9
  Exclude:
10
10
  - bin/**/*
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [v1.2.0](https://github.com/graphql-devise/graphql_devise/tree/v1.2.0) (2022-11-14)
4
+
5
+ [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v1.1.1...v1.2.0)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - Set base controller from route mount [\#237](https://github.com/graphql-devise/graphql_devise/pull/237) ([00dav00](https://github.com/00dav00))
10
+
3
11
  ## [v1.1.1](https://github.com/graphql-devise/graphql_devise/tree/v1.1.1) (2022-10-20)
4
12
 
5
13
  [Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v1.1.0...v1.1.1)
data/README.md CHANGED
@@ -157,6 +157,8 @@ Rails.application.routes.draw do
157
157
  login: Mutations::Login
158
158
  },
159
159
  skip: [:register],
160
+ # optional, use only if you need a specific base controller to mount the new actions
161
+ base_controller: ApiController,
160
162
  additional_mutations: {
161
163
  # generates mutation { adminUserSignUp }
162
164
  admin_user_sign_up: Mutations::AdminUserSignUp
@@ -264,6 +266,11 @@ our default classes and yielding your customized code after calling `super`, exa
264
266
  and an `authenticatable` type to every query. Gem will try to use `Types::<model>Type` by
265
267
  default, so in our example you could define `Types::UserType` and every query and mutation
266
268
  will use it. But, you can override this type with this option like in the example.
269
+ 1. `base_controller`: Specifying this is optional. By default the controller used to mount the route is
270
+ `GraphqlDevise::ApplicationController` which inherits from `ActionController::API` or `ActionController::Base`
271
+ depending on the rails version of the main project. This option allows you to set the controller used as the parent of
272
+ the controller where the route will be mounted. This config is similar to `Devise`'s `base_controller` config but in
273
+ this case each route can have a different parent controller. **This option only works if you are using the mount method.**
267
274
  1. `skip`: An array of the operations that should not be available in the authentication schema. All these operations are
268
275
  symbols and should belong to the list of available operations in the gem.
269
276
  1. `only`: An array of the operations that should be available in the authentication schema. The `skip` and `only` options are
@@ -5,48 +5,6 @@ require_dependency 'graphql_devise/application_controller'
5
5
  module GraphqlDevise
6
6
  class GraphqlController < ApplicationController
7
7
  include SetUserByToken
8
-
9
- def auth
10
- result = if params[:_json]
11
- Schema.multiplex(
12
- params[:_json].map do |param|
13
- { query: param[:query] }.merge(execute_params(param))
14
- end
15
- )
16
- else
17
- Schema.execute(params[:query], **execute_params(params))
18
- end
19
-
20
- render json: result unless performed?
21
- end
22
-
23
- attr_accessor :client_id, :token, :resource
24
-
25
- private
26
-
27
- def execute_params(item)
28
- {
29
- operation_name: item[:operationName],
30
- variables: ensure_hash(item[:variables]),
31
- context: { controller: self }
32
- }
33
- end
34
-
35
- def ensure_hash(ambiguous_param)
36
- case ambiguous_param
37
- when String
38
- if ambiguous_param.present?
39
- ensure_hash(JSON.parse(ambiguous_param))
40
- else
41
- {}
42
- end
43
- when Hash, ActionController::Parameters
44
- ambiguous_param
45
- when nil
46
- {}
47
- else
48
- raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
49
- end
50
- end
8
+ include AuthControllerMethods
51
9
  end
52
10
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlDevise
4
+ module AuthControllerMethods
5
+ extend ActiveSupport::Concern
6
+
7
+ def auth
8
+ result = if params[:_json]
9
+ Schema.multiplex(
10
+ params[:_json].map do |param|
11
+ { query: param[:query] }.merge(execute_params(param))
12
+ end
13
+ )
14
+ else
15
+ Schema.execute(params[:query], **execute_params(params))
16
+ end
17
+
18
+ render json: result unless performed?
19
+ end
20
+
21
+ attr_accessor :client_id, :token, :resource
22
+
23
+ private
24
+
25
+ def execute_params(item)
26
+ {
27
+ operation_name: item[:operationName],
28
+ variables: ensure_hash(item[:variables]),
29
+ context: { controller: self }
30
+ }
31
+ end
32
+
33
+ def ensure_hash(ambiguous_param)
34
+ case ambiguous_param
35
+ when String
36
+ if ambiguous_param.present?
37
+ ensure_hash(JSON.parse(ambiguous_param))
38
+ else
39
+ {}
40
+ end
41
+ when Hash, ActionController::Parameters
42
+ ambiguous_param
43
+ when nil
44
+ {}
45
+ else
46
+ raise ArgumentError, "Unexpected parameter: #{ambiguous_param}"
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,13 +1,23 @@
1
1
  module GraphqlDevise
2
2
  module RouteMounter
3
3
  def mount_graphql_devise_for(resource, options = {})
4
+ routing = 'graphql_devise/graphql#auth'
5
+
6
+ if (base_controller = options.delete(:base_controller))
7
+ new_controller = GraphqlDevise.const_set("#{resource}AuthController", Class.new(base_controller))
8
+ new_controller.include(SetUserByToken)
9
+ new_controller.include(AuthControllerMethods)
10
+
11
+ routing = "#{new_controller.to_s.underscore.gsub('_controller','')}#auth"
12
+ end
13
+
4
14
  clean_options = ResourceLoader.new(resource, options, true).call(
5
15
  Types::QueryType,
6
16
  Types::MutationType
7
17
  )
8
18
 
9
- post clean_options.at, to: 'graphql_devise/graphql#auth'
10
- get clean_options.at, to: 'graphql_devise/graphql#auth'
19
+ post clean_options.at, to: routing
20
+ get clean_options.at, to: routing
11
21
  end
12
22
  end
13
23
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GraphqlDevise
4
- VERSION = '1.1.1'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CookiesController < ApplicationController
4
+ include ActionController::Cookies
5
+ protect_from_forgery with: :null_session
6
+ end
7
+
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Rails.application.routes.draw do
4
- mount_graphql_devise_for User, at: '/api/v1/graphql_auth', operations: {
5
- login: Mutations::Login,
6
- register: Mutations::Register
7
- }, additional_mutations: {
8
- register_confirmed_user: Mutations::RegisterConfirmedUser
9
- }, additional_queries: {
10
- public_user: Resolvers::PublicUser
11
- }
4
+ mount_graphql_devise_for(
5
+ User,
6
+ at: '/api/v1/graphql_auth',
7
+ base_controller: CookiesController,
8
+ operations: { login: Mutations::Login, register: Mutations::Register },
9
+ additional_mutations: { register_confirmed_user: Mutations::RegisterConfirmedUser },
10
+ additional_queries: { public_user: Resolvers::PublicUser }
11
+ )
12
12
 
13
13
  mount_graphql_devise_for(
14
14
  Admin,
@@ -185,23 +185,22 @@ RSpec.describe 'Login Requests' do
185
185
  end
186
186
  end
187
187
 
188
-
189
188
  if DeviseTokenAuth.respond_to?(:cookie_enabled)
190
189
  context 'when using cookies for auth' do
191
190
  let!(:user) { create(:user, :confirmed, password: password, email: 'vvega@wallaceinc.com') }
192
191
  let(:email) { user.email }
193
192
  let(:query) do
194
193
  <<-GRAPHQL
195
- mutation {
196
- userLogin(
197
- email: "#{email}",
198
- password: "#{password}"
199
- ) {
200
- authenticatable { email }
201
- credentials { accessToken uid tokenType client expiry }
194
+ mutation {
195
+ userLogin(
196
+ email: "#{email}",
197
+ password: "#{password}"
198
+ ) {
199
+ authenticatable { email }
200
+ credentials { accessToken uid tokenType client expiry }
201
+ }
202
202
  }
203
- }
204
- GRAPHQL
203
+ GRAPHQL
205
204
  end
206
205
 
207
206
  around do |example|
@@ -214,7 +213,7 @@ RSpec.describe 'Login Requests' do
214
213
 
215
214
  it 'honors DTA configuration of setting auth info in cookies' do
216
215
  cookie = cookies.get_cookie('auth_cookie')
217
- expect(JSON.parse(cookie.value).keys).to include(*%w[uid access-token client])
216
+ expect(JSON.parse(cookie.value).keys).to include('uid', 'access-token', 'client')
218
217
  end
219
218
  end
220
219
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql_devise
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Celi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-10-20 00:00:00.000000000 Z
12
+ date: 2022-11-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: devise_token_auth
@@ -333,6 +333,7 @@ files:
333
333
  - lib/graphql_devise.rb
334
334
  - lib/graphql_devise/concerns/additional_controller_methods.rb
335
335
  - lib/graphql_devise/concerns/additional_model_methods.rb
336
+ - lib/graphql_devise/concerns/auth_controller_methods.rb
336
337
  - lib/graphql_devise/concerns/authenticatable.rb
337
338
  - lib/graphql_devise/concerns/controller_methods.rb
338
339
  - lib/graphql_devise/concerns/field_authentication.rb
@@ -389,6 +390,7 @@ files:
389
390
  - spec/dummy/app/assets/config/manifest.js
390
391
  - spec/dummy/app/controllers/api/v1/graphql_controller.rb
391
392
  - spec/dummy/app/controllers/application_controller.rb
393
+ - spec/dummy/app/controllers/cookies_controller.rb
392
394
  - spec/dummy/app/graphql/dummy_schema.rb
393
395
  - spec/dummy/app/graphql/interpreter_schema.rb
394
396
  - spec/dummy/app/graphql/mutations/base_mutation.rb
@@ -526,7 +528,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
526
528
  - !ruby/object:Gem::Version
527
529
  version: '0'
528
530
  requirements: []
529
- rubygems_version: 3.3.24
531
+ rubygems_version: 3.3.26
530
532
  signing_key:
531
533
  specification_version: 4
532
534
  summary: GraphQL queries and mutations on top of devise_token_auth
@@ -536,6 +538,7 @@ test_files:
536
538
  - spec/dummy/app/assets/config/manifest.js
537
539
  - spec/dummy/app/controllers/api/v1/graphql_controller.rb
538
540
  - spec/dummy/app/controllers/application_controller.rb
541
+ - spec/dummy/app/controllers/cookies_controller.rb
539
542
  - spec/dummy/app/graphql/dummy_schema.rb
540
543
  - spec/dummy/app/graphql/interpreter_schema.rb
541
544
  - spec/dummy/app/graphql/mutations/base_mutation.rb