Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/controllers/graphql_devise/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

module GraphqlDevise
ApplicationController = if Rails::VERSION::MAJOR >= 5
Class.new(ActionController::API)
else
Class.new(ActionController::Base)
end
ApplicationController = Class.new(GraphqlDevise.base_controller)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this fail in production with eager load? I think this would happen before the mount method gets executed during app start.

# ApplicationController = if Rails::VERSION::MAJOR >= 5
# Class.new(ActionController::API)
# else
# Class.new(ActionController::Base)
# end
end
14 changes: 14 additions & 0 deletions lib/graphql_devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class InvalidMountOptionsError < ::GraphqlDevise::Error; end

@schema_loaded = false
@mounted_resources = []
@base_controller = if Rails::VERSION::MAJOR >= 5
ActionController::API
else
ActionController::Base
end

def self.schema_loaded?
@schema_loaded
Expand All @@ -46,6 +51,15 @@ def self.mount_resource(model)
@mounted_resources << model
end

def self.setup_base_controller(controller)
return unless controller
@base_controller = controller
end

def self.base_controller
@base_controller
end

def self.add_mapping(mapping_name, resource)
return if Devise.mappings.key?(mapping_name.to_sym)

Expand Down
1 change: 1 addition & 0 deletions lib/graphql_devise/route_mounter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module GraphqlDevise
module RouteMounter
def mount_graphql_devise_for(resource, options = {})
GraphqlDevise.setup_base_controller(options.delete(:base_controller))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we build a new controller class every time the method is called? Maybe something like:

# do not execute if base_controller option is not present
new_controller = GraphqlDevise.const_set("#{resource}AuthController", Class.new(options.delete(:base_controller)))

new_controller.include(SetUserByToken)
new_controller.include(AuthActionMethod) # module that defines the controller action

post clean_options.at, to: "#{new_controller.to_s.underscore}#auth"
get clean_options.at, to: "#{new_controller.to_s.underscore}#auth"

Not sure that works, but it should?

clean_options = ResourceLoader.new(resource, options, true).call(
Types::QueryType,
Types::MutationType
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

Rails.application.routes.draw do
mount_graphql_devise_for User, at: '/api/v1/graphql_auth', operations: {
mount_graphql_devise_for User, at: '/api/v1/graphql_auth', base_controller: ApplicationController, operations: {
login: Mutations::Login,
register: Mutations::Register
}, additional_mutations: {
Expand Down