diff --git a/.travis.yml b/.travis.yml index 78bc061a..87657838 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index fa295ed2..c957aca8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/lib/graphql_devise.rb b/lib/graphql_devise.rb index 8387df39..ca44f3d1 100644 --- a/lib/graphql_devise.rb +++ b/lib/graphql_devise.rb @@ -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' diff --git a/lib/graphql_devise/rails/routes.rb b/lib/graphql_devise/rails/routes.rb index 93515306..e4b9a1c9 100644 --- a/lib/graphql_devise/rails/routes.rb +++ b/lib/graphql_devise/rails/routes.rb @@ -25,10 +25,6 @@ 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, @@ -36,6 +32,18 @@ def mount_graphql_devise_for(resource, options = {}) 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, @@ -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 diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index c52fe04e..deedd3f9 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -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