Skip to content

Commit

Permalink
Rename Subset => Profile
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Oct 4, 2024
1 parent b9932a0 commit 23c79bb
Show file tree
Hide file tree
Showing 22 changed files with 124 additions and 123 deletions.
14 changes: 7 additions & 7 deletions lib/graphql/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ def selected_operation_name
# @param max_depth [Numeric] the maximum number of nested selections allowed for this query (falls back to schema-level value)
# @param max_complexity [Numeric] the maximum field complexity for this query (falls back to schema-level value)
# @param visibility_profile [Symbol]
def initialize(schema, query_string = nil, query: nil, document: nil, context: nil, variables: nil, validate: true, static_validator: nil, visibility_profile: nil, subscription_topic: nil, operation_name: nil, root_value: nil, max_depth: schema.max_depth, max_complexity: schema.max_complexity, warden: nil, use_schema_subset: nil)
def initialize(schema, query_string = nil, query: nil, document: nil, context: nil, variables: nil, validate: true, static_validator: nil, visibility_profile: nil, subscription_topic: nil, operation_name: nil, root_value: nil, max_depth: schema.max_depth, max_complexity: schema.max_complexity, warden: nil, use_visibility_profile: nil)
# Even if `variables: nil` is passed, use an empty hash for simpler logic
variables ||= {}
@schema = schema
@context = schema.context_class.new(query: self, values: context)

if use_schema_subset.nil?
use_schema_subset = warden ? false : schema.use_schema_visibility?
if use_visibility_profile.nil?
use_visibility_profile = warden ? false : schema.use_visibility_profile?
end

@visibility_profile = visibility_profile

if use_schema_subset
@schema_subset = @schema.visibility.profile_for(@context, visibility_profile)
if use_visibility_profile
@visibility_profile = @schema.visibility.profile_for(@context, visibility_profile)
@warden = Schema::Warden::NullWarden.new(context: @context, schema: @schema)
else
@schema_subset = nil
@visibility_profile = nil
@warden = warden
end

Expand Down Expand Up @@ -375,7 +375,7 @@ def root_type_for_operation(op_type)
end

def types
@schema_subset || warden.schema_subset
@visibility_profile || warden.visibility_profile
end

# @param abstract_type [GraphQL::UnionType, GraphQL::InterfaceType]
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/query/null_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize
end

def types
@types ||= GraphQL::Schema::Warden::SchemaSubset.new(@warden)
@types ||= Schema::Warden::VisibilityProfile.new(@warden)
end
end
end
Expand Down
71 changes: 37 additions & 34 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ def plugins
# @return [Hash<String => Class>] A dictionary of type classes by their GraphQL name
# @see get_type Which is more efficient for finding _one type_ by name, because it doesn't merge hashes.
def types(context = GraphQL::Query::NullContext.instance)
if use_schema_visibility?
types = Visibility::Subset.from_context(context, self)
if use_visibility_profile?
types = Visibility::Profile.from_context(context, self)
return types.all_types_h
end
all_types = non_introspection_types.merge(introspection_system.types)
Expand Down Expand Up @@ -366,18 +366,18 @@ def types(context = GraphQL::Query::NullContext.instance)

# @param type_name [String]
# @param context [GraphQL::Query::Context] Used for filtering definitions at query-time
# @param use_schema_visibility Private, for migration to {Schema::Visibility}
# @param use_visibility_profile Private, for migration to {Schema::Visibility}
# @return [Module, nil] A type, or nil if there's no type called `type_name`
def get_type(type_name, context = GraphQL::Query::NullContext.instance, use_schema_visibility = use_schema_visibility?)
if use_schema_visibility
return Visibility::Subset.from_context(context, self).type(type_name)
def get_type(type_name, context = GraphQL::Query::NullContext.instance, use_visibility_profile = use_visibility_profile?)
if use_visibility_profile
return Visibility::Profile.from_context(context, self).type(type_name)
end
local_entry = own_types[type_name]
type_defn = case local_entry
when nil
nil
when Array
if context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Subset)
if context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Profile)
local_entry
else
visible_t = nil
Expand All @@ -403,7 +403,7 @@ def get_type(type_name, context = GraphQL::Query::NullContext.instance, use_sche

type_defn ||
introspection_system.types[type_name] || # todo context-specific introspection?
(superclass.respond_to?(:get_type) ? superclass.get_type(type_name, context, use_schema_visibility) : nil)
(superclass.respond_to?(:get_type) ? superclass.get_type(type_name, context, use_visibility_profile) : nil)
end

# @return [Boolean] Does this schema have _any_ definition for a type named `type_name`, regardless of visibility?
Expand Down Expand Up @@ -435,7 +435,7 @@ def query(new_query_object = nil, &lazy_load_block)
if @query_object
dup_defn = new_query_object || yield
raise GraphQL::Error, "Second definition of `query(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@query_object.inspect}"
elsif use_schema_visibility?
elsif use_visibility_profile?
@query_object = block_given? ? lazy_load_block : new_query_object
else
@query_object = new_query_object || lazy_load_block.call
Expand All @@ -454,7 +454,7 @@ def mutation(new_mutation_object = nil, &lazy_load_block)
if @mutation_object
dup_defn = new_mutation_object || yield
raise GraphQL::Error, "Second definition of `mutation(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@mutation_object.inspect}"
elsif use_schema_visibility?
elsif use_visibility_profile?
@mutation_object = block_given? ? lazy_load_block : new_mutation_object
else
@mutation_object = new_mutation_object || lazy_load_block.call
Expand All @@ -473,7 +473,7 @@ def subscription(new_subscription_object = nil, &lazy_load_block)
if @subscription_object
dup_defn = new_subscription_object || yield
raise GraphQL::Error, "Second definition of `subscription(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@subscription_object.inspect}"
elsif use_schema_visibility?
elsif use_visibility_profile?
@subscription_object = block_given? ? lazy_load_block : new_subscription_object
add_subscription_extension_if_necessary
else
Expand Down Expand Up @@ -507,7 +507,7 @@ def root_type_for_operation(operation)
end

def root_types
if use_schema_visibility?
if use_visibility_profile?
[query, mutation, subscription].compact
else
@root_types
Expand All @@ -526,40 +526,43 @@ def warden_class

attr_writer :warden_class

def subset_class
if defined?(@subset_class)
@subset_class
elsif superclass.respond_to?(:subset_class)
superclass.subset_class
# @api private
def visibility_profile_class
if defined?(@visibility_profile_class)
@visibility_profile_class
elsif superclass.respond_to?(:visibility_profile_class)
superclass.visibility_profile_class
else
GraphQL::Schema::Visibility::Subset
GraphQL::Schema::Visibility::Profile
end
end

attr_writer :subset_class, :use_schema_visibility
# @api private
attr_writer :visibility_profile_class, :use_visibility_profile
# @api private
attr_accessor :visibility

def use_schema_visibility?
if defined?(@use_schema_visibility)
@use_schema_visibility
elsif superclass.respond_to?(:use_schema_visibility?)
superclass.use_schema_visibility?
# @api private
def use_visibility_profile?
if defined?(@use_visibility_profile)
@use_visibility_profile
elsif superclass.respond_to?(:use_visibility_profile?)
superclass.use_visibility_profile?
else
false
end
end

# @param type [Module] The type definition whose possible types you want to see
# @param context [GraphQL::Query::Context] used for filtering visible possible types at runtime
# @param use_schema_visibility Private, for migration to {Schema::Visibility}
# @param use_visibility_profile Private, for migration to {Schema::Visibility}
# @return [Hash<String, Module>] All possible types, if no `type` is given.
# @return [Array<Module>] Possible types for `type`, if it's given.
def possible_types(type = nil, context = GraphQL::Query::NullContext.instance, use_schema_visibility = use_schema_visibility?)
if use_schema_visibility
def possible_types(type = nil, context = GraphQL::Query::NullContext.instance, use_visibility_profile = use_visibility_profile?)
if use_visibility_profile
if type
return Visibility::Subset.from_context(context, self).possible_types(type)
return Visibility::Profile.from_context(context, self).possible_types(type)
else
raise "Schema.possible_types is not implemented for `use_schema_visibility?`"
raise "Schema.possible_types is not implemented for `use_visibility_profile?`"
end
end
if type
Expand All @@ -579,7 +582,7 @@ def possible_types(type = nil, context = GraphQL::Query::NullContext.instance, u
introspection_system.possible_types[type] ||
(
superclass.respond_to?(:possible_types) ?
superclass.possible_types(type, context, use_schema_visibility) :
superclass.possible_types(type, context, use_visibility_profile) :
EMPTY_ARRAY
)
end
Expand Down Expand Up @@ -935,7 +938,7 @@ def orphan_types(*new_orphan_types)
To add other types to your schema, you might want `extra_types`: https://graphql-ruby.org/schema/definition.html#extra-types
ERR
end
add_type_and_traverse(new_orphan_types, root: false) unless use_schema_visibility?
add_type_and_traverse(new_orphan_types, root: false) unless use_visibility_profile?
own_orphan_types.concat(new_orphan_types.flatten)
end

Expand Down Expand Up @@ -1078,7 +1081,7 @@ def inherited(child_class)
end
child_class.singleton_class.prepend(ResolveTypeWithType)

if use_schema_visibility?
if use_visibility_profile?
vis = self.visibility
child_class.visibility = vis.dup_for(child_class)
end
Expand Down Expand Up @@ -1199,7 +1202,7 @@ def directives(*new_directives)
# @param new_directive [Class]
# @return void
def directive(new_directive)
if use_schema_visibility?
if use_visibility_profile?
own_directives[new_directive.graphql_name] = new_directive
else
add_type_and_traverse(new_directive, root: false)
Expand Down
4 changes: 2 additions & 2 deletions lib/graphql/schema/member/has_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def all_argument_definitions

def get_argument(argument_name, context = GraphQL::Query::NullContext.instance)
warden = Warden.from_context(context)
skip_visible = context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Subset)
skip_visible = context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Profile)
for ancestor in ancestors
if ancestor.respond_to?(:own_arguments) &&
(a = ancestor.own_arguments[argument_name]) &&
Expand Down Expand Up @@ -210,7 +210,7 @@ def all_argument_definitions
# @return [GraphQL::Schema::Argument, nil] Argument defined on this thing, fetched by name.
def get_argument(argument_name, context = GraphQL::Query::NullContext.instance)
warden = Warden.from_context(context)
if (arg_config = own_arguments[argument_name]) && ((context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Subset)) || (visible_arg = Warden.visible_entry?(:visible_argument?, arg_config, context, warden)))
if (arg_config = own_arguments[argument_name]) && ((context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Profile)) || (visible_arg = Warden.visible_entry?(:visible_argument?, arg_config, context, warden)))
visible_arg || arg_config
elsif defined?(@resolver_class) && @resolver_class
@resolver_class.get_field_argument(argument_name, context)
Expand Down
4 changes: 2 additions & 2 deletions lib/graphql/schema/member/has_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def all_field_definitions
module InterfaceMethods
def get_field(field_name, context = GraphQL::Query::NullContext.instance)
warden = Warden.from_context(context)
skip_visible = context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Subset)
skip_visible = context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Profile)
for ancestor in ancestors
if ancestor.respond_to?(:own_fields) &&
(f_entry = ancestor.own_fields[field_name]) &&
Expand Down Expand Up @@ -135,7 +135,7 @@ def get_field(field_name, context = GraphQL::Query::NullContext.instance)
# Objects need to check that the interface implementation is visible, too
warden = Warden.from_context(context)
ancs = ancestors
skip_visible = context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Subset)
skip_visible = context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Profile)
i = 0
while (ancestor = ancs[i])
if ancestor.respond_to?(:own_fields) &&
Expand Down
12 changes: 6 additions & 6 deletions lib/graphql/schema/visibility.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require "graphql/schema/visibility/subset"
require "graphql/schema/visibility/profile"
require "graphql/schema/visibility/migration"

module GraphQL
Expand All @@ -17,9 +17,9 @@ def self.use(schema, dynamic: false, profiles: EmptyObjects::EMPTY_ARRAY, preloa

def initialize(schema, dynamic:, preload:, profiles:, migration_errors:)
@schema = schema
schema.use_schema_visibility = true
schema.use_visibility_profile = true
if migration_errors
schema.subset_class = Migration
schema.visibility_profile_class = Migration
end
@profiles = profiles
@cached_profiles = {}
Expand Down Expand Up @@ -57,17 +57,17 @@ def profile_for(context, visibility_profile)
if @profiles.any?
if visibility_profile.nil?
if @dynamic
@schema.subset_class.new(context: context, schema: @schema)
@schema.visibility_profile_class.new(context: context, schema: @schema)
elsif @profiles.any?
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 !@profiles.include?(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
@cached_profiles[visibility_profile] ||= @schema.subset_class.new(name: visibility_profile, context: context, schema: @schema)
@cached_profiles[visibility_profile] ||= @schema.visibility_profile_class.new(name: visibility_profile, context: context, schema: @schema)
end
else
@schema.subset_class.new(context: context, schema: @schema)
@schema.visibility_profile_class.new(context: context, schema: @schema)
end
end
end
Expand Down
Loading

0 comments on commit 23c79bb

Please sign in to comment.