diff --git a/src/Controller/GraphController.php b/src/Controller/GraphController.php index cf7608487..45f677d60 100644 --- a/src/Controller/GraphController.php +++ b/src/Controller/GraphController.php @@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use function in_array; class GraphController @@ -19,19 +20,22 @@ class GraphController private Parser $requestParser; private bool $shouldHandleCORS; private bool $useApolloBatchingMethod; + private bool $debugMode; public function __construct( BatchParser $batchParser, Executor $requestExecutor, Parser $requestParser, bool $shouldHandleCORS, - string $graphQLBatchingMethod + string $graphQLBatchingMethod, + bool $debugMode = false ) { $this->batchParser = $batchParser; $this->requestExecutor = $requestExecutor; $this->requestParser = $requestParser; $this->shouldHandleCORS = $shouldHandleCORS; $this->useApolloBatchingMethod = 'apollo' === $graphQLBatchingMethod; + $this->debugMode = $debugMode; } /** @@ -61,7 +65,17 @@ private function createResponse(Request $request, ?string $schemaName, bool $bat if (!in_array($request->getMethod(), ['POST', 'GET'])) { return new JsonResponse('', 405); } - $payload = $this->processQuery($request, $schemaName, $batched); + + try { + $payload = $this->processQuery($request, $schemaName, $batched); + } catch(BadRequestHttpException $e) { + if ($this->debugMode) { + throw $e; + } else { + return new JsonResponse($e->getMessage(), 400); + } + } + $response = new JsonResponse($payload, 200); } $this->addCORSHeadersIfNeeded($response, $request); diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 251820a79..50235ed59 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -88,6 +88,7 @@ services: - '@Overblog\GraphQLBundle\Request\Parser' - "%overblog_graphql.handle_cors%" - "%overblog_graphql.batching_method%" + - "%kernel.debug%" Overblog\GraphQLBundle\Definition\ConfigProcessor: arguments: