Skip to content

Commit be26c4a

Browse files
committed
Deprecate authenticating resources inside the GQL schema
1 parent 82155a8 commit be26c4a

4 files changed

Lines changed: 50 additions & 9 deletions

File tree

app/controllers/graphql_devise/concerns/set_user_by_token.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,25 @@ def set_resource_by_token(resource)
4242
set_user_by_token(resource)
4343
end
4444

45-
def graphql_context(resource_name)
46-
context = {
47-
resource_name: resource_name,
48-
controller: self
49-
}
50-
context[:current_resource] = @resource if @resource.present?
45+
def graphql_context(resource_name = nil)
46+
if !resource_name.nil?
47+
ActiveSupport::Deprecation.warn(<<-DEPRECATION.strip_heredoc, caller)
48+
Providing an argument to `graphql_context` is deprecated and will be removed in a future version
49+
of this gem.
50+
Use `set_resource_by_model` in your controller and the pass `graphql_context` as part of the GQL context.
5151
52-
context
52+
EXAMPLE
53+
include GraphqlDevise::Concerns::SetUserByToken
54+
55+
set_resource_by_model User
56+
DummySchema.execute(params[:query], context: graphql_context)
57+
DEPRECATION
58+
end
59+
60+
{ controller: self }.merge(
61+
current_resource: @resource.presence,
62+
resource_name: resource_name.presence
63+
)
5364
end
5465

5566
def build_redirect_headers(access_token, client, redirect_header_options = {})

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
return clean_options if GraphqlDevise.resource_mounted?(model) && @routing
1933

lib/graphql_devise/schema_plugin.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ 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 `set_resource_by_model` in you controller and send `graphql_context` without arguments
38+
to the GQL context instead.
39+
40+
EXAMPLE
41+
include GraphqlDevise::Concerns::SetUserByToken
42+
43+
set_resource_by_model User
44+
45+
DummySchema.execute(params[:query], context: graphql_context)
46+
DEPRECATION
47+
end
48+
3349
if auth_required && !(public_introspection && introspection_field?(field))
3450
context = set_current_resource(context)
3551
raise_on_missing_resource(context, field)

0 commit comments

Comments
 (0)