Skip to content
Closed
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
7 changes: 6 additions & 1 deletion lib/graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def self.eager_load!
Query.eager_load!
Types.eager_load!
Schema.eager_load!
@_eager_loaded = true
end

class Error < StandardError
Expand Down Expand Up @@ -86,7 +87,7 @@ class << self
# If `production?` is detected but `eager_load!` wasn't called, emit a warning.
# @return [void]
def ensure_eager_load!
if production? && !eager_loading?
if production? && !eager_loaded?
warn <<~WARNING
GraphQL-Ruby thinks this is a production deployment but didn't eager-load its constants. Address this by:

Expand All @@ -111,6 +112,10 @@ def production?
(detected_env = ENV["RACK_ENV"] || ENV["RAILS_ENV"] || ENV["HANAMI_ENV"] || ENV["APP_ENV"]) && detected_env.to_s.downcase == "production"
end
end

def eager_loaded?
@_eager_loaded ||= false
end
end

self.reject_numbers_followed_by_names = false
Expand Down
10 changes: 10 additions & 0 deletions spec/graphql/autoload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def self.eager_load!

describe "warning in production" do
before do
GraphQL.remove_instance_variable(:@_eager_loaded) if GraphQL.instance_variable_defined?(:@_eager_loaded)
@prev_env = ENV.to_hash
ENV.update("HANAMI_ENV" => "production")
end
Expand Down Expand Up @@ -84,5 +85,14 @@ def self.eager_load!
ensure
GraphQL.env = prev_env
end

it "silences the warning when already eager-loaded" do
GraphQL.eager_load!
stdout, stderr = capture_io do
GraphQL.ensure_eager_load!
end
assert_equal "", stdout
assert_equal "", stderr
end
end
end
Loading