Skip to content

Commit df49c5e

Browse files
feat: add delegated errors reporter
1 parent 9d8eba8 commit df49c5e

3 files changed

+42
-6
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace XGraphQL\DelegateExecution;
6+
7+
use GraphQL\Error\Error;
8+
9+
interface DelegatedErrorsReporterInterface
10+
{
11+
/**
12+
* @param Error[] $errors
13+
* @return void
14+
*/
15+
public function reportErrors(array $errors): void;
16+
}

src/Execution.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
final class Execution
1010
{
11-
public static function delegate(Schema $schema, ExecutionDelegatorInterface $delegator): void
12-
{
11+
public static function delegate(
12+
Schema $schema,
13+
ExecutionDelegatorInterface $delegator,
14+
DelegatedErrorsReporterInterface $errorsReporter = null,
15+
): void {
1316
foreach (['query', 'mutation', 'subscription'] as $operation) {
1417
$rootType = $schema->getOperationType($operation);
1518

1619
if (null !== $rootType) {
17-
$rootType->resolveFieldFn = new RootFieldsResolver($delegator);
20+
$rootType->resolveFieldFn = new RootFieldsResolver($delegator, $errorsReporter);
1821
}
1922
}
2023
}

src/RootFieldsResolver.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ final class RootFieldsResolver
2525
*/
2626
private \WeakMap $delegatedPromises;
2727

28-
public function __construct(private readonly ExecutionDelegatorInterface $delegator)
29-
{
28+
public function __construct(
29+
private readonly ExecutionDelegatorInterface $delegator,
30+
private readonly ?DelegatedErrorsReporterInterface $delegatedErrorsReporter = null,
31+
) {
3032
$this->delegatedPromises = new \WeakMap();
3133
}
3234

@@ -41,7 +43,22 @@ public function __invoke(mixed $value, array $arguments, mixed $context, Resolve
4143
SelectionSet::addTypename($operation->getSelectionSet());
4244
SelectionSet::addTypenameToFragments($fragments);
4345

44-
$this->delegatedPromises[$info->operation] = $this->delegator->delegate($info->schema, $operation, $fragments, $info->variableValues);
46+
$this->delegatedPromises[$info->operation] = $this
47+
->delegator
48+
->delegate(
49+
$info->schema,
50+
$operation,
51+
$fragments,
52+
$info->variableValues
53+
)->then(
54+
function (ExecutionResult $result): ExecutionResult {
55+
if ([] !== $result->errors) {
56+
$this->delegatedErrorsReporter?->reportErrors($result->errors);
57+
}
58+
59+
return $result;
60+
}
61+
);
4562
}
4663

4764
return $this->resolve($info);

0 commit comments

Comments
 (0)