Skip to content

Refactor AlwaysVisible to use Schema::Visibility under the hood #5326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
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
1 change: 1 addition & 0 deletions lib/graphql/schema/always_visible.rb
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ module GraphQL
class Schema
module AlwaysVisible
def self.use(schema, **opts)
schema.use(GraphQL::Schema::Visibility, profiles: { nil => {} })
schema.extend(self)
end

27 changes: 13 additions & 14 deletions lib/graphql/schema/visibility.rb
Original file line number Diff line number Diff line change
@@ -148,23 +148,22 @@ def migration_errors?

attr_reader :cached_profiles

def profile_for(context, visibility_profile = context[:visibility_profile])
def profile_for(context)
if [email protected]?
if visibility_profile.nil?
if @dynamic
if context.is_a?(Query::NullContext)
top_level_profile
else
@schema.visibility_profile_class.new(context: context, schema: @schema)
end
elsif [email protected]?
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."
end
elsif [email protected]?(visibility_profile)
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."
else
visibility_profile = context[:visibility_profile]
if @profiles.include?(visibility_profile)
profile_ctx = @profiles[visibility_profile]
@cached_profiles[visibility_profile] ||= @schema.visibility_profile_class.new(name: visibility_profile, context: profile_ctx, schema: @schema)
elsif @dynamic
if context.is_a?(Query::NullContext)
top_level_profile
else
@schema.visibility_profile_class.new(context: context, schema: @schema)
end
elsif !context.key?(:visibility_profile)
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."
else
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."
end
elsif context.is_a?(Query::NullContext)
top_level_profile
Loading
Oops, something went wrong.