Skip to content

Commit

Permalink
Merge pull request #5026 from juanrgon/issue-5020--2.2.x-fix
Browse files Browse the repository at this point in the history
Backport fix to Issue #5020 to 2.2.x
  • Loading branch information
rmosolgo authored Jul 17, 2024
2 parents 3f32a78 + 9a9fd1a commit ae16068
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/graphql/dataloader/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ def result_for(key)
end
result = @results[key]

raise result if result.class <= StandardError
if result.is_a?(StandardError)
# Dup it because the rescuer may modify it.
# (This happens for GraphQL::ExecutionErrors, at least)
raise result.dup
end

result
end
Expand Down
95 changes: 95 additions & 0 deletions spec/graphql/execution_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,99 @@
]
assert_equal(expected_errors, result["errors"])
end

describe "when using DataLoaders" do
let(:schema) do
item_error_loader = Class.new(GraphQL::Dataloader::Source) do
def fetch(keys)
keys.map { |key| GraphQL::ExecutionError.new("Error for #{key}") }
end
end

query_type = Class.new(GraphQL::Schema::Object) do
graphql_name "Query"
field :item, String do
argument :key, String
end
define_method(:item) do |key:|
dataloader.with(item_error_loader).load(key)
end
end

Class.new(GraphQL::Schema) do
query query_type
use GraphQL::Dataloader
end
end

let(:result) { schema.execute(query_string) }

describe "when querying for unique items" do
let(:query_string) {
<<-GRAPHQL
query {
query0: item(key: "a")
query1: item(key: "b")
}
GRAPHQL
}

it "returns unique execution errors locations and paths" do
expected_result = {
"data" => {
"query0" => nil,
"query1" => nil
},
"errors" => [
{
"message" => "Error for a",
"locations" => [{"line" => 2, "column" => 13}],
"path" => ["query0"]
},
{
"message" => "Error for b",
"locations" => [{"line" => 3, "column" => 13}],
"path" => ["query1"]
}
]
}

assert_equal(expected_result, result.to_h)
end
end

describe "when querying for duplicate items" do
let(:query_string) {
<<-GRAPHQL
query {
query0: item(key: "a")
query1: item(key: "a")
}
GRAPHQL
}

it "returns execution errors for duplicate items" do
expected_result = {
"data" => {
"query0" => nil,
"query1" => nil
},
"errors" => [
{
"message" => "Error for a",
"locations" => [{"line" => 2, "column" => 13}],
"path" => ["query0"]
},
{
"message" => "Error for a",
"locations" => [{"line" => 3, "column" => 13}],
"path" => ["query1"]
}
]
}

assert_equal(expected_result, result.to_h)
end
end
end
end

0 comments on commit ae16068

Please sign in to comment.