Skip to content

Commit

Permalink
Merge pull request #4823 from eapache-opslevel/errors-before-data
Browse files Browse the repository at this point in the history
Reorder errors before data in the response
  • Loading branch information
rmosolgo committed Feb 5, 2024
2 parents 6779577 + a662168 commit 18f3ded
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/graphql/execution/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ def run_all(schema, query_options, context: {}, max_complexity: schema.max_compl
data_result
end
else
result = {
"data" => query.context.namespace(:interpreter_runtime)[:runtime].final_result
}
result = {}

if query.context.errors.any?
error_result = query.context.errors.map(&:to_h)
result["errors"] = error_result
end

result["data"] = query.context.namespace(:interpreter_runtime)[:runtime].final_result

result
end
if query.context.namespace?(:__query_result_extensions__)
Expand Down
13 changes: 13 additions & 0 deletions spec/graphql/execution/interpreter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,19 @@ def execute_multiplex(multiplex:)
assert_nil Thread.current[:__graphql_runtime_info]
end

it "places errors ahead of data in the response" do
query_str = <<-GRAPHQL
{
expansion(sym: "XYZ") {
name
}
}
GRAPHQL

res = InterpreterTest::Schema.execute(query_str)
assert_equal ["errors", "data"], res.keys
end

it "propagates nulls in lists" do
query_str = <<-GRAPHQL
{
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/execution/multiplex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def raise_exception

it "can access the query results" do
InspectSchema.execute("{ raiseExecutionError }")
handled_err_json = '{"data":{"raiseExecutionError":null},"errors":[{"message":"Whoops","locations":[{"line":1,"column":3}],"path":["raiseExecutionError"]}]}'
handled_err_json = '{"errors":[{"message":"Whoops","locations":[{"line":1,"column":3}],"path":["raiseExecutionError"]}],"data":{"raiseExecutionError":null}}'
assert_equal handled_err_json, InspectQueryInstrumentation.last_json


Expand Down

0 comments on commit 18f3ded

Please sign in to comment.