Skip to content

Commit

Permalink
Add unaliased path to resolve info
Browse files Browse the repository at this point in the history
Fixes #1345
  • Loading branch information
ruudk committed Mar 24, 2024
1 parent 240b2fb commit 6808e9f
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 38 deletions.
37 changes: 33 additions & 4 deletions src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@ class Error extends \Exception implements \JsonSerializable, ClientAware, Provid
/**
* An array describing the JSON-path into the execution response which
* corresponds to this error. Only included for errors during execution.
* When fields are aliased, the path includes aliases.
*
* @var array<int, int|string>|null
*/
public ?array $path;

/**
* An array describing the JSON-path into the execution response which
* corresponds to this error. Only included for errors during execution.
* This will never include aliases.
*
* @var array<int, int|string>|null
*/
public ?array $unaliasedPath;

/**
* An array of GraphQL AST Nodes corresponding to this error.
*
Expand Down Expand Up @@ -67,6 +77,7 @@ class Error extends \Exception implements \JsonSerializable, ClientAware, Provid
* @param array<int, int>|null $positions
* @param array<int, int|string>|null $path
* @param array<string, mixed>|null $extensions
* @param array<int, int|string>|null $unaliasedPath
*/
public function __construct(
string $message = '',
Expand All @@ -75,7 +86,8 @@ public function __construct(
?array $positions = null,
?array $path = null,
?\Throwable $previous = null,
?array $extensions = null
?array $extensions = null,
?array $unaliasedPath = null
) {
parent::__construct($message, 0, $previous);

Expand All @@ -93,6 +105,7 @@ public function __construct(
$this->source = $source;
$this->positions = $positions;
$this->path = $path;
$this->unaliasedPath = $unaliasedPath;

if (\is_array($extensions) && $extensions !== []) {
$this->extensions = $extensions;
Expand All @@ -115,8 +128,9 @@ public function __construct(
* @param mixed $error
* @param iterable<Node>|Node|null $nodes
* @param array<int, int|string>|null $path
* @param array<int, int|string>|null $unaliasedPath
*/
public static function createLocatedError($error, $nodes = null, ?array $path = null): Error
public static function createLocatedError($error, $nodes = null, ?array $path = null, ?array $unaliasedPath = null): Error
{
if ($error instanceof self) {
if ($error->isLocated()) {
Expand All @@ -125,6 +139,7 @@ public static function createLocatedError($error, $nodes = null, ?array $path =

$nodes ??= $error->getNodes();
$path ??= $error->getPath();
$unaliasedPath ??= $error->getUnaliasedPath();
}

$source = null;
Expand Down Expand Up @@ -159,7 +174,8 @@ public static function createLocatedError($error, $nodes = null, ?array $path =
$positions,
$path,
$originalError,
$extensions
$extensions,
$unaliasedPath
);
}

Expand Down Expand Up @@ -251,7 +267,7 @@ public function getNodes(): ?array

/**
* Returns an array describing the path from the root value to the field which produced this error.
* Only included for execution errors.
* Only included for execution errors. When fields are aliased, the path includes aliases.
*
* @return array<int, int|string>|null
*
Expand All @@ -262,6 +278,19 @@ public function getPath(): ?array
return $this->path;
}

/**
* Returns an array describing the path from the root value to the field which produced this error.
* Only included for execution errors. This will never include aliases.
*
* @return array<int, int|string>|null
*
* @api
*/
public function getUnaliasedPath(): ?array
{
return $this->unaliasedPath;
}

/** @return array<string, mixed>|null */
public function getExtensions(): ?array
{
Expand Down
Loading

0 comments on commit 6808e9f

Please sign in to comment.