Skip to content

Commit dee9294

Browse files
authored
Merge pull request #5409 from rmosolgo/switch-on-development-not-production
Visibility: Default to preload: true when env.staging?
2 parents ccf2cb7 + b74bf4a commit dee9294

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/graphql/schema/visibility.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class Schema
1010
class Visibility
1111
# @param schema [Class<GraphQL::Schema>]
1212
# @param profiles [Hash<Symbol => Hash>] A hash of `name => context` pairs for preloading visibility profiles
13-
# @param preload [Boolean] if `true`, load the default schema profile and all named profiles immediately (defaults to `true` for `Rails.env.production?`)
13+
# @param preload [Boolean] if `true`, load the default schema profile and all named profiles immediately (defaults to `true` for `Rails.env.production?` and `Rails.env.staging?`)
1414
# @param migration_errors [Boolean] if `true`, raise an error when `Visibility` and `Warden` return different results
15-
def self.use(schema, dynamic: false, profiles: EmptyObjects::EMPTY_HASH, preload: (defined?(Rails.env) ? Rails.env.production? : nil), migration_errors: false)
15+
def self.use(schema, dynamic: false, profiles: EmptyObjects::EMPTY_HASH, preload: (defined?(Rails.env) ? (Rails.env.production? || Rails.env.staging?) : nil), migration_errors: false)
1616
profiles&.each { |name, ctx|
1717
ctx[:visibility_profile] = name
1818
ctx.freeze

spec/graphql/schema/visibility_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,33 @@ def self.resolve_type(...); Thing; end
310310
res = InterfaceSuperclassSchema.execute("{ node { id ... on Thing { name } } }")
311311
assert_equal "Hat", res["data"]["node"]["name"]
312312
end
313+
314+
it "defaults to preload: true for Rails.env.staging?" do
315+
if defined?(Rails)
316+
prev_rails = Rails
317+
Object.send :remove_const, :Rails
318+
end
319+
mock_env = OpenStruct.new(:staging? => true)
320+
Object.const_set(:Rails, OpenStruct.new(env: mock_env))
321+
schema = Class.new(GraphQL::Schema) do
322+
use GraphQL::Schema::Visibility
323+
end
324+
assert Rails.env.staging?
325+
assert schema.visibility.preload?
326+
327+
mock_env[:staging?] = false
328+
mock_env[:test?] = true
329+
330+
schema = Class.new(GraphQL::Schema) do
331+
use GraphQL::Schema::Visibility
332+
end
333+
assert Rails.env.test?
334+
refute Rails.env.staging?
335+
refute schema.visibility.preload?
336+
ensure
337+
Object.send(:remove_const, :Rails)
338+
if prev_rails
339+
Object.const_set(:Rails, prev_rails)
340+
end
341+
end
313342
end

0 commit comments

Comments
 (0)