Skip to content

Commit aa6fb01

Browse files
committed
Separating GraphQL error formatter callback as service
Writing test for GraphQLExceptionFormatterPass
1 parent 2e1a3f4 commit aa6fb01

File tree

8 files changed

+292
-36
lines changed

8 files changed

+292
-36
lines changed

src/Bridge/Symfony/Bundle/Resources/config/graphql.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@
153153
<argument type="service" id="api_platform.graphql.fields_builder" />
154154
</service>
155155
<!-- Exception formatter -->
156+
<service id="api_platform.graphql.exception_formatter_callback" class="ApiPlatform\Core\GraphQl\Exception\ExceptionFormatterCallback">
157+
<argument type="service" id="api_platform.graphql.exception_formatter_factory" />
158+
</service>
159+
156160
<service id="api_platform.graphql.validation_exception_formatter" class="ApiPlatform\Core\GraphQl\Exception\Formatter\ValidationExceptionFormatter">
157161
<tag name="api_platform.graphql.exception_formatter" />
158162
</service>
@@ -171,7 +175,7 @@
171175
<argument type="service" id="api_platform.graphql.executor" />
172176
<argument type="service" id="api_platform.graphql.action.graphiql" />
173177
<argument type="service" id="api_platform.graphql.action.graphql_playground" />
174-
<argument type="service" id="api_platform.graphql.exception_formatter_factory" />
178+
<argument type="service" id="api_platform.graphql.exception_formatter_callback" />
175179
<argument>%kernel.debug%</argument>
176180
<argument>%api_platform.graphql.graphiql.enabled%</argument>
177181
<argument>%api_platform.graphql.graphql_playground.enabled%</argument>

src/GraphQl/Action/EntrypointAction.php

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313

1414
namespace ApiPlatform\Core\GraphQl\Action;
1515

16-
use ApiPlatform\Core\GraphQl\Exception\ExceptionFormatterFactory;
17-
use ApiPlatform\Core\GraphQl\Exception\ExceptionFormatterInterface;
16+
use ApiPlatform\Core\GraphQl\Exception\ExceptionFormatterCallbackInterface;
1817
use ApiPlatform\Core\GraphQl\ExecutorInterface;
1918
use ApiPlatform\Core\GraphQl\Type\SchemaBuilderInterface;
2019
use GraphQL\Error\Debug;
2120
use GraphQL\Error\Error;
22-
use GraphQL\Error\FormattedError;
2321
use GraphQL\Executor\ExecutionResult;
2422
use Symfony\Component\HttpFoundation\JsonResponse;
2523
use Symfony\Component\HttpFoundation\Request;
@@ -40,9 +38,9 @@ final class EntrypointAction
4038
private $graphiqlEnabled;
4139
private $graphQlPlaygroundEnabled;
4240
private $defaultIde;
43-
private $exceptionFormatterFactory;
41+
private $exceptionFormatterCallback;
4442

45-
public function __construct(SchemaBuilderInterface $schemaBuilder, ExecutorInterface $executor, GraphiQlAction $graphiQlAction, GraphQlPlaygroundAction $graphQlPlaygroundAction, ExceptionFormatterFactory $exceptionFormatterFactory, bool $debug = false, bool $graphiqlEnabled = false, bool $graphQlPlaygroundEnabled = false, $defaultIde = false)
43+
public function __construct(SchemaBuilderInterface $schemaBuilder, ExecutorInterface $executor, GraphiQlAction $graphiQlAction, GraphQlPlaygroundAction $graphQlPlaygroundAction, ExceptionFormatterCallbackInterface $exceptionFormatterCallback, bool $debug = false, bool $graphiqlEnabled = false, bool $graphQlPlaygroundEnabled = false, $defaultIde = false)
4644
{
4745
$this->schemaBuilder = $schemaBuilder;
4846
$this->executor = $executor;
@@ -52,7 +50,7 @@ public function __construct(SchemaBuilderInterface $schemaBuilder, ExecutorInter
5250
$this->graphiqlEnabled = $graphiqlEnabled;
5351
$this->graphQlPlaygroundEnabled = $graphQlPlaygroundEnabled;
5452
$this->defaultIde = $defaultIde;
55-
$this->exceptionFormatterFactory = $exceptionFormatterFactory;
53+
$this->exceptionFormatterCallback = $exceptionFormatterCallback;
5654
}
5755

5856
public function __invoke(Request $request): Response
@@ -79,31 +77,10 @@ public function __invoke(Request $request): Response
7977

8078
try {
8179
$executionResult = $this->executor->executeQuery($this->schemaBuilder->getSchema(), $query, null, null, $variables, $operation)
82-
->setErrorFormatter(function (Error $error) {
83-
$formatters = $this->exceptionFormatterFactory->getExceptionFormatters();
84-
usort($formatters, function ($a, $b) {
85-
/**
86-
* @var ExceptionFormatterInterface
87-
* @var ExceptionFormatterInterface $b
88-
*/
89-
if ($a->getPriority() == $b->getPriority()) {
90-
return 0;
91-
}
92-
93-
return ($a->getPriority() > $b->getPriority()) ? -1 : 1;
94-
});
95-
/** @var ExceptionFormatterInterface $exceptionFormatter */
96-
foreach ($formatters as $exceptionFormatter) {
97-
if (null !== $error->getPrevious() && $exceptionFormatter->supports($error->getPrevious())) {
98-
return $exceptionFormatter->format($error);
99-
}
100-
}
101-
102-
// falling back to default GraphQL error formatter
103-
return FormattedError::createFromException($error);
104-
});
80+
->setErrorFormatter($this->exceptionFormatterCallback);
10581
} catch (\Exception $e) {
106-
$executionResult = new ExecutionResult(null, [new Error($e->getMessage(), null, null, null, null, $e)]);
82+
$executionResult = (new ExecutionResult(null, [new Error($e->getMessage(), null, null, null, null, $e)]))
83+
->setErrorFormatter($this->exceptionFormatterCallback);
10784
}
10885

10986
return new JsonResponse($executionResult->toArray($this->debug));
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\GraphQl\Exception;
15+
16+
use GraphQL\Error\Error;
17+
use GraphQL\Error\FormattedError;
18+
19+
/**
20+
* @expremintal
21+
*
22+
* @author Mahmood Bazdar <[email protected]>
23+
*/
24+
class ExceptionFormatterCallback implements ExceptionFormatterCallbackInterface
25+
{
26+
private $exceptionFormatterFactory;
27+
28+
public function __construct(ExceptionFormatterFactoryInterface $exceptionFormatterFactory)
29+
{
30+
$this->exceptionFormatterFactory = $exceptionFormatterFactory;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function __invoke(Error $error): array
37+
{
38+
$formatters = $this->exceptionFormatterFactory->getExceptionFormatters();
39+
usort($formatters, function (ExceptionFormatterInterface $a, ExceptionFormatterInterface $b) {
40+
if ($a->getPriority() == $b->getPriority()) {
41+
return 0;
42+
}
43+
44+
return ($a->getPriority() > $b->getPriority()) ? -1 : 1;
45+
});
46+
/** @var ExceptionFormatterInterface $exceptionFormatter */
47+
foreach ($formatters as $exceptionFormatter) {
48+
if (null !== $error->getPrevious() && $exceptionFormatter->supports($error->getPrevious())) {
49+
return $exceptionFormatter->format($error);
50+
}
51+
}
52+
53+
// falling back to default GraphQL error formatter
54+
return FormattedError::createFromException($error);
55+
}
56+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\GraphQl\Exception;
15+
16+
use GraphQL\Error\Error;
17+
18+
/**
19+
* @expremintal
20+
*
21+
* @author Mahmood Bazdar <[email protected]>
22+
*/
23+
interface ExceptionFormatterCallbackInterface
24+
{
25+
/**
26+
* Callback function will be used for formatting GraphQL errors.
27+
*/
28+
public function __invoke(Error $error): array;
29+
}

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ public function testDisableGraphQl()
363363
$containerBuilderProphecy->setDefinition('api_platform.graphql.query_resolver_locator', Argument::type(Definition::class))->shouldNotBeCalled();
364364
$containerBuilderProphecy->setDefinition('api_platform.graphql.mutation_resolver_locator', Argument::type(Definition::class))->shouldNotBeCalled();
365365
$containerBuilderProphecy->setDefinition('api_platform.graphql.validation_exception_formatter', Argument::type(Definition::class))->shouldNotBeCalled();
366+
$containerBuilderProphecy->setDefinition('api_platform.graphql.exception_formatter_callback', Argument::type(Definition::class))->shouldNotBeCalled();
366367
$containerBuilderProphecy->setDefinition('api_platform.graphql.exception_formatter_locator', Argument::type(Definition::class))->shouldNotBeCalled();
367368
$containerBuilderProphecy->setDefinition('api_platform.graphql.exception_formatter_factory', Argument::type(Definition::class))->shouldNotBeCalled();
368369
$containerBuilderProphecy->setDefinition('api_platform.graphql.command.export_command', Argument::type(Definition::class))->shouldNotBeCalled();
@@ -1133,6 +1134,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
11331134
'api_platform.graphql.resolver.stage.write',
11341135
'api_platform.graphql.resolver.stage.validate',
11351136
'api_platform.graphql.resolver.resource_field',
1137+
'api_platform.graphql.exception_formatter_callback',
11361138
'api_platform.graphql.validation_exception_formatter',
11371139
'api_platform.graphql.exception_formatter_locator',
11381140
'api_platform.graphql.exception_formatter_factory',
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Bundle\DependencyInjection\Compiler;
15+
16+
use ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler\GraphQlExceptionFormatterPass;
17+
use PHPUnit\Framework\TestCase;
18+
use Prophecy\Argument;
19+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20+
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Definition;
22+
use Symfony\Component\DependencyInjection\Reference;
23+
24+
/**
25+
* @author Mahmood Bazdar <[email protected]>
26+
*/
27+
class GraphQlExceptionFormatterPassTest extends TestCase
28+
{
29+
public function testProcess()
30+
{
31+
$filterPass = new GraphQlExceptionFormatterPass();
32+
33+
$this->assertInstanceOf(CompilerPassInterface::class, $filterPass);
34+
35+
$exceptionFormatterLocatorDefinitionProphecy = $this->prophesize(Definition::class);
36+
$exceptionFormatterLocatorDefinitionProphecy->addArgument(Argument::that(function (array $arg) {
37+
return !isset($arg['foo']) && isset($arg['my_id']) && $arg['my_id'] instanceof Reference;
38+
}))->shouldBeCalled();
39+
40+
$exceptionFormatterFactoryDefinitionProphecy = $this->prophesize(Definition::class);
41+
$exceptionFormatterFactoryDefinitionProphecy->addArgument(['my_id'])->shouldBeCalled();
42+
43+
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
44+
$containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(true)->shouldBeCalled();
45+
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.exception_formatter', true)->willReturn(['foo' => [], 'bar' => [['id' => 'my_id']]])->shouldBeCalled();
46+
$containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_locator')->willReturn($exceptionFormatterLocatorDefinitionProphecy->reveal())->shouldBeCalled();
47+
$containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_factory')->willReturn($exceptionFormatterFactoryDefinitionProphecy->reveal())->shouldBeCalled();
48+
49+
$filterPass->process($containerBuilderProphecy->reveal());
50+
}
51+
52+
public function testIdNotExist()
53+
{
54+
$filterPass = new GraphQlExceptionFormatterPass();
55+
56+
$this->assertInstanceOf(CompilerPassInterface::class, $filterPass);
57+
58+
$exceptionFormatterLocatorDefinitionProphecy = $this->prophesize(Definition::class);
59+
$exceptionFormatterLocatorDefinitionProphecy->addArgument(Argument::that(function (array $arg) {
60+
return !isset($arg['foo']) && isset($arg['bar']) && $arg['bar'] instanceof Reference;
61+
}))->shouldBeCalled();
62+
63+
$exceptionFormatterFactoryDefinitionProphecy = $this->prophesize(Definition::class);
64+
$exceptionFormatterFactoryDefinitionProphecy->addArgument(['bar'])->shouldBeCalled();
65+
66+
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
67+
$containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(true)->shouldBeCalled();
68+
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.exception_formatter', true)->willReturn(['foo' => [], 'bar' => [['hi' => 'hello']]])->shouldBeCalled();
69+
$containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_locator')->willReturn($exceptionFormatterLocatorDefinitionProphecy->reveal())->shouldBeCalled();
70+
$containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_factory')->willReturn($exceptionFormatterFactoryDefinitionProphecy->reveal())->shouldBeCalled();
71+
72+
$filterPass->process($containerBuilderProphecy->reveal());
73+
}
74+
75+
public function testDisabled()
76+
{
77+
$filterPass = new GraphQlExceptionFormatterPass();
78+
79+
$this->assertInstanceOf(CompilerPassInterface::class, $filterPass);
80+
81+
$exceptionFormatterLocatorDefinitionProphecy = $this->prophesize(Definition::class);
82+
$exceptionFormatterLocatorDefinitionProphecy->addArgument(Argument::any())->shouldNotBeCalled();
83+
84+
$exceptionFormatterFactoryDefinitionProphecy = $this->prophesize(Definition::class);
85+
$exceptionFormatterFactoryDefinitionProphecy->addArgument(['my_id'])->shouldNotBeCalled();
86+
87+
$containerBuilderProphecy = $this->prophesize(ContainerBuilder::class);
88+
$containerBuilderProphecy->getParameter('api_platform.graphql.enabled')->willReturn(false)->shouldBeCalled();
89+
$containerBuilderProphecy->findTaggedServiceIds('api_platform.graphql.exception_formatter', true)->willReturn(['foo' => [], 'bar' => [['id' => 'my_id']]])->shouldNotBeCalled();
90+
$containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_locator')->willReturn($exceptionFormatterLocatorDefinitionProphecy->reveal())->shouldNotBeCalled();
91+
$containerBuilderProphecy->getDefinition('api_platform.graphql.exception_formatter_factory')->willReturn($exceptionFormatterFactoryDefinitionProphecy->reveal())->shouldNotBeCalled();
92+
93+
$filterPass->process($containerBuilderProphecy->reveal());
94+
}
95+
}

tests/GraphQl/Action/EntrypointActionTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use ApiPlatform\Core\GraphQl\Action\EntrypointAction;
1717
use ApiPlatform\Core\GraphQl\Action\GraphiQlAction;
1818
use ApiPlatform\Core\GraphQl\Action\GraphQlPlaygroundAction;
19-
use ApiPlatform\Core\GraphQl\Exception\ExceptionFormatterFactory;
19+
use ApiPlatform\Core\GraphQl\Exception\ExceptionFormatterCallbackInterface;
2020
use ApiPlatform\Core\GraphQl\ExecutorInterface;
2121
use ApiPlatform\Core\GraphQl\Type\SchemaBuilderInterface;
2222
use GraphQL\Executor\ExecutionResult;
@@ -119,20 +119,20 @@ private function getEntrypointAction(): EntrypointAction
119119
$schemaBuilderProphecy = $this->prophesize(SchemaBuilderInterface::class);
120120
$schemaBuilderProphecy->getSchema()->willReturn($schema->reveal());
121121

122+
$exceptionFormatterCallback = $this->prophesize(ExceptionFormatterCallbackInterface::class)->reveal();
123+
122124
$executionResultProphecy = $this->prophesize(ExecutionResult::class);
123-
$executionResultProphecy->setErrorFormatter(Argument::type('callable'))->willReturn($executionResultProphecy);
125+
$executionResultProphecy->setErrorFormatter($exceptionFormatterCallback)->willReturn($executionResultProphecy);
124126
$executionResultProphecy->toArray(3)->willReturn(['GraphQL']);
125127
$executorProphecy = $this->prophesize(ExecutorInterface::class);
126128
$executorProphecy->executeQuery(Argument::is($schema->reveal()), 'graphqlQuery', null, null, ['graphqlVariable'], 'graphqlOperationName')->willReturn($executionResultProphecy->reveal());
127129

128130
$twigProphecy = $this->prophesize(TwigEnvironment::class);
129131
$routerProphecy = $this->prophesize(RouterInterface::class);
130132

131-
$exceptionFormatterFactoryProphecy = $this->prophesize(ExceptionFormatterFactory::class);
132-
133133
$graphiQlAction = new GraphiQlAction($twigProphecy->reveal(), $routerProphecy->reveal(), true);
134134
$graphQlPlaygroundAction = new GraphQlPlaygroundAction($twigProphecy->reveal(), $routerProphecy->reveal(), true);
135135

136-
return new EntrypointAction($schemaBuilderProphecy->reveal(), $executorProphecy->reveal(), $graphiQlAction, $graphQlPlaygroundAction, $exceptionFormatterFactoryProphecy->reveal(), true, true, true, 'graphiql');
136+
return new EntrypointAction($schemaBuilderProphecy->reveal(), $executorProphecy->reveal(), $graphiQlAction, $graphQlPlaygroundAction, $exceptionFormatterCallback, true, true, true, 'graphiql');
137137
}
138138
}

0 commit comments

Comments
 (0)