From 860bafd7aaaa02047a6de575d5af506b4e0b196a Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Sat, 14 Nov 2026 11:21:00 +0100 Subject: [PATCH] feat(execution): add execution outcome metadata --- docs/class-reference.md | 7 ++++++ src/Executor/ExecutionResult.php | 8 ++++++ src/Executor/ReferenceExecutor.php | 5 +++- tests/Executor/ExecutionResultTest.php | 1 + tests/Executor/NonNullTest.php | 10 ++++++-- tests/GraphQLTest.php | 35 ++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 3 deletions(-) diff --git a/docs/class-reference.md b/docs/class-reference.md index 14a94a287..8ad80a1f3 100644 --- a/docs/class-reference.md +++ b/docs/class-reference.md @@ -1798,6 +1798,13 @@ public $data; */ public $errors; +/** + * Whether GraphQL operation execution occurred. + * + * @api + */ +public $executed; + /** * User-defined serializable array of extensions included in serialized result. * diff --git a/src/Executor/ExecutionResult.php b/src/Executor/ExecutionResult.php index cc5ad1b44..ceb317688 100644 --- a/src/Executor/ExecutionResult.php +++ b/src/Executor/ExecutionResult.php @@ -54,6 +54,13 @@ class ExecutionResult implements \JsonSerializable */ public array $errors = []; + /** + * Whether GraphQL operation execution occurred. + * + * @api + */ + public bool $executed = false; + /** * User-defined serializable array of extensions included in serialized result. * @@ -87,6 +94,7 @@ public function __construct(?array $data = null, array $errors = [], array $exte $this->data = $data; $this->errors = $errors; $this->extensions = $extensions; + $this->executed = $data !== null; } /** diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index f87bd06c0..b3b342abd 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -281,7 +281,10 @@ protected function buildResponse($data) $data = (array) $data; } - return new ExecutionResult($data, $this->exeContext->errors); + $result = new ExecutionResult($data, $this->exeContext->errors); + $result->executed = true; + + return $result; } /** diff --git a/tests/Executor/ExecutionResultTest.php b/tests/Executor/ExecutionResultTest.php index e63d69d04..47b8f2d26 100644 --- a/tests/Executor/ExecutionResultTest.php +++ b/tests/Executor/ExecutionResultTest.php @@ -12,6 +12,7 @@ public function testToArrayWithoutExtensions(): void { $executionResult = new ExecutionResult(); + self::assertFalse($executionResult->executed); self::assertSame([], $executionResult->toArray()); } diff --git a/tests/Executor/NonNullTest.php b/tests/Executor/NonNullTest.php index 8870d327a..6fe59198f 100644 --- a/tests/Executor/NonNullTest.php +++ b/tests/Executor/NonNullTest.php @@ -961,9 +961,12 @@ public function testNullsTheTopLevelIfAsyncNonNullableFieldErrors(): void ], ]; + $result = Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q'); + + self::assertTrue($result->executed); self::assertArraySubset( $expected, - Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray() + $result->toArray() ); } @@ -1005,9 +1008,12 @@ public function testNullsTheTopLevelIfAsyncNonNullableFieldResolvesNull(): void ], ]; + $result = Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q'); + + self::assertTrue($result->executed); self::assertArraySubset( $expected, - Executor::execute($this->schema, $ast, $this->nullingData, null, [], 'Q')->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE) + $result->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE) ); } } diff --git a/tests/GraphQLTest.php b/tests/GraphQLTest.php index 04c869f99..69b6ce0dd 100644 --- a/tests/GraphQLTest.php +++ b/tests/GraphQLTest.php @@ -36,6 +36,41 @@ public function testPromiseToExecute(): void $promise = GraphQL::promiseToExecute($promiseAdapter, $schema, '{ sayHi(name: "John") }'); $result = $promiseAdapter->wait($promise); + self::assertTrue($result->executed); self::assertSame(['data' => ['sayHi' => 'Hi John!']], $result->toArray()); } + + public function testExecutionOutcomeDistinguishesRequestErrorFromNullData(): void + { + $schema = new Schema( + (new SchemaConfig()) + ->setQuery(new ObjectType([ + 'name' => 'Query', + 'fields' => [ + 'required' => [ + 'type' => Type::nonNull(Type::string()), + 'resolve' => static fn () => null, + ], + ], + ])) + ); + + $requestError = GraphQL::executeQuery($schema, 'not a query'); + self::assertFalse($requestError->executed); + self::assertNotEmpty($requestError->errors); + + $executionResult = GraphQL::executeQuery($schema, '{ required }'); + self::assertTrue($executionResult->executed); + self::assertNull($executionResult->data); + self::assertSame( + [ + 'errors' => [[ + 'message' => 'Internal server error', + 'locations' => [['line' => 1, 'column' => 3]], + 'path' => ['required'], + ]], + ], + $executionResult->toArray() + ); + } }