From 018b743bb1246f85987dcf8ce6f9530f962ddaca Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Wed, 30 Jul 2025 06:35:41 -0400 Subject: [PATCH] Visibility: Default to preload: true when env.staging? --- lib/graphql/schema/visibility.rb | 4 ++-- spec/graphql/schema/visibility_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/graphql/schema/visibility.rb b/lib/graphql/schema/visibility.rb index 0ac274a786..61e9c716a1 100644 --- a/lib/graphql/schema/visibility.rb +++ b/lib/graphql/schema/visibility.rb @@ -10,9 +10,9 @@ class Schema class Visibility # @param schema [Class] # @param profiles [Hash Hash>] A hash of `name => context` pairs for preloading visibility profiles - # @param preload [Boolean] if `true`, load the default schema profile and all named profiles immediately (defaults to `true` for `Rails.env.production?`) + # @param preload [Boolean] if `true`, load the default schema profile and all named profiles immediately (defaults to `false` for `Rails.env.development?`) # @param migration_errors [Boolean] if `true`, raise an error when `Visibility` and `Warden` return different results - def self.use(schema, dynamic: false, profiles: EmptyObjects::EMPTY_HASH, preload: (defined?(Rails.env) ? Rails.env.production? : nil), migration_errors: false) + def self.use(schema, dynamic: false, profiles: EmptyObjects::EMPTY_HASH, preload: (defined?(Rails.env) ? !Rails.env.development? : nil), migration_errors: false) profiles&.each { |name, ctx| ctx[:visibility_profile] = name ctx.freeze diff --git a/spec/graphql/schema/visibility_spec.rb b/spec/graphql/schema/visibility_spec.rb index 507599f15d..cbf5175ad2 100644 --- a/spec/graphql/schema/visibility_spec.rb +++ b/spec/graphql/schema/visibility_spec.rb @@ -310,4 +310,28 @@ def self.resolve_type(...); Thing; end res = InterfaceSuperclassSchema.execute("{ node { id ... on Thing { name } } }") assert_equal "Hat", res["data"]["node"]["name"] end + + focus + it "defaults to preload: true for Rails.env.staging?" do + prev_rails = defined?(Rails) ? Rails : nil + mock_env = OpenStruct.new(:development? => false) + Object.const_set(:Rails, OpenStruct.new(env: mock_env)) + schema = Class.new(GraphQL::Schema) do + use GraphQL::Schema::Visibility + end + refute Rails.env.development? + assert schema.visibility.preload? + + mock_env[:development?] = true + + schema = Class.new(GraphQL::Schema) do + use GraphQL::Schema::Visibility + end + assert Rails.env.development? + refute schema.visibility.preload? + ensure + if prev_rails + const_set(:Rails, prev_rails) + end + end end