From a66216810dcead5eaba557c90d5979050be40b9d Mon Sep 17 00:00:00 2001 From: Evan Huus Date: Mon, 5 Feb 2024 17:10:48 -0500 Subject: [PATCH] Reorder errors before data in the response --- lib/graphql/execution/interpreter.rb | 6 +++--- spec/graphql/execution/interpreter_spec.rb | 13 +++++++++++++ spec/graphql/execution/multiplex_spec.rb | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/graphql/execution/interpreter.rb b/lib/graphql/execution/interpreter.rb index fc34f4966a..174d874c9f 100644 --- a/lib/graphql/execution/interpreter.rb +++ b/lib/graphql/execution/interpreter.rb @@ -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__) diff --git a/spec/graphql/execution/interpreter_spec.rb b/spec/graphql/execution/interpreter_spec.rb index 95980029bc..432abc901a 100644 --- a/spec/graphql/execution/interpreter_spec.rb +++ b/spec/graphql/execution/interpreter_spec.rb @@ -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 { diff --git a/spec/graphql/execution/multiplex_spec.rb b/spec/graphql/execution/multiplex_spec.rb index b8d1c8624e..bf62db0512 100644 --- a/spec/graphql/execution/multiplex_spec.rb +++ b/spec/graphql/execution/multiplex_spec.rb @@ -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