Skip to content

Commit f0c2122

Browse files
authored
Merge pull request #5326 from rmosolgo/always-visible-use-schema-visibility
Refactor AlwaysVisible to use Schema::Visibility under the hood
2 parents 5a7d96b + 0dd1cce commit f0c2122

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/graphql/schema/always_visible.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module GraphQL
33
class Schema
44
module AlwaysVisible
55
def self.use(schema, **opts)
6+
schema.use(GraphQL::Schema::Visibility, profiles: { nil => {} })
67
schema.extend(self)
78
end
89

lib/graphql/schema/visibility.rb

+13-14
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,22 @@ def migration_errors?
148148

149149
attr_reader :cached_profiles
150150

151-
def profile_for(context, visibility_profile = context[:visibility_profile])
151+
def profile_for(context)
152152
if !@profiles.empty?
153-
if visibility_profile.nil?
154-
if @dynamic
155-
if context.is_a?(Query::NullContext)
156-
top_level_profile
157-
else
158-
@schema.visibility_profile_class.new(context: context, schema: @schema)
159-
end
160-
elsif !@profiles.empty?
161-
raise ArgumentError, "#{@schema} expects a visibility profile, but `visibility_profile:` wasn't passed. Provide a `visibility_profile:` value or add `dynamic: true` to your visibility configuration."
162-
end
163-
elsif !@profiles.include?(visibility_profile)
164-
raise ArgumentError, "`#{visibility_profile.inspect}` isn't allowed for `visibility_profile:` (must be one of #{@profiles.keys.map(&:inspect).join(", ")}). Or, add `#{visibility_profile.inspect}` to the list of profiles in the schema definition."
165-
else
153+
visibility_profile = context[:visibility_profile]
154+
if @profiles.include?(visibility_profile)
166155
profile_ctx = @profiles[visibility_profile]
167156
@cached_profiles[visibility_profile] ||= @schema.visibility_profile_class.new(name: visibility_profile, context: profile_ctx, schema: @schema)
157+
elsif @dynamic
158+
if context.is_a?(Query::NullContext)
159+
top_level_profile
160+
else
161+
@schema.visibility_profile_class.new(context: context, schema: @schema)
162+
end
163+
elsif !context.key?(:visibility_profile)
164+
raise ArgumentError, "#{@schema} expects a visibility profile, but `visibility_profile:` wasn't passed. Provide a `visibility_profile:` value or add `dynamic: true` to your visibility configuration."
165+
else
166+
raise ArgumentError, "`#{visibility_profile.inspect}` isn't allowed for `visibility_profile:` (must be one of #{@profiles.keys.map(&:inspect).join(", ")}). Or, add `#{visibility_profile.inspect}` to the list of profiles in the schema definition."
168167
end
169168
elsif context.is_a?(Query::NullContext)
170169
top_level_profile

0 commit comments

Comments
 (0)