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 4331fde
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 40 deletions.
27 changes: 25 additions & 2 deletions docs/class-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public $fieldNodes;
public $parentType;

/**
* Path to this field from the very root value.
* Path to this field from the very root value. When fields are aliased, the path includes aliases.
*
* @api
*
Expand All @@ -361,6 +361,17 @@ public $parentType;
*/
public $path;

/**
* Path to this field from the very root value. This will never include aliases.
*
* @api
*
* @var array<int, string|int>
*
* @phpstan-var Path
*/
public $unaliasedPath;

/**
* Instance of a schema used for execution.
*
Expand Down Expand Up @@ -1730,7 +1741,7 @@ function getLocations(): array
```php
/**
* 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 @@ -1739,6 +1750,18 @@ function getLocations(): array
function getPath(): ?array
```

```php
/**
* 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
*/
function getUnaliasedPath(): ?array
```

## GraphQL\Error\Warning

Encapsulates warnings produced by the library.
Expand Down
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

0 comments on commit 4331fde

Please sign in to comment.