Skip to content
This repository was archived by the owner on Mar 8, 2023. It is now read-only.

Commit 6a306e1

Browse files
authored
Refactor error handler injection (#294)
* Refactor error handler injection * Add unit test for error handler
1 parent eaf06eb commit 6a306e1

9 files changed

Lines changed: 140 additions & 112 deletions

File tree

src/Error/ErrorHandler.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55
class ErrorHandler implements ErrorHandlerInterface
66
{
77
/**
8-
* @inheritdoc
8+
* @var callable
9+
*/
10+
protected $handleCallback;
11+
12+
/**
13+
* CallbackErrorHandler constructor.
14+
* @param callable $handleCallback
15+
*/
16+
public function __construct(callable $handleCallback)
17+
{
18+
$this->handleCallback = $handleCallback;
19+
}
20+
21+
/**
22+
* @param ExecutionException $exception
923
*/
1024
public function handleError(ExecutionException $exception)
1125
{
12-
// The default error handler does not need to do anything.
26+
\call_user_func($this->handleCallback, $exception);
1327
}
1428
}

src/Error/ErrorProvider.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Execution/Execution.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@
1212

1313
class Execution implements ExecutionInterface
1414
{
15-
/**
16-
* @var ErrorHandlerInterface
17-
*/
18-
private $errorHandler;
19-
20-
/**
21-
* Execution constructor.
22-
* @param ErrorHandlerInterface $errorHandler
23-
*/
24-
public function __construct(ErrorHandlerInterface $errorHandler)
25-
{
26-
$this->errorHandler = $errorHandler;
27-
}
2815

2916
/**
3017
* @param Schema $schema
@@ -44,7 +31,8 @@ public function execute(
4431
$contextValue = null,
4532
array $variableValues = [],
4633
?string $operationName = null,
47-
?callable $fieldResolver = null
34+
?callable $fieldResolver = null,
35+
?ErrorHandlerInterface $errorHandler = null
4836
): ExecutionResult {
4937
try {
5038
$context = $this->createContext(
@@ -65,7 +53,7 @@ public function execute(
6553
return new ExecutionResult(null, [$error]);
6654
}
6755

68-
$data = $this->createExecutor($context)->execute();
56+
$data = $this->createExecutor($context, $errorHandler)->execute();
6957
$errors = $context->getErrors();
7058

7159
return new ExecutionResult($data, $errors);
@@ -151,11 +139,12 @@ protected function createContext(
151139
}
152140

153141
/**
154-
* @param ExecutionContext $context
142+
* @param ExecutionContext $context
143+
* @param ErrorHandlerInterface|null $errorHandler
155144
* @return Executor
156145
*/
157-
protected function createExecutor(ExecutionContext $context): Executor
146+
protected function createExecutor(ExecutionContext $context, ?ErrorHandlerInterface $errorHandler = null): Executor
158147
{
159-
return new Executor($context, new FieldCollector($context), $this->errorHandler);
148+
return new Executor($context, new FieldCollector($context), $errorHandler);
160149
}
161150
}

src/Execution/ExecutionInterface.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
namespace Digia\GraphQL\Execution;
44

5+
use Digia\GraphQL\Error\ErrorHandlerInterface;
56
use Digia\GraphQL\Language\Node\DocumentNode;
67
use Digia\GraphQL\Schema\Schema;
78

89
interface ExecutionInterface
910
{
1011
/**
11-
* @param Schema $schema
12-
* @param DocumentNode $documentNode
13-
* @param mixed $rootValue
14-
* @param mixed $contextValue
15-
* @param array $variableValues
16-
* @param string|null $operationName
17-
* @param callable|null $fieldResolver
12+
* @param Schema $schema
13+
* @param DocumentNode $documentNode
14+
* @param mixed $rootValue
15+
* @param mixed $contextValue
16+
* @param array $variableValues
17+
* @param string|null $operationName
18+
* @param callable|null $fieldResolver
19+
* @param ErrorHandlerInterface|null $errorHandler
1820
* @return ExecutionResult
1921
*/
2022
public function execute(
@@ -24,6 +26,7 @@ public function execute(
2426
$contextValue = null,
2527
array $variableValues = [],
2628
string $operationName = null,
27-
callable $fieldResolver = null
29+
callable $fieldResolver = null,
30+
?ErrorHandlerInterface $errorHandler = null
2831
): ExecutionResult;
2932
}

src/Execution/ExecutionProvider.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ class ExecutionProvider extends AbstractServiceProvider
2020
*/
2121
public function register()
2222
{
23-
$this->container
24-
->share(ExecutionInterface::class, Execution::class)
25-
->addArgument(ErrorHandlerInterface::class);
26-
23+
$this->container->share(ExecutionInterface::class, Execution::class);
2724
$this->container->share(ValuesHelper::class, ValuesHelper::class);
2825
}
2926
}

src/Execution/Executor.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Executor
6161
protected $finalResult;
6262

6363
/**
64-
* @var ErrorHandlerInterface
64+
* @var ErrorHandlerInterface|null
6565
*/
6666
protected $errorHandler;
6767

@@ -72,14 +72,14 @@ class Executor
7272

7373
/**
7474
* Executor constructor.
75-
* @param ExecutionContext $context
76-
* @param FieldCollector $fieldCollector
77-
* @param ErrorHandlerInterface $errorHandler
75+
* @param ExecutionContext $context
76+
* @param FieldCollector $fieldCollector
77+
* @param ErrorHandlerInterface|null $errorHandler
7878
*/
7979
public function __construct(
8080
ExecutionContext $context,
8181
FieldCollector $fieldCollector,
82-
ErrorHandlerInterface $errorHandler
82+
?ErrorHandlerInterface $errorHandler = null
8383
) {
8484
$this->context = $context;
8585
$this->fieldCollector = $fieldCollector;
@@ -962,7 +962,10 @@ protected function createResolveInfo(
962962
*/
963963
protected function handleError(ExecutionException $error)
964964
{
965-
$this->errorHandler->handleError($error);
965+
if (null !== $this->errorHandler) {
966+
$this->errorHandler->handleError($error);
967+
}
968+
966969
$this->context->addError($error);
967970
}
968971

src/GraphQL.php

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Digia\GraphQL;
44

5-
use Digia\GraphQL\Error\ErrorProvider;
5+
use Digia\GraphQL\Error\ErrorHandler;
6+
use Digia\GraphQL\Error\ErrorHandlerInterface;
67
use Digia\GraphQL\Execution\ExecutionInterface;
78
use Digia\GraphQL\Execution\ExecutionProvider;
89
use Digia\GraphQL\Execution\ExecutionResult;
@@ -61,7 +62,6 @@ class GraphQL
6162
* @var array
6263
*/
6364
private static $providers = [
64-
ErrorProvider::class,
6565
LanguageProvider::class,
6666
SchemaBuildingProvider::class,
6767
SchemaExtensionProvider::class,
@@ -128,14 +128,16 @@ public static function make(string $id)
128128
*/
129129
public static function buildSchema(Source $source, $resolverRegistry, array $options = []): Schema
130130
{
131-
return static::make(SchemaBuilderInterface::class)
132-
->build(
133-
static::parse($source, $options),
134-
$resolverRegistry instanceof ResolverRegistryInterface
135-
? $resolverRegistry
136-
: new ResolverRegistry($resolverRegistry),
137-
$options
138-
);
131+
/** @var SchemaBuilderInterface $schemaBuilder */
132+
$schemaBuilder = static::make(SchemaBuilderInterface::class);
133+
134+
return $schemaBuilder->build(
135+
static::parse($source, $options),
136+
$resolverRegistry instanceof ResolverRegistryInterface
137+
? $resolverRegistry
138+
: new ResolverRegistry($resolverRegistry),
139+
$options
140+
);
139141
}
140142

141143
/**
@@ -151,15 +153,17 @@ public static function extendSchema(
151153
$resolverRegistry,
152154
array $options = []
153155
): Schema {
154-
return static::make(SchemaExtenderInterface::class)
155-
->extend(
156-
$schema,
157-
static::parse($source, $options),
158-
$resolverRegistry instanceof ResolverRegistryInterface
159-
? $resolverRegistry
160-
: new ResolverRegistry($resolverRegistry),
161-
$options
162-
);
156+
/** @var SchemaExtenderInterface $schemaExtender */
157+
$schemaExtender = static::make(SchemaExtenderInterface::class);
158+
159+
return $schemaExtender->extend(
160+
$schema,
161+
static::parse($source, $options),
162+
$resolverRegistry instanceof ResolverRegistryInterface
163+
? $resolverRegistry
164+
: new ResolverRegistry($resolverRegistry),
165+
$options
166+
);
163167
}
164168

165169
/**
@@ -227,13 +231,14 @@ public static function validate(Schema $schema, DocumentNode $document): array
227231
}
228232

229233
/**
230-
* @param Schema $schema
231-
* @param DocumentNode $document
232-
* @param mixed $rootValue
233-
* @param mixed $contextValue
234-
* @param array $variableValues
235-
* @param string|null $operationName
236-
* @param callable|null $fieldResolver
234+
* @param Schema $schema
235+
* @param DocumentNode $document
236+
* @param mixed $rootValue
237+
* @param mixed $contextValue
238+
* @param array $variableValues
239+
* @param string|null $operationName
240+
* @param callable|null $fieldResolver
241+
* @param ErrorHandlerInterface|callable|null $errorHandler
237242
* @return ExecutionResult
238243
*/
239244
public static function execute(
@@ -243,19 +248,27 @@ public static function execute(
243248
$contextValue = null,
244249
array $variableValues = [],
245250
$operationName = null,
246-
callable $fieldResolver = null
251+
callable $fieldResolver = null,
252+
$errorHandler = null
247253
): ExecutionResult {
248254
/** @var ExecutionInterface $execution */
249255
$execution = static::make(ExecutionInterface::class);
250256

257+
if (null !== $errorHandler) {
258+
$errorHandler = $errorHandler instanceof ErrorHandlerInterface
259+
? $errorHandler
260+
: new ErrorHandler($errorHandler);
261+
}
262+
251263
return $execution->execute(
252264
$schema,
253265
$document,
254266
$rootValue,
255267
$contextValue,
256268
$variableValues,
257269
$operationName,
258-
$fieldResolver
270+
$fieldResolver,
271+
$errorHandler
259272
);
260273
}
261274

0 commit comments

Comments
 (0)