Skip to content

Commit 1c7b8ea

Browse files
Merge pull request #16 from graphql-devise/fix-separate-resource-mounts
Fix separate resource mounts
2 parents d67bc37 + 8334eb2 commit 1c7b8ea

7 files changed

Lines changed: 41 additions & 22 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
# rvm config files
2222
.ruby-version
2323
.ruby-gemset
24+
25+
.env

app/controllers/graphql_devise/graphql_controller.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
module GraphqlDevise
44
class GraphqlController < ApplicationController
5-
before_action :set_user_by_token
6-
75
def auth
86
result = if params[:_json]
97
GraphqlDevise::Schema.multiplex(
@@ -26,11 +24,7 @@ def execute_params(item)
2624
{
2725
operation_name: item[:operationName],
2826
variables: ensure_hash(item[:variables]),
29-
context: {
30-
current_resource: @resource,
31-
controller: self,
32-
resource_class: resource_class
33-
}
27+
context: { controller: self }
3428
}
3529
end
3630

app/graphql/graphql_devise/resolvers/confirm_account.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ def resolve(confirmation_token:, redirect_url:)
3030
raise_user_error(I18n.t('graphql_devise.confirmations.invalid_token'))
3131
end
3232
end
33-
34-
private
35-
36-
def resource_name
37-
resource_class.to_s.underscore.tr('/', '_')
38-
end
3933
end
4034
end
4135
end

lib/graphql_devise/concerns/controller_methods.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ def controller
3131
context[:controller]
3232
end
3333

34+
def resource_name
35+
self.class.instance_variable_get(:@resource_name)
36+
end
37+
3438
def resource_class
35-
context[:resource_class]
39+
controller.send(:resource_class, resource_name)
3640
end
3741

3842
def recoverable_enabled?
@@ -44,7 +48,7 @@ def confirmable_enabled?
4448
end
4549

4650
def current_resource
47-
context[:current_resource]
51+
@current_resource ||= controller.send(:set_user_by_token, resource_name)
4852
end
4953

5054
def client

lib/graphql_devise/rails/routes.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ class Mapper
33
def mount_graphql_devise_for(resource, opts = {})
44
custom_operations = opts[:operations] || {}
55

6-
path = opts.fetch(:at, '/')
7-
mapping_name = resource.underscore.tr('/', '_')
6+
path = opts.fetch(:at, '/graphql_auth')
7+
mapping_name = resource.underscore.tr('/', '_').to_sym
88

99
devise_for(
1010
resource.pluralize.underscore.tr('/', '_').to_sym,
@@ -34,6 +34,7 @@ def mount_graphql_devise_for(resource, opts = {})
3434

3535
new_mutation
3636
end
37+
used_mutation.instance_variable_set(:@resource_name, mapping_name)
3738

3839
GraphqlDevise::Types::MutationType.field("#{mapping_name}_#{action}", mutation: used_mutation)
3940
end
@@ -53,15 +54,16 @@ def mount_graphql_devise_for(resource, opts = {})
5354

5455
new_query
5556
end
57+
used_query.instance_variable_set(:@resource_name, mapping_name)
5658

5759
GraphqlDevise::Types::QueryType.field("#{mapping_name}_#{action}", resolver: used_query)
5860
end
5961

6062
Devise.mailer.helper(GraphqlDevise::MailerHelper)
6163

62-
devise_scope mapping_name.to_sym do
63-
post "#{path}/graphql_auth", to: 'graphql_devise/graphql#auth'
64-
get "#{path}/graphql_auth", to: 'graphql_devise/graphql#auth'
64+
devise_scope mapping_name do
65+
post path, to: 'graphql_devise/graphql#auth'
66+
get path, to: 'graphql_devise/graphql#auth'
6567
end
6668
end
6769
end

spec/dummy/config/routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Rails.application.routes.draw do
2-
mount_graphql_devise_for 'User', at: 'api/v1', operations: {
2+
mount_graphql_devise_for 'User', at: '/api/v1/graphql_auth', operations: {
33
login: Mutations::Login,
44
sign_up: Mutations::SignUp
55
}
66

77
mount_graphql_devise_for(
88
'Admin',
99
authenticable_type: Types::CustomAdminType,
10-
at: 'api/v1/admin'
10+
at: '/api/v1/admin/graphql_auth'
1111
)
1212

1313
post '/api/v1/graphql', to: 'api/v1/graphql#graphql'

spec/requests/mutations/logout_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,27 @@
3939
)
4040
end
4141
end
42+
43+
context 'when using the admin model' do
44+
let(:query) do
45+
<<-GRAPHQL
46+
mutation {
47+
adminLogout {
48+
authenticable { email }
49+
}
50+
}
51+
GRAPHQL
52+
end
53+
let(:admin) { create(:admin, :confirmed) }
54+
let(:headers) { admin.create_new_auth_token }
55+
56+
it 'logs out the admin' do
57+
expect(response).not_to include_auth_headers
58+
expect(admin.reload.tokens.keys).to be_empty
59+
expect(json_response[:data][:adminLogout]).to match(
60+
authenticable: { email: admin.email }
61+
)
62+
expect(json_response[:errors]).to be_nil
63+
end
64+
end
4265
end

0 commit comments

Comments
 (0)