Skip to content

Commit

Permalink
Merge 7fb017c into d52f09a
Browse files Browse the repository at this point in the history
  • Loading branch information
ollietb committed Jul 1, 2020
2 parents d52f09a + 7fb017c commit 7902612
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/graphql.xml
Expand Up @@ -167,6 +167,7 @@
<argument type="service" id="api_platform.graphql.executor" />
<argument type="service" id="api_platform.graphql.action.graphiql" />
<argument type="service" id="api_platform.graphql.action.graphql_playground" />
<argument type="service" id="api_platform.graphql.error_handler" />
<argument>%kernel.debug%</argument>
<argument>%api_platform.graphql.graphiql.enabled%</argument>
<argument>%api_platform.graphql.graphql_playground.enabled%</argument>
Expand All @@ -187,6 +188,8 @@
<argument>%api_platform.title%</argument>
</service>

<service id="api_platform.graphql.error_handler" class="ApiPlatform\Core\GraphQl\Error\ErrorHandler" public="false" />

<!-- Serializer -->

<service id="api_platform.graphql.normalizer.item" class="ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer" public="false">
Expand Down
8 changes: 6 additions & 2 deletions src/GraphQl/Action/EntrypointAction.php
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\GraphQl\Action;

use ApiPlatform\Core\GraphQl\ExecutorInterface;
use ApiPlatform\Core\GraphQl\Error\ErrorHandlerInterface;
use ApiPlatform\Core\GraphQl\Type\SchemaBuilderInterface;
use GraphQL\Error\Debug;
use GraphQL\Error\DebugFlag;
Expand All @@ -40,8 +41,9 @@ final class EntrypointAction
private $graphiqlEnabled;
private $graphQlPlaygroundEnabled;
private $defaultIde;
private $errorHandler;

public function __construct(SchemaBuilderInterface $schemaBuilder, ExecutorInterface $executor, GraphiQlAction $graphiQlAction, GraphQlPlaygroundAction $graphQlPlaygroundAction, bool $debug = false, bool $graphiqlEnabled = false, bool $graphQlPlaygroundEnabled = false, $defaultIde = false)
public function __construct(SchemaBuilderInterface $schemaBuilder, ExecutorInterface $executor, GraphiQlAction $graphiQlAction, GraphQlPlaygroundAction $graphQlPlaygroundAction, ErrorHandlerInterface $errorHandler, bool $debug = false, bool $graphiqlEnabled = false, bool $graphQlPlaygroundEnabled = false, $defaultIde = false)
{
$this->schemaBuilder = $schemaBuilder;
$this->executor = $executor;
Expand All @@ -55,6 +57,7 @@ public function __construct(SchemaBuilderInterface $schemaBuilder, ExecutorInter
$this->graphiqlEnabled = $graphiqlEnabled;
$this->graphQlPlaygroundEnabled = $graphQlPlaygroundEnabled;
$this->defaultIde = $defaultIde;
$this->errorHandler = $errorHandler;
}

public function __invoke(Request $request): Response
Expand All @@ -75,7 +78,7 @@ public function __invoke(Request $request): Response
throw new BadRequestHttpException('GraphQL query is not valid.');
}

$executionResult = $this->executor->executeQuery($this->schemaBuilder->getSchema(), $query, null, null, $variables, $operation);
$executionResult = $this->executor->executeQuery($this->schemaBuilder->getSchema(), $query, null, null, $variables, $operation)->setErrorsHandler($this->errorHandler);
} catch (BadRequestHttpException $e) {
$exception = new UserError($e->getMessage(), 0, $e);

Expand Down Expand Up @@ -219,4 +222,5 @@ private function buildExceptionResponse(\Exception $e, int $statusCode): JsonRes

return new JsonResponse($executionResult->toArray($this->debug), $statusCode);
}

}
13 changes: 13 additions & 0 deletions src/GraphQl/Error/ErrorHandler.php
@@ -0,0 +1,13 @@
<?php


namespace ApiPlatform\Core\GraphQl\Error;


class ErrorHandler implements ErrorHandlerInterface
{
public function __invoke(array $errors, callable $formatter)
{
return array_map($formatter, $errors);
}
}
10 changes: 10 additions & 0 deletions src/GraphQl/Error/ErrorHandlerInterface.php
@@ -0,0 +1,10 @@
<?php


namespace ApiPlatform\Core\GraphQl\Error;


interface ErrorHandlerInterface
{
public function __invoke(array $errors, callable $formatter);
}
Expand Up @@ -337,6 +337,7 @@ public function testDisableGraphQl()
$containerBuilderProphecy->setDefinition('api_platform.graphql.action.entrypoint', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setDefinition('api_platform.graphql.action.graphiql', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setDefinition('api_platform.graphql.action.graphql_playground', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setDefinition('api_platform.graphql.error_handler', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setDefinition('api_platform.graphql.resolver.factory.collection', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setDefinition('api_platform.graphql.resolver.factory.item_mutation', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setDefinition('api_platform.graphql.resolver.factory.item', Argument::type(Definition::class))->shouldNotBeCalled();
Expand Down Expand Up @@ -1167,6 +1168,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
'api_platform.graphql.action.entrypoint',
'api_platform.graphql.action.graphiql',
'api_platform.graphql.action.graphql_playground',
'api_platform.graphql.error_handler',
'api_platform.graphql.executor',
'api_platform.graphql.type_builder',
'api_platform.graphql.fields_builder',
Expand Down
7 changes: 6 additions & 1 deletion tests/GraphQl/Action/EntrypointActionTest.php
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Core\GraphQl\Action\GraphiQlAction;
use ApiPlatform\Core\GraphQl\Action\GraphQlPlaygroundAction;
use ApiPlatform\Core\GraphQl\ExecutorInterface;
use ApiPlatform\Core\GraphQl\Error\ErrorHandlerInterface;
use ApiPlatform\Core\GraphQl\Type\SchemaBuilderInterface;
use GraphQL\Error\DebugFlag;
use GraphQL\Executor\ExecutionResult;
Expand Down Expand Up @@ -231,15 +232,19 @@ private function getEntrypointAction(array $variables = ['graphqlVariable']): En

$executionResultProphecy = $this->prophesize(ExecutionResult::class);
$executionResultProphecy->toArray(DebugFlag::NONE)->willReturn(['GraphQL']);

$executorProphecy = $this->prophesize(ExecutorInterface::class);
$executorProphecy->executeQuery(Argument::is($schema->reveal()), 'graphqlQuery', null, null, $variables, 'graphqlOperationName')->willReturn($executionResultProphecy->reveal());
$executionResultProphecy->setErrorsHandler(Argument::any())->willReturn($executionResultProphecy->reveal());

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

$graphiQlAction = new GraphiQlAction($twigProphecy->reveal(), $routerProphecy->reveal(), true);
$graphQlPlaygroundAction = new GraphQlPlaygroundAction($twigProphecy->reveal(), $routerProphecy->reveal(), true);

return new EntrypointAction($schemaBuilderProphecy->reveal(), $executorProphecy->reveal(), $graphiQlAction, $graphQlPlaygroundAction, false, true, true, 'graphiql');
$errorHandlerProphecy = $this->prophesize(ErrorHandlerInterface::class);

return new EntrypointAction($schemaBuilderProphecy->reveal(), $executorProphecy->reveal(), $graphiQlAction, $graphQlPlaygroundAction, $errorHandlerProphecy->reveal(), false, true, true, 'graphiql');
}
}

0 comments on commit 7902612

Please sign in to comment.