Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/class-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Executor/ExecutionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
simPod marked this conversation as resolved.
Resolved

return $result;
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Executor/ExecutionResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function testToArrayWithoutExtensions(): void
{
$executionResult = new ExecutionResult();

self::assertFalse($executionResult->executed);
self::assertSame([], $executionResult->toArray());
}

Expand Down
10 changes: 8 additions & 2 deletions tests/Executor/NonNullTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand Down Expand Up @@ -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)
);
}
}
35 changes: 35 additions & 0 deletions tests/GraphQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
Loading