Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unaliased path to resolve info #1548

Merged
merged 3 commits into from
Jun 10, 2024
Merged
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
33 changes: 28 additions & 5 deletions docs/class-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ Passed as 4th argument to every field resolver. See [docs on field resolving (da

@phpstan-import-type QueryPlanOptions from QueryPlan

@phpstan-type Path array<int, string|int>
@phpstan-type Path list<string|int>

### GraphQL\Type\Definition\ResolveInfo Props

Expand Down Expand Up @@ -351,16 +351,27 @@ 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
*
* @var array<int, string|int>
* @var list<string|int>
*
* @phpstan-var Path
*/
public $path;

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

/**
* Instance of a schema used for execution.
*
Expand Down Expand Up @@ -1730,15 +1741,27 @@ 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
* @return list<int|string>|null
*
* @api
*/
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 list<int|string>|null
*
* @api
*/
function getUnaliasedPath(): ?array
```

## GraphQL\Error\Warning

Encapsulates warnings produced by the library.
Expand Down
45 changes: 37 additions & 8 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
* @var list<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 list<int|string>|null
*/
public ?array $unaliasedPath;

/**
* An array of GraphQL AST Nodes corresponding to this error.
*
Expand All @@ -65,8 +75,9 @@ class Error extends \Exception implements \JsonSerializable, ClientAware, Provid
/**
* @param iterable<array-key, Node|null>|Node|null $nodes
* @param array<int, int>|null $positions
* @param array<int, int|string>|null $path
* @param list<int|string>|null $path
* @param array<string, mixed>|null $extensions
* @param list<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
spawnia marked this conversation as resolved.
Show resolved Hide resolved
) {
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 @@ -114,9 +127,10 @@ public function __construct(
*
* @param mixed $error
* @param iterable<Node>|Node|null $nodes
* @param array<int, int|string>|null $path
* @param list<int|string>|null $path
* @param list<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,9 +267,9 @@ 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
* @return list<int|string>|null
*
* @api
*/
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 list<int|string>|null
*
* @api
*/
public function getUnaliasedPath(): ?array
{
return $this->unaliasedPath;
}

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