Skip to content

Commit 7c8f716

Browse files
Merge pull request #176 from graphql-devise/deprecate-schema-level-authentication
Deprecate authenticating resources inside the GQL schema
2 parents 3d4972c + f071ace commit 7c8f716

6 files changed

Lines changed: 43 additions & 4 deletions

File tree

app/controllers/graphql_devise/concerns/additional_controller_methods.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ def set_resource_by_token(resource)
4141
end
4242

4343
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(models)` 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+
4455
{
4556
resource_name: resource_name,
4657
controller: self

lib/generators/graphql_devise/install_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def mount_in_schema
8383
query: Types::QueryType,
8484
mutation: Types::MutationType,
8585
resource_loaders: [
86-
GraphqlDevise::ResourceLoader.new('#{user_class}'),
86+
GraphqlDevise::ResourceLoader.new(#{user_class})
8787
]
8888
)
8989
RUBY

lib/graphql_devise.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def self.add_mapping(mapping_name, resource)
3333

3434
Devise.add_mapping(
3535
mapping_name.to_s.pluralize.to_sym,
36-
module: :devise, class_name: resource
36+
module: :devise, class_name: resource.to_s
3737
)
3838
end
3939

lib/graphql_devise/resource_loader.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ 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 = @resource.is_a?(String) ? @resource.constantize : @resource
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
30+
end
1731

1832
# Necesary when mounting a resource via route file as Devise forces the reloading of routes
1933
return clean_options if GraphqlDevise.resource_mounted?(model) && @routing

lib/graphql_devise/schema_plugin.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ 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+
3347
if auth_required && !(public_introspection && introspection_field?(field))
3448
context = set_current_resource(context)
3549
raise_on_missing_resource(context, field)

spec/generators/graphql_devise/install_generator_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
assert_file 'app/controllers/application_controller.rb', /^\s{2}include GraphqlDevise::Concerns::SetUserByToken/
3535

36-
assert_file 'app/graphql/gqld_dummy_schema.rb', /\s+#{Regexp.escape("GraphqlDevise::ResourceLoader.new('Admin')")}/
36+
assert_file 'app/graphql/gqld_dummy_schema.rb', /\s+#{Regexp.escape("GraphqlDevise::ResourceLoader.new(Admin)")}/
3737
end
3838
end
3939

0 commit comments

Comments
 (0)