Skip to content

Commit

Permalink
Check .visible? on enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Nov 4, 2024
1 parent 1b04dd9 commit 56d58da
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/graphql/query/null_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ class NullSchema < GraphQL::Schema
extend Forwardable

attr_reader :schema, :query, :warden, :dataloader
def_delegators GraphQL::EmptyObjects::EMPTY_HASH, :[], :fetch, :dig, :key?
def_delegators GraphQL::EmptyObjects::EMPTY_HASH, :[], :fetch, :dig, :key?, :to_h

def initialize
@query = NullQuery.new
@dataloader = GraphQL::Dataloader::NullDataloader.new
@schema = NullSchema
@warden = Schema::Warden::NullWarden.new(context: self, schema: @schema)
@types = @warden.visibility_profile
freeze
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/enum_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def value(new_val = nil)
end

def inspect
"#<#{self.class} #{path} @value=#{@value.inspect}#{description ? " @description=#{description.inspect}" : ""}>"
"#<#{self.class} #{path} @value=#{@value.inspect}#{description ? " @description=#{description.inspect}" : ""}#{deprecation_reason ? " @deprecation_reason=#{deprecation_reason.inspect}" : ""}>"
end

def visible?(_ctx); true; end
Expand Down
6 changes: 4 additions & 2 deletions lib/graphql/schema/visibility/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ def self.from_context(ctx, schema)
end
end

def self.pass_thru(context:, schema:)
self.new(name: "PassThru", context: context, schema: schema)
def self.null_profile(context:, schema:)
profile = self.new(name: "NullProfile", context: context, schema: schema)
profile.instance_variable_set(:@cached_visible, Hash.new { |k, v| k[v] = true }.compare_by_identity)
profile
end

# @return [Symbol, nil]
Expand Down
11 changes: 2 additions & 9 deletions lib/graphql/schema/warden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,17 @@ def initialize(_filter = nil, context:, schema:)
# No-op, but for compatibility:
attr_writer :skip_warning

# @api private
module NullVisibilityProfile
def self.new(context:, schema:)
NullWarden.new(context: context, schema: schema).visibility_profile
end
end

attr_reader :visibility_profile

def visible_field?(field_defn, _ctx = nil, owner = nil); true; end
def visible_argument?(arg_defn, _ctx = nil); true; end
def visible_type?(type_defn, _ctx = nil); true; end
def visible_enum_value?(enum_value, _ctx = nil); true; end
def visible_enum_value?(enum_value, _ctx = nil); enum_value.visible?(Query::NullContext.instance); end
def visible_type_membership?(type_membership, _ctx = nil); true; end
def interface_type_memberships(obj_type, _ctx = nil); obj_type.interface_type_memberships; end
def get_type(type_name); @schema.get_type(type_name, Query::NullContext.instance, false); end # rubocop:disable Development/ContextIsPassedCop
def arguments(argument_owner, ctx = nil); argument_owner.all_argument_definitions; end
def enum_values(enum_defn); enum_defn.enum_values; end # rubocop:disable Development/ContextIsPassedCop
def enum_values(enum_defn); enum_defn.enum_values(Query::NullContext.instance); end # rubocop:disable Development/ContextIsPassedCop
def get_argument(parent_type, argument_name); parent_type.get_argument(argument_name); end # rubocop:disable Development/ContextIsPassedCop
def types; @schema.types; end # rubocop:disable Development/ContextIsPassedCop
def root_type_for_operation(op_name); @schema.root_type_for_operation(op_name); end
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/testing/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def run_graphql_field(schema, field_path, object, arguments: {}, context: {}, as
end
graphql_result
else
unfiltered_type = Schema::Visibility::Profile.pass_thru(schema: schema, context: context).type(type_name)
unfiltered_type = Schema::Visibility::Profile.null_profile(schema: schema, context: context).type(type_name)
if unfiltered_type
raise TypeNotVisibleError.new(type_name: type_name)
else
Expand Down

0 comments on commit 56d58da

Please sign in to comment.