Version 4.6.0

This commit is contained in:
Neo committed 2025-12-19 05:55:31 -08:00
1 parent f79dcf067a
commit 517a5e1f70
2036 files changed
+110041 -26162

No files matched your search

+51 -43
View File
@@ -1,8 +1,15 @@
<?php
declare(strict_types=1);
namespace GraphQL\Executor;
use GraphQL\Error\DebugFlag;
use GraphQL\Error\Error;
use GraphQL\Error\FormattedError;
use JsonSerializable;
use function array_map;
use function count;
/**
* Returned after [query execution](executing-queries.md).
@@ -12,13 +19,13 @@ use GraphQL\Error\FormattedError;
* Could be converted to [spec-compliant](https://facebook.github.io/graphql/#sec-Response-Format)
* serializable array using `toArray()`
*/
class ExecutionResult implements \JsonSerializable
class ExecutionResult implements JsonSerializable
{
/**
* Data collected from resolvers during query execution
*
* @api
* @var array
* @var mixed[]
*/
public $data;
@@ -29,38 +36,34 @@ class ExecutionResult implements \JsonSerializable
* contain original exception.
*
* @api
* @var \GraphQL\Error\Error[]
* @var Error[]
*/
public $errors;
/**
* User-defined serializable array of extensions included in serialized result.
* Conforms to
*
* @api
* @var array
* @var mixed[]
*/
public $extensions;
/**
* @var callable
*/
/** @var callable */
private $errorFormatter;
/**
* @var callable
*/
/** @var callable */
private $errorsHandler;
/**
* @param array $data
* @param array $errors
* @param array $extensions
* @param mixed[] $data
* @param Error[] $errors
* @param mixed[] $extensions
*/
public function __construct(array $data = null, array $errors = [], array $extensions = [])
public function __construct($data = null, array $errors = [], array $extensions = [])
{
$this->data = $data;
$this->errors = $errors;
$this->data = $data;
$this->errors = $errors;
$this->extensions = $extensions;
}
@@ -77,13 +80,14 @@ class ExecutionResult implements \JsonSerializable
* // ... other keys
* );
*
* @return self
*
* @api
* @param callable $errorFormatter
* @return $this
*/
public function setErrorFormatter(callable $errorFormatter)
{
$this->errorFormatter = $errorFormatter;
return $this;
}
@@ -97,16 +101,25 @@ class ExecutionResult implements \JsonSerializable
* return array_map($formatter, $errors);
* }
*
* @return self
*
* @api
* @param callable $handler
* @return $this
*/
public function setErrorsHandler(callable $handler)
{
$this->errorsHandler = $handler;
return $this;
}
/**
* @return mixed[]
*/
public function jsonSerialize()
{
return $this->toArray();
}
/**
* Converts GraphQL query result to spec-compliant serializable array using provided
* errors handler and formatter.
@@ -114,45 +127,40 @@ class ExecutionResult implements \JsonSerializable
* If debug argument is passed, output of error formatter is enriched which debugging information
* ("debugMessage", "trace" keys depending on flags).
*
* $debug argument must be either bool (only adds "debugMessage" to result) or sum of flags from
* GraphQL\Error\Debug
* $debug argument must sum of flags from @see \GraphQL\Error\DebugFlag
*
* @return mixed[]
*
* @api
* @param bool|int $debug
* @return array
*/
public function toArray($debug = false)
public function toArray(int $debug = DebugFlag::NONE) : array
{
$result = [];
if (!empty($this->errors)) {
$errorsHandler = $this->errorsHandler ?: function(array $errors, callable $formatter) {
if (count($this->errors ?? []) > 0) {
$errorsHandler = $this->errorsHandler ?? static function (array $errors, callable $formatter) : array {
return array_map($formatter, $errors);
};
$result['errors'] = $errorsHandler(
$handledErrors = $errorsHandler(
$this->errors,
FormattedError::prepareFormatter($this->errorFormatter, $debug)
);
// While we know that there were errors initially, they might have been discarded
if ($handledErrors !== []) {
$result['errors'] = $handledErrors;
}
}
if (null !== $this->data) {
if ($this->data !== null) {
$result['data'] = $this->data;
}
if (!empty($this->extensions)) {
$result['extensions'] = (array) $this->extensions;
if (count($this->extensions ?? []) > 0) {
$result['extensions'] = $this->extensions;
}
return $result;
}
/**
* Part of \JsonSerializable interface
*
* @return array
*/
public function jsonSerialize()
{
return $this->toArray();
}
}