diff --git a/lib/graphql.rb b/lib/graphql.rb index 17eb3ed363..608714b4d8 100644 --- a/lib/graphql.rb +++ b/lib/graphql.rb @@ -16,6 +16,7 @@ def self.eager_load! Query.eager_load! Types.eager_load! Schema.eager_load! + @_eager_loaded = true end class Error < StandardError @@ -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: @@ -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 diff --git a/spec/graphql/autoload_spec.rb b/spec/graphql/autoload_spec.rb index cfb78aabfd..0587e0a6fd 100644 --- a/spec/graphql/autoload_spec.rb +++ b/spec/graphql/autoload_spec.rb @@ -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 @@ -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