Skip to content

Commit d286f92

Browse files
authored
Merge pull request #5289 from rmosolgo/test-helpers-authorize-dataloader
Fix run_graphql_field when .authorized? uses Dataloader
2 parents fcbfa86 + 67dba63 commit d286f92

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

lib/graphql/testing/helpers.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,21 @@ def run_graphql_field(schema, field_path, object, arguments: {}, context: {}, as
4343
type_name, *field_names = field_path.split(".")
4444
dummy_query = GraphQL::Query.new(schema, "{ __typename }", context: context)
4545
query_context = dummy_query.context
46+
dataloader = query_context.dataloader
4647
object_type = dummy_query.types.type(type_name) # rubocop:disable Development/ContextIsPassedCop
4748
if object_type
4849
graphql_result = object
4950
field_names.each do |field_name|
5051
inner_object = graphql_result
51-
graphql_result = object_type.wrap(inner_object, query_context)
52+
dataloader.run_isolated {
53+
graphql_result = object_type.wrap(inner_object, query_context)
54+
}
5255
if graphql_result.nil?
5356
return nil
5457
end
5558
visible_field = dummy_query.types.field(object_type, field_name) # rubocop:disable Development/ContextIsPassedCop
5659
if visible_field
57-
dummy_query.context.dataloader.run_isolated {
60+
dataloader.run_isolated {
5861
query_context[:current_field] = visible_field
5962
field_args = visible_field.coerce_arguments(graphql_result, arguments, query_context)
6063
field_args = schema.sync_lazy(field_args)

spec/graphql/schema/field_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,9 @@ def resolve
836836
# This is custom state added by some test schemas:
837837
custom_ivars = [:@upcase, :@future_schema, :@visible, :@allow_for, :@metadata, :@admin_only]
838838

839+
# Remove any invalid (non-retained) field instances from the heap
840+
GC.start
841+
839842
ObjectSpace.each_object(GraphQL::Schema::Field) do |field_obj|
840843
field_ivars = field_obj.instance_variables
841844
custom_ivars.each do |ivar|

spec/graphql/testing/helpers_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ def current_field
8181
end
8282
end
8383

84+
class AuthorizedObject < GraphQL::Schema::Object
85+
def self.authorized?(object, context)
86+
context.dataloader.with(BillSource).load(object)[:amount] > 5
87+
end
88+
89+
field :id, ID
90+
end
8491
class Query < GraphQL::Schema::Object
8592
field :students, [Student]
8693

@@ -97,6 +104,8 @@ def student(student:)
97104
def lookahead_selections(lookahead:)
98105
lookahead.selections.to_s
99106
end
107+
108+
field :authorized_object, AuthorizedObject
100109
end
101110

102111
query(Query)
@@ -199,6 +208,10 @@ def self.resolve_type(abs_t, obj, ctx)
199208
assert_equal "An instance of Hash failed AssertionsSchema::Student's authorization check on field ssn", err.message
200209
end
201210

211+
it "works when .authorized? calls dataloader" do
212+
assert_equal "100", run_graphql_field(AssertionsSchema, "AuthorizedObject.id", { id: "100" })
213+
end
214+
202215
it "raises when the type doesn't exist" do
203216
err = assert_raises GraphQL::Testing::Helpers::TypeNotDefinedError do
204217
run_graphql_field(AssertionsSchema, "Nothing.nothing", :nothing)

0 commit comments

Comments
 (0)