Skip to content

Commit 5a50495

Browse files
authored
Merge pull request #5597 from rmosolgo/list-dataloader-bug
Use fresh state for list item dataloader fibers
2 parents 3584553 + fa8fd61 commit 5a50495

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

lib/graphql/execution/interpreter/runtime.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ def continue_field(value, owner_type, field, current_type, ast_node, next_select
761761
idx += 1
762762
if use_dataloader_job
763763
@dataloader.append_job do
764-
resolve_list_item(inner_value, inner_type, inner_type_non_null, ast_node, field, owner_object, arguments, this_idx, response_list, owner_type, was_scoped, runtime_state)
764+
resolve_list_item(inner_value, inner_type, inner_type_non_null, ast_node, field, owner_object, arguments, this_idx, response_list, owner_type, was_scoped, nil)
765765
end
766766
else
767767
resolve_list_item(inner_value, inner_type, inner_type_non_null, ast_node, field, owner_object, arguments, this_idx, response_list, owner_type, was_scoped, runtime_state)
@@ -803,6 +803,7 @@ def continue_field(value, owner_type, field, current_type, ast_node, next_select
803803
end
804804

805805
def resolve_list_item(inner_value, inner_type, inner_type_non_null, ast_node, field, owner_object, arguments, this_idx, response_list, owner_type, was_scoped, runtime_state) # rubocop:disable Metrics/ParameterLists
806+
runtime_state ||= get_current_runtime_state
806807
runtime_state.current_result_name = this_idx
807808
runtime_state.current_result = response_list
808809
call_method_on_directives(:resolve_each, owner_object, ast_node.directives) do

spec/graphql/execution/interpreter_spec.rb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,4 +999,102 @@ def exec_multiplex(...)
999999
assert_nil Fiber[:__graphql_runtime_info]
10001000
end
10011001
end
1002+
1003+
describe "list items with dataloader and current_path usage" do
1004+
class ListBugExampleSchema < GraphQL::Schema
1005+
class PathTest < GraphQL::Schema::Directive
1006+
locations(GraphQL::Schema::Directive::INLINE_FRAGMENT)
1007+
1008+
def self.resolve(object, arguments, context)
1009+
context[:test_paths] ||= []
1010+
context[:test_paths] << context[:current_path]
1011+
super
1012+
end
1013+
end
1014+
1015+
class DataloadedSource < GraphQL::Dataloader::Source
1016+
def fetch(objects)
1017+
objects.map(&:dataloaded)
1018+
end
1019+
end
1020+
1021+
class DataloadedType < GraphQL::Schema::Object
1022+
field :int, Integer
1023+
end
1024+
1025+
class SiblingType < GraphQL::Schema::Object
1026+
field :dataloaded, DataloadedType, null: false
1027+
1028+
def dataloaded
1029+
dataload(DataloadedSource, object)
1030+
end
1031+
end
1032+
1033+
1034+
class ChildType < GraphQL::Schema::Object
1035+
field :int, Integer, null: false
1036+
end
1037+
1038+
class ParentType < GraphQL::Schema::Object
1039+
field :children, [ChildType], null: false
1040+
end
1041+
1042+
class Query < GraphQL::Schema::Object
1043+
field :parent, ParentType do
1044+
argument :name, String
1045+
end
1046+
1047+
def parent(name:)
1048+
object.parent
1049+
end
1050+
1051+
field :siblings, [SiblingType], null: false
1052+
end
1053+
1054+
query(Query)
1055+
use GraphQL::Dataloader
1056+
directive PathTest
1057+
end
1058+
1059+
it "correctly provides current_type at selections-level" do
1060+
query_str = <<~GRAPHQL
1061+
query {
1062+
parent(name: "ABC") {
1063+
children {
1064+
... @pathTest {
1065+
int
1066+
}
1067+
}
1068+
}
1069+
siblings {
1070+
dataloaded {
1071+
int
1072+
}
1073+
}
1074+
}
1075+
GRAPHQL
1076+
1077+
root_value = OpenStruct.new(
1078+
parent: OpenStruct.new(
1079+
children: [
1080+
OpenStruct.new(int: 1),
1081+
]
1082+
),
1083+
siblings: [
1084+
OpenStruct.new(dataloaded: OpenStruct.new(int: 2)),
1085+
]
1086+
)
1087+
1088+
result = ListBugExampleSchema.execute(query_str, root_value: root_value)
1089+
expected_result = {
1090+
"data" => {
1091+
"parent" => {"children" => [{"int" => 1}]},
1092+
"siblings" => [{"dataloaded" => {"int" => 2}}]
1093+
}
1094+
}
1095+
1096+
assert_graphql_equal expected_result, result
1097+
assert_equal [["parent", "children", 0]], result.context[:test_paths]
1098+
end
1099+
end
10021100
end

0 commit comments

Comments
 (0)