Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ cache: bundler
before_install: gem install bundler -v 1.17
before_script: RAILS_ENV=test bundle exec rake db:create db:schema:load

env: EAGER_LOAD=true

rvm:
- 2.2.10
- 2.3.8
Expand Down
19 changes: 12 additions & 7 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
GraphqlDevise::Engine.routes.draw do
if GraphqlDevise::Types::QueryType.fields.blank?
GraphqlDevise::Types::QueryType.field(:dummy, resolver: GraphqlDevise::Resolvers::Dummy)
end
# Required as Devise forces routes to reload on eager_load
unless GraphqlDevise.schema_loaded?
if GraphqlDevise::Types::QueryType.fields.blank?
GraphqlDevise::Types::QueryType.field(:dummy, resolver: GraphqlDevise::Resolvers::Dummy)
end

if GraphqlDevise::Types::MutationType.fields.present?
GraphqlDevise::Schema.mutation(GraphqlDevise::Types::MutationType)
end
if GraphqlDevise::Types::MutationType.fields.present?
GraphqlDevise::Schema.mutation(GraphqlDevise::Types::MutationType)
end

GraphqlDevise::Schema.query(GraphqlDevise::Types::QueryType)
GraphqlDevise::Schema.query(GraphqlDevise::Types::QueryType)

GraphqlDevise.load_schema
end
end
19 changes: 19 additions & 0 deletions lib/graphql_devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ module GraphqlDevise
class Error < StandardError; end

class InvalidMountOptionsError < GraphqlDevise::Error; end

@schema_loaded = false
@mounted_resources = []

def self.schema_loaded?
@schema_loaded
end

def self.load_schema
@schema_loaded = true
end

def self.mount_resource(resource)
@mounted_resources << resource
end

def self.resource_mounted?(resource)
@mounted_resources.include?(resource)
end
end

require 'graphql_devise/concerns/controller_methods'
Expand Down
21 changes: 13 additions & 8 deletions lib/graphql_devise/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@ def mount_graphql_devise_for(resource, options = {})
]
).validate!

authenticatable_type = clean_options.authenticatable_type.presence ||
"Types::#{resource}Type".safe_constantize ||
GraphqlDevise::Types::AuthenticatableType

devise_for(
resource.pluralize.underscore.tr('/', '_').to_sym,
module: :devise,
class_name: resource,
skip: DEVISE_OPERATIONS
)

devise_scope resource.underscore.tr('/', '_').to_sym do
post clean_options.at, to: 'graphql_devise/graphql#auth'
get clean_options.at, to: 'graphql_devise/graphql#auth'
end

# Avoid routes reload done by Devise
return if GraphqlDevise.resource_mounted?(resource)

authenticatable_type = clean_options.authenticatable_type.presence ||
"Types::#{resource}Type".safe_constantize ||
GraphqlDevise::Types::AuthenticatableType

prepared_mutations = GraphqlDevise::MountMethod::OperationPreparer.new(
resource: resource,
custom: clean_options.operations,
Expand Down Expand Up @@ -66,10 +74,7 @@ def mount_graphql_devise_for(resource, options = {})

Devise.mailer.helper(GraphqlDevise::MailerHelper)

devise_scope resource.underscore.tr('/', '_').to_sym do
post clean_options.at, to: 'graphql_devise/graphql#auth'
get clean_options.at, to: 'graphql_devise/graphql#auth'
end
GraphqlDevise.mount_resource(resource)
end
end
end
2 changes: 1 addition & 1 deletion spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
config.eager_load = ENV['EAGER_LOAD'].present?

# Configure public file server for tests with Cache-Control for performance.
if Rails::VERSION::MAJOR >= 5
Expand Down