Skip to content

Commit 6a9c132

Browse files
authored
Merge pull request #5439 from rmosolgo/add-visibility-profile-to-testing-helpers
Support visibility_profile: ... in testing helpers
2 parents 87d0257 + 0d251d9 commit 6a9c132

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

lib/graphql/testing/helpers.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def initialize(type_name:, field_name:)
3939
end
4040
end
4141

42-
def run_graphql_field(schema, field_path, object, arguments: {}, context: {}, ast_node: nil, lookahead: nil)
42+
def run_graphql_field(schema, field_path, object, arguments: {}, context: {}, ast_node: nil, lookahead: nil, visibility_profile: nil)
4343
type_name, *field_names = field_path.split(".")
44-
dummy_query = GraphQL::Query.new(schema, "{ __typename }", context: context)
44+
dummy_query = GraphQL::Query.new(schema, "{ __typename }", context: context, visibility_profile: visibility_profile)
4545
query_context = dummy_query.context
4646
dataloader = query_context.dataloader
4747
object_type = dummy_query.types.type(type_name) # rubocop:disable Development/ContextIsPassedCop
@@ -104,41 +104,44 @@ def run_graphql_field(schema, field_path, object, arguments: {}, context: {}, as
104104
end
105105
end
106106

107-
def with_resolution_context(schema, type:, object:, context:{})
107+
def with_resolution_context(schema, type:, object:, context:{}, visibility_profile: nil)
108108
resolution_context = ResolutionAssertionContext.new(
109109
self,
110110
schema: schema,
111111
type_name: type,
112112
object: object,
113-
context: context
113+
context: context,
114+
visibility_profile: visibility_profile,
114115
)
115116
yield(resolution_context)
116117
end
117118

118119
class ResolutionAssertionContext
119-
def initialize(test, type_name:, object:, schema:, context:)
120+
def initialize(test, type_name:, object:, schema:, context:, visibility_profile:)
120121
@test = test
121122
@type_name = type_name
122123
@object = object
123124
@schema = schema
124125
@context = context
126+
@visibility_profile = visibility_profile
125127
end
126128

129+
attr_reader :visibility_profile
127130

128131
def run_graphql_field(field_name, arguments: {})
129132
if @schema
130-
@test.run_graphql_field(@schema, "#{@type_name}.#{field_name}", @object, arguments: arguments, context: @context)
133+
@test.run_graphql_field(@schema, "#{@type_name}.#{field_name}", @object, arguments: arguments, context: @context, visibility_profile: @visibility_profile)
131134
else
132-
@test.run_graphql_field("#{@type_name}.#{field_name}", @object, arguments: arguments, context: @context)
135+
@test.run_graphql_field("#{@type_name}.#{field_name}", @object, arguments: arguments, context: @context, visibility_profile: @visibility_profile)
133136
end
134137
end
135138
end
136139

137140
module SchemaHelpers
138141
include Helpers
139142

140-
def run_graphql_field(field_path, object, arguments: {}, context: {})
141-
super(@@schema_class_for_helpers, field_path, object, arguments: arguments, context: context)
143+
def run_graphql_field(field_path, object, arguments: {}, context: {}, visibility_profile: nil)
144+
super(@@schema_class_for_helpers, field_path, object, arguments: arguments, context: context, visibility_profile: visibility_profile)
142145
end
143146

144147
def with_resolution_context(*args, **kwargs, &block)

spec/graphql/testing/helpers_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ def lookahead_selections(lookahead:)
111111
query(Query)
112112
use GraphQL::Dataloader
113113
lazy_resolve Proc, :call
114+
use GraphQL::Schema::Visibility, profiles: {
115+
public: { public: true }
116+
}, dynamic: true
114117

115118
def self.unauthorized_object(err)
116119
raise err
@@ -163,6 +166,16 @@ def self.resolve_type(abs_t, obj, ctx)
163166
end
164167
end
165168

169+
it "works with a visibility_profile" do
170+
student2 = run_graphql_field(AssertionsSchema, "Query.student", nil, visibility_profile: :public, arguments: { student: "s1" })
171+
expected_student = { name: "Student1", type: AssertionsSchema::Student }
172+
assert_equal(expected_student, student2)
173+
174+
with_resolution_context(AssertionsSchema, object: {}, type: "Query", context: {}, visibility_profile: :public) do |rc|
175+
assert_equal :public, rc.visibility_profile
176+
end
177+
end
178+
166179
it "raises an error when the type is hidden" do
167180
assert_equal 1_000_000, run_graphql_field(AssertionsSchema, "TuitionBill.amountInCents", { amount: 1_000_000 }, context: admin_context)
168181

0 commit comments

Comments
 (0)