Skip to content

Commit 7e81386

Browse files
Merge pull request #199 from graphql-devise/remove-deprecated-authentication-methods
Remove deprecated authentication methods
2 parents 10c9944 + 009ff66 commit 7e81386

9 files changed

Lines changed: 15 additions & 120 deletions

File tree

app/controllers/graphql_devise/concerns/additional_controller_methods.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,6 @@ def set_resource_by_token(resource)
4040
set_user_by_token(resource)
4141
end
4242

43-
def graphql_context(resource_name)
44-
ActiveSupport::Deprecation.warn(<<-DEPRECATION.strip_heredoc, caller)
45-
`graphql_context` is deprecated and will be removed in a future version of this gem.
46-
Use `gql_devise_context(model)` instead.
47-
48-
EXAMPLE
49-
include GraphqlDevise::Concerns::SetUserByToken
50-
51-
DummySchema.execute(params[:query], context: gql_devise_context(User))
52-
DummySchema.execute(params[:query], context: gql_devise_context(User, Admin))
53-
DEPRECATION
54-
55-
{
56-
resource_name: resource_name,
57-
controller: self
58-
}
59-
end
60-
6143
def build_redirect_headers(access_token, client, redirect_header_options = {})
6244
{
6345
DeviseTokenAuth.headers_names[:"access-token"] => access_token,

app/models/graphql_devise/concerns/model.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'graphql_devise/model/with_email_updater'
4-
53
module GraphqlDevise
64
module Concerns
75
module Model

lib/graphql_devise/resource_loader.rb

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,23 @@ def call(query, mutation)
1313
# clean_options responds to all keys defined in GraphqlDevise::MountMethod::SUPPORTED_OPTIONS
1414
clean_options = GraphqlDevise::MountMethod::OptionSanitizer.new(@options).call!
1515

16-
model = if @resource.is_a?(String)
17-
ActiveSupport::Deprecation.warn(<<-DEPRECATION.strip_heredoc, caller)
18-
Providing a String as the model you want to mount is deprecated and will be removed in a future version of
19-
this gem. Please use the actual model constant instead.
20-
21-
EXAMPLE
22-
23-
GraphqlDevise::ResourceLoader.new(User) # instead of GraphqlDevise::ResourceLoader.new('User')
24-
25-
mount_graphql_devise_for User # instead of mount_graphql_devise_for 'User'
26-
DEPRECATION
27-
@resource.constantize
28-
else
29-
@resource
16+
unless @resource.is_a?(Class)
17+
raise(
18+
GraphqlDevise::InvalidMountOptionsError,
19+
'A class must be provided when mounting a model. String values are no longer supported.'
20+
)
3021
end
3122

3223
# Necesary when mounting a resource via route file as Devise forces the reloading of routes
33-
return clean_options if GraphqlDevise.resource_mounted?(model) && @routing
24+
return clean_options if GraphqlDevise.resource_mounted?(@resource) && @routing
3425

3526
validate_options!(clean_options)
3627

3728
authenticatable_type = clean_options.authenticatable_type.presence ||
3829
"Types::#{@resource}Type".safe_constantize ||
3930
GraphqlDevise::Types::AuthenticatableType
4031

41-
prepared_mutations = prepare_mutations(model, clean_options, authenticatable_type)
32+
prepared_mutations = prepare_mutations(@resource, clean_options, authenticatable_type)
4233

4334
if prepared_mutations.any? && mutation.blank?
4435
raise GraphqlDevise::Error, 'You need to provide a mutation type unless all mutations are skipped'
@@ -48,7 +39,7 @@ def call(query, mutation)
4839
mutation.field(action, mutation: prepared_mutation, authenticate: false)
4940
end
5041

51-
prepared_resolvers = prepare_resolvers(model, clean_options, authenticatable_type)
42+
prepared_resolvers = prepare_resolvers(@resource, clean_options, authenticatable_type)
5243

5344
if prepared_resolvers.any? && query.blank?
5445
raise GraphqlDevise::Error, 'You need to provide a query type unless all queries are skipped'
@@ -59,7 +50,7 @@ def call(query, mutation)
5950
end
6051

6152
GraphqlDevise.add_mapping(GraphqlDevise.to_mapping_name(@resource).to_sym, @resource)
62-
GraphqlDevise.mount_resource(model) if @routing
53+
GraphqlDevise.mount_resource(@resource) if @routing
6354

6455
clean_options
6556
end

lib/graphql_devise/schema_plugin.rb

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,7 @@ def trace(event, trace_data)
3030
auth_required = authenticate_option(field, trace_data)
3131
context = context_from_data(trace_data)
3232

33-
if context.key?(:resource_name)
34-
ActiveSupport::Deprecation.warn(<<-DEPRECATION.strip_heredoc, caller)
35-
Providing `resource_name` as part of the GQL context, or doing so by using the `graphql_context(resource_name)`
36-
method on your controller is deprecated and will be removed in a future version of this gem.
37-
Please use `gql_devise_context` in you controller instead.
38-
39-
EXAMPLE
40-
include GraphqlDevise::Concerns::SetUserByToken
41-
42-
DummySchema.execute(params[:query], context: gql_devise_context(User))
43-
DummySchema.execute(params[:query], context: gql_devise_context(User, Admin))
44-
DEPRECATION
45-
end
46-
4733
if auth_required && !(public_introspection && introspection_field?(field))
48-
context = set_current_resource(context)
4934
raise_on_missing_resource(context, field, auth_required)
5035
end
5136

@@ -56,25 +41,6 @@ def trace(event, trace_data)
5641

5742
attr_reader :public_introspection
5843

59-
def set_current_resource(context)
60-
controller = context[:controller]
61-
resource_names = Array(context[:resource_name])
62-
63-
context[:current_resource] ||= resource_names.find do |resource_name|
64-
unless Devise.mappings.key?(resource_name)
65-
raise(
66-
GraphqlDevise::Error,
67-
"Invalid resource_name `#{resource_name}` provided to `graphql_context`. Possible values are: #{Devise.mappings.keys}."
68-
)
69-
end
70-
71-
found = controller.set_resource_by_token(resource_name)
72-
break found if found
73-
end
74-
75-
context
76-
end
77-
7844
def raise_on_missing_resource(context, field, auth_required)
7945
@unauthenticated_proc.call(field.name) if context[:current_resource].blank?
8046

spec/dummy/app/controllers/api/v1/graphql_controller.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,13 @@ def interpreter
1515
render json: InterpreterSchema.execute(params[:query], **execute_params(params))
1616
end
1717

18-
def failing_resource_name
19-
render json: DummySchema.execute(params[:query], context: graphql_context([:user, :fail]))
20-
end
21-
22-
def controller_auth
23-
result = DummySchema.execute(
24-
params[:query],
25-
operation_name: params[:operationName],
26-
variables: ensure_hash(params[:variables]),
27-
context: gql_devise_context(SchemaUser, User)
28-
)
29-
30-
render json: result unless performed?
31-
end
32-
3318
private
3419

3520
def execute_params(item)
3621
{
3722
operation_name: item[:operationName],
3823
variables: ensure_hash(item[:variables]),
39-
context: graphql_context([:user, :schema_user])
24+
context: gql_devise_context(SchemaUser, User)
4025
}
4126
end
4227

spec/dummy/app/graphql/dummy_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class DummySchema < GraphQL::Schema
1010
User,
1111
only: [
1212
:login,
13-
:resend_confirmation_with_token,
13+
:resend_confirmation_with_token
1414
]
1515
),
1616
GraphqlDevise::ResourceLoader.new(Guest, only: [:logout]),

spec/dummy/config/routes.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
Rails.application.routes.draw do
4-
mount_graphql_devise_for 'User', at: '/api/v1/graphql_auth', operations: {
4+
mount_graphql_devise_for User, at: '/api/v1/graphql_auth', operations: {
55
login: Mutations::Login,
66
register: Mutations::Register
77
}, additional_mutations: {
@@ -21,20 +21,18 @@
2121
)
2222

2323
mount_graphql_devise_for(
24-
'Guest',
24+
Guest,
2525
only: [:login, :logout, :register],
2626
at: '/api/v1/guest/graphql_auth'
2727
)
2828

2929
mount_graphql_devise_for(
30-
'Users::Customer',
30+
Users::Customer,
3131
only: [:login],
3232
at: '/api/v1/user_customer/graphql_auth'
3333
)
3434

3535
get '/api/v1/graphql', to: 'api/v1/graphql#graphql'
3636
post '/api/v1/graphql', to: 'api/v1/graphql#graphql'
3737
post '/api/v1/interpreter', to: 'api/v1/graphql#interpreter'
38-
post '/api/v1/failing', to: 'api/v1/graphql#failing_resource_name'
39-
post '/api/v1/controller_auth', to: 'api/v1/graphql#controller_auth'
4038
end

spec/requests/user_controller_spec.rb

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,6 @@
5050
GRAPHQL
5151
end
5252

53-
context 'when authenticating before using the GQL schema' do
54-
before { post_request('/api/v1/controller_auth') }
55-
56-
context 'when user is authenticated' do
57-
let(:headers) { create(:schema_user).create_new_auth_token }
58-
59-
it 'allows authentication at the controller level' do
60-
expect(json_response[:data][:privateField]).to eq('Field will always require authentication')
61-
end
62-
end
63-
64-
context 'when user is not authenticated' do
65-
it_behaves_like 'returns a must authenticate error', 'privateField'
66-
end
67-
end
68-
6953
context 'when using a regular schema' do
7054
before { post_request('/api/v1/graphql') }
7155

@@ -88,15 +72,6 @@
8872
context 'when user is not authenticated' do
8973
it_behaves_like 'returns a must authenticate error', 'privateField'
9074
end
91-
92-
context 'when using the failing route' do
93-
it 'raises an invalid resource_name error' do
94-
expect { post_request('/api/v1/failing') }.to raise_error(
95-
GraphqlDevise::Error,
96-
'Invalid resource_name `fail` provided to `graphql_context`. Possible values are: [:user, :admin, :guest, :users_customer, :schema_user].'
97-
)
98-
end
99-
end
10075
end
10176

10277
context 'when using an interpreter schema' do

spec/services/resource_loader_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
let(:mutation) { class_double(GraphQL::Schema::Object) }
1111
let(:routing) { false }
1212
let(:mounted) { false }
13-
let(:resource) { 'User' }
13+
let(:resource) { User }
1414
let(:options) { { only: [:login], additional_queries: { public_user: Class.new(GraphQL::Schema::Resolver) } } }
1515

1616
before do

0 commit comments

Comments
 (0)