Skip to content

Commit 6731fcd

Browse files
committed
add simplified path to GraphQLErrors
1 parent d7c96c7 commit 6731fcd

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/Errors/GraphQLError.php

+21-10
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,46 @@ public function __construct(string $message = "", array $node = null, array $pat
2727
{
2828
parent::__construct($message);
2929
$this->node = $node;
30-
$this->path = array_filter(
31-
array_reverse($path ?? []),
32-
function ($pathItem) {
33-
return $pathItem !== null;
34-
}
35-
);
30+
$this->path = $path;
3631
$this->customExtensions = $customExtensions;
3732
}
3833

3934
/**
4035
* Returns all given locations of errors in the query.
4136
*
42-
* @return null
37+
* @return array|null
4338
*/
44-
public function getLocations()
39+
public function getLocations(): ?array
4540
{
4641
return $this->node["loc"] ?? null;
4742
}
4843

4944
/**
5045
* Returns all given locations of errors in the query.
5146
*
52-
* @return null
47+
* @return array|null
5348
*/
54-
public function getPath()
49+
public function getPath(): ?array
5550
{
5651
return $this->path;
5752
}
5853

54+
/**
55+
* Returns all given locations of errors in the query.
56+
*
57+
* @return array|null
58+
*/
59+
public function getSimplifiedPath(): ?array
60+
{
61+
$simplifiedPath = [];
62+
$currentPathField = $this->path;
63+
while($currentPathField !== null){
64+
$simplifiedPath[] = $currentPathField[0];
65+
$currentPathField = $currentPathField[2];
66+
}
67+
return array_reverse($simplifiedPath);
68+
}
69+
5970
/**
6071
* Returns the internal code of the error.
6172
*

src/Execution/Executor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,15 @@ private function shouldIncludeNode(ExecutionContext $executionContext, $node): b
242242
* @param $sourceValue
243243
* @param $path
244244
* @param $fields
245-
* @return mixed
245+
* @return array
246246
* @throws GraphQLError
247247
*/
248248
public function executeFields(ExecutionContext $executionContext, GraphQLObjectType $parentType, $sourceValue, $path, $fields)
249249
{
250250
$initial = [];
251251
return array_reduce(array_keys($fields), function ($results, $responseName) use ($executionContext, $parentType, $sourceValue, $path, $fields) {
252252
$fieldNodes = $fields[$responseName];
253-
$fieldPath = [$path, $responseName, $parentType->getName()];
253+
$fieldPath = [$responseName, $parentType->getName(), $path];
254254

255255
$result = $this->resolveField(
256256
$executionContext,
@@ -446,7 +446,7 @@ private function completeListValue(ExecutionContext $executionContext, GraphQLLi
446446

447447
$itemType = $returnType->getInnerType();
448448
return array_map(function ($item, $index) use ($executionContext, $returnType, $fieldNodes, $info, $path, $result, $itemType) {
449-
$itemPath = [$path, $index, null];
449+
$itemPath = [$index, null, $path];
450450
try {
451451
return $this->completeValue(
452452
$executionContext,

src/Utilities/Errors.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public static function prettyPrintErrors(array $errors): array
2121
return [
2222
"message" => $error->getMessage(),
2323
"locations" => $error->getLocations(),
24-
"path" => $error->getPath(),
25-
"extensions" => $error->getExtensions()
24+
"extensions" => $error->getExtensions(),
25+
"path" => $error->getSimplifiedPath()
2626
];
2727
}, $errors);
2828
}

0 commit comments

Comments
 (0)