class GraphQL::Schema::Visibility
Use this plugin to make some parts of your schema hidden from some viewers.
Attributes
Public Class Methods
Source
# File lib/graphql/schema/visibility.rb, line 35 def initialize(schema, dynamic:, preload:, profiles:, migration_errors:, configuration_inherited: false) @schema = schema schema.use_visibility_profile = true schema.visibility_profile_class = if migration_errors Visibility::Migration else Visibility::Profile end @preload = preload @profiles = profiles @cached_profiles = {} @dynamic = dynamic @migration_errors = migration_errors # Top-level type caches: @visit = nil @interface_type_memberships = nil @directives = nil @types = nil @all_references = nil @loaded_all = false @configuration_inherited = configuration_inherited if preload self.preload end end
# File lib/graphql/schema/visibility.rb, line 27 def self.use(schema, dynamic: false, profiles: EmptyObjects::EMPTY_HASH, preload: (defined?(Rails.env) ? (Rails.env.production? || Rails.env.staging? || nil) : false), migration_errors: false) profiles&.each { |name, ctx| ctx[:visibility_profile] = name ctx.freeze } schema.visibility = self.new(schema, dynamic: dynamic, preload: preload, profiles: profiles, migration_errors: migration_errors) end
Parameters
-
schema(Class<GraphQL::Schema>) -
profiles(Hash<Symbol => Hash>) — A hash ofname => contextpairs for preloading visibility profiles -
preload(Boolean) — iftrue, load the default schema profile and all named profiles immediately (defaults totrueforRails.env.production?andRails.env.staging?) -
migration_errors(Boolean) — iftrue, raise an error whenVisibilityandWardenreturn different results
Public Instance Methods
Source
# File lib/graphql/schema/visibility.rb, line 69 def all_directives load_all @directives end
# File lib/graphql/schema/visibility.rb, line 74 def all_interface_type_memberships load_all @interface_type_memberships end
Source
# File lib/graphql/schema/visibility.rb, line 79 def all_references load_all @all_references end
Source
# File lib/graphql/schema/visibility.rb, line 61 def freeze load_all @visit = true @interface_type_memberships.default_proc = nil @all_references.default_proc = nil super end
Calls superclass method
Source
# File lib/graphql/schema/visibility.rb, line 84 def get_type(type_name) load_all @types[type_name] end
Source
# File lib/graphql/schema/visibility.rb, line 153 def migration_errors? @migration_errors end
Source
# File lib/graphql/schema/visibility.rb, line 95 def preload # Traverse the schema now (and in the *_configured hooks below) # To make sure things are loaded during boot @preloaded_types = Set.new types_to_visit = [ @schema.query, @schema.mutation, @schema.subscription, *@schema.introspection_system.types.values, *@schema.introspection_system.entry_points.map { |ep| ep.type.unwrap }, *@schema.orphan_types, ] # Root types may have been nil: types_to_visit.compact! ensure_all_loaded(types_to_visit) @cached_profiles.clear @profiles.each do |profile_name, example_ctx| prof = profile_for(example_ctx) prof.preload end end
Source
# File lib/graphql/schema/visibility.rb, line 159 def profile_for(context) if !@profiles.empty? 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, visibility: self) elsif @dynamic if context.is_a?(Query::NullContext) top_level_profile else @schema.visibility_profile_class.new(context: context, schema: @schema, visibility: self) 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 else @schema.visibility_profile_class.new(context: context, schema: @schema, visibility: self) end end
# File lib/graphql/schema/visibility.rb, line 187 def top_level_profile(refresh: false) if refresh @top_level_profile = nil end @top_level_profile ||= @schema.visibility_profile_class.new(context: @schema.null_context, schema: @schema, visibility: self) end