Skip to content

Commit 41d92a2

Browse files
Merge pull request #88 from graphql-devise/fix-route-reload-devise
Avoid multiple schema and type load (Devise behavior)
2 parents 8a6cb20 + 7a8c4ff commit 41d92a2

5 files changed

Lines changed: 47 additions & 16 deletions

File tree

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ cache: bundler
55
before_install: gem install bundler -v 1.17
66
before_script: RAILS_ENV=test bundle exec rake db:create db:schema:load
77

8+
env: EAGER_LOAD=true
9+
810
rvm:
911
- 2.2.10
1012
- 2.3.8

config/routes.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
GraphqlDevise::Engine.routes.draw do
2-
if GraphqlDevise::Types::QueryType.fields.blank?
3-
GraphqlDevise::Types::QueryType.field(:dummy, resolver: GraphqlDevise::Resolvers::Dummy)
4-
end
2+
# Required as Devise forces routes to reload on eager_load
3+
unless GraphqlDevise.schema_loaded?
4+
if GraphqlDevise::Types::QueryType.fields.blank?
5+
GraphqlDevise::Types::QueryType.field(:dummy, resolver: GraphqlDevise::Resolvers::Dummy)
6+
end
57

6-
if GraphqlDevise::Types::MutationType.fields.present?
7-
GraphqlDevise::Schema.mutation(GraphqlDevise::Types::MutationType)
8-
end
8+
if GraphqlDevise::Types::MutationType.fields.present?
9+
GraphqlDevise::Schema.mutation(GraphqlDevise::Types::MutationType)
10+
end
911

10-
GraphqlDevise::Schema.query(GraphqlDevise::Types::QueryType)
12+
GraphqlDevise::Schema.query(GraphqlDevise::Types::QueryType)
13+
14+
GraphqlDevise.load_schema
15+
end
1116
end

lib/graphql_devise.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ module GraphqlDevise
66
class Error < StandardError; end
77

88
class InvalidMountOptionsError < GraphqlDevise::Error; end
9+
10+
@schema_loaded = false
11+
@mounted_resources = []
12+
13+
def self.schema_loaded?
14+
@schema_loaded
15+
end
16+
17+
def self.load_schema
18+
@schema_loaded = true
19+
end
20+
21+
def self.mount_resource(resource)
22+
@mounted_resources << resource
23+
end
24+
25+
def self.resource_mounted?(resource)
26+
@mounted_resources.include?(resource)
27+
end
928
end
1029

1130
require 'graphql_devise/concerns/controller_methods'

lib/graphql_devise/rails/routes.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@ def mount_graphql_devise_for(resource, options = {})
2525
]
2626
).validate!
2727

28-
authenticatable_type = clean_options.authenticatable_type.presence ||
29-
"Types::#{resource}Type".safe_constantize ||
30-
GraphqlDevise::Types::AuthenticatableType
31-
3228
devise_for(
3329
resource.pluralize.underscore.tr('/', '_').to_sym,
3430
module: :devise,
3531
class_name: resource,
3632
skip: DEVISE_OPERATIONS
3733
)
3834

35+
devise_scope resource.underscore.tr('/', '_').to_sym do
36+
post clean_options.at, to: 'graphql_devise/graphql#auth'
37+
get clean_options.at, to: 'graphql_devise/graphql#auth'
38+
end
39+
40+
# Avoid routes reload done by Devise
41+
return if GraphqlDevise.resource_mounted?(resource)
42+
43+
authenticatable_type = clean_options.authenticatable_type.presence ||
44+
"Types::#{resource}Type".safe_constantize ||
45+
GraphqlDevise::Types::AuthenticatableType
46+
3947
prepared_mutations = GraphqlDevise::MountMethod::OperationPreparer.new(
4048
resource: resource,
4149
custom: clean_options.operations,
@@ -66,10 +74,7 @@ def mount_graphql_devise_for(resource, options = {})
6674

6775
Devise.mailer.helper(GraphqlDevise::MailerHelper)
6876

69-
devise_scope resource.underscore.tr('/', '_').to_sym do
70-
post clean_options.at, to: 'graphql_devise/graphql#auth'
71-
get clean_options.at, to: 'graphql_devise/graphql#auth'
72-
end
77+
GraphqlDevise.mount_resource(resource)
7378
end
7479
end
7580
end

spec/dummy/config/environments/test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Do not eager load code on boot. This avoids loading your whole application
1111
# just for the purpose of running a single test. If you are using a tool that
1212
# preloads Rails for running tests, you may have to set it to true.
13-
config.eager_load = false
13+
config.eager_load = ENV['EAGER_LOAD'].present?
1414

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

0 commit comments

Comments
 (0)