Skip to content

Commit

Permalink
📦 Refactor to avoid using additional kernel exception listener
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-demb authored and cvergne committed Jan 5, 2025
1 parent 03af0fe commit 57abcaa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 57 deletions.
41 changes: 0 additions & 41 deletions EventListener/ExceptionListener.php

This file was deleted.

73 changes: 61 additions & 12 deletions src/Controller/GraphQLiteController.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<?php


namespace TheCodingMachine\GraphQLite\Bundle\Controller;


use GraphQL\Error\Error;
use Laminas\Diactoros\ResponseFactory;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\StreamFactory;
use Laminas\Diactoros\UploadedFileFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Server\ServerConfig;
use GraphQL\Server\StandardServer;
Expand All @@ -24,10 +30,12 @@
use TheCodingMachine\GraphQLite\Bundle\Context\SymfonyGraphQLContext;
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException;

use function array_map;
use function array_map;
use function class_exists;
use function json_decode;
use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException;

/**
* Listens to every single request and forward Graphql requests to Graphql Webonix standardServer.
Expand Down Expand Up @@ -88,19 +96,13 @@ public function handleRequest(Request $request): Response
flags: \JSON_THROW_ON_ERROR
);
} catch (\JsonException $e) {
throw JsonException::create(
reason: $e->getMessage(),
code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE,
previous:$e
);
return $this->invalidJsonBodyResponse($e);
}

if (!is_array($parsedBody)) {
throw JsonException::create(
reason: 'Expecting associative array from request, got ' . gettype($parsedBody),
code: Response::HTTP_UNPROCESSABLE_ENTITY
);
return $this->invalidRequestBodyExpectedAssociativeResponse($parsedBody);
}

$psr7Request = $psr7Request->withParsedBody($parsedBody);
}

Expand Down Expand Up @@ -139,4 +141,51 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym

throw new RuntimeException('Only SyncPromiseAdapter is supported');
}

private function invalidJsonBodyResponse(\JsonException $e): JsonResponse
{
$jsonException = JsonException::create(
reason: $e->getMessage(),
code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE,
previous: $e,
);
$result = new ExecutionResult(
null,
[
new Error(
'Invalid JSON.',
previous: $jsonException,
extensions: $jsonException->getExtensions(),
),
]
);

return new JsonResponse(
$result->toArray($this->debug),
$this->httpCodeDecider->decideHttpStatusCode($result)
);
}

private function invalidRequestBodyExpectedAssociativeResponse(mixed $parsedBody): JsonResponse
{
$jsonException = JsonException::create(
reason: 'Expecting associative array from request, got ' . gettype($parsedBody),
code: Response::HTTP_UNPROCESSABLE_ENTITY,
);
$result = new ExecutionResult(
null,
[
new Error(
'Invalid JSON.',
previous: $jsonException,
extensions: $jsonException->getExtensions(),
),
]
);

return new JsonResponse(
$result->toArray($this->debug),
$this->httpCodeDecider->decideHttpStatusCode($result)
);
}
}
4 changes: 0 additions & 4 deletions src/Resources/config/container/graphqlite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@
<tag name="routing.route_loader"/>
</service>

<service id="TheCodingMachine\GraphQLite\Bundle\EventListener\ExceptionListener">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
</service>

<service id="TheCodingMachine\GraphQLite\Bundle\Mappers\RequestParameterMiddleware">
<tag name="graphql.parameter_middleware"/>
</service>
Expand Down

0 comments on commit 57abcaa

Please sign in to comment.