From 447a3815848c9f14c52fe90202af1e1c0d38e8a5 Mon Sep 17 00:00:00 2001 From: Scott Walkinshaw Date: Thu, 23 Oct 2025 10:58:42 -0400 Subject: [PATCH] Ensure `data` entry exists for execution errors According to the spec, the `data` entry should always exist once execution starts. Only if an error is raised _before_ execution starts should the `data` key be omitted. The only test that needed updating was when an `ExecutionError` was raised during the `execute_query` trace hook. Despite this running before `super`, this still happens "during execution" and should have the `data` key present. Ref: https://spec.graphql.org/draft/#sec-Data --- lib/graphql/execution/interpreter.rb | 1 - spec/graphql/authorization_spec.rb | 7 +++++-- spec/graphql/execution/instrumentation_spec.rb | 9 +++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/graphql/execution/interpreter.rb b/lib/graphql/execution/interpreter.rb index bbc778355d..21d1c0a4f4 100644 --- a/lib/graphql/execution/interpreter.rb +++ b/lib/graphql/execution/interpreter.rb @@ -80,7 +80,6 @@ def run_all(schema, query_options, context: {}, max_complexity: schema.max_compl end rescue GraphQL::ExecutionError => err query.context.errors << err - NO_OPERATION end end results[idx] = result diff --git a/spec/graphql/authorization_spec.rb b/spec/graphql/authorization_spec.rb index b49af2aa44..b06b9f745c 100644 --- a/spec/graphql/authorization_spec.rb +++ b/spec/graphql/authorization_spec.rb @@ -909,8 +909,11 @@ def auth_execute(*args, **kwargs) assert_equal "Query", res["data"]["__typename"] unauth_res = auth_execute(query, context: { query_unauthorized: true }) - assert_nil unauth_res["data"] - assert_equal [{"message"=>"Unauthorized Query: nil"}], unauth_res["errors"] + + assert_equal({ + "errors" => [{"message"=>"Unauthorized Query: nil"}], + "data" => nil, + }, unauth_res.to_h) end describe "when the object authorization raises an UnauthorizedFieldError" do diff --git a/spec/graphql/execution/instrumentation_spec.rb b/spec/graphql/execution/instrumentation_spec.rb index 1044d0da47..c940e9309c 100644 --- a/spec/graphql/execution/instrumentation_spec.rb +++ b/spec/graphql/execution/instrumentation_spec.rb @@ -128,8 +128,13 @@ def int(value:) it "rescues execution errors from execute_query" do context = {raise_execution_error: true} res = schema.execute(" { int(value: 2) } ", context: context) - assert_equal "Raised from trace execute_query", res["errors"].first["message"] - refute res.key?("data"), "The query doesn't run" + + assert_equal({ + "data" => nil, + "errors" => [ + { "message" => "Raised from trace execute_query" }, + ] + }, res.to_h) end it "can assign a query string there" do