|
1 | 1 | <?php |
2 | 2 |
|
3 | | - |
4 | 3 | namespace TheCodingMachine\GraphQLite\Bundle\Controller; |
5 | 4 |
|
6 | | - |
7 | | -use GraphQL\Executor\ExecutionResult; |
8 | | -use GraphQL\Server\ServerConfig; |
9 | | -use GraphQL\Server\StandardServer; |
10 | | -use GraphQL\Upload\UploadMiddleware; |
| 5 | +use GraphQL\Error\Error; |
11 | 6 | use Laminas\Diactoros\ResponseFactory; |
12 | 7 | use Laminas\Diactoros\ServerRequestFactory; |
13 | 8 | use Laminas\Diactoros\StreamFactory; |
14 | 9 | use Laminas\Diactoros\UploadedFileFactory; |
| 10 | +use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
| 11 | +use TheCodingMachine\GraphQLite\Http\HttpCodeDecider; |
| 12 | +use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface; |
| 13 | +use GraphQL\Executor\ExecutionResult; |
| 14 | +use GraphQL\Server\ServerConfig; |
| 15 | +use GraphQL\Server\StandardServer; |
| 16 | +use GraphQL\Upload\UploadMiddleware; |
15 | 17 | use Psr\Http\Message\ServerRequestInterface; |
16 | 18 | use RuntimeException; |
17 | | -use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
18 | 19 | use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; |
19 | 20 | use Symfony\Component\HttpFoundation\JsonResponse; |
20 | 21 | use Symfony\Component\HttpFoundation\Request; |
21 | 22 | use Symfony\Component\HttpFoundation\Response; |
22 | 23 | use Symfony\Component\Routing\Route; |
23 | 24 | use Symfony\Component\Routing\RouteCollection; |
24 | 25 | use TheCodingMachine\GraphQLite\Bundle\Context\SymfonyGraphQLContext; |
25 | | -use TheCodingMachine\GraphQLite\Http\HttpCodeDecider; |
26 | | -use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface; |
| 26 | +use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException; |
| 27 | + |
27 | 28 | use function array_map; |
28 | 29 | use function class_exists; |
29 | 30 | use function json_decode; |
30 | | -use TheCodingMachine\GraphQLite\Bundle\Exceptions\JsonException; |
31 | 31 |
|
32 | 32 | /** |
33 | 33 | * Listens to every single request and forward Graphql requests to Graphql Webonix standardServer. |
@@ -88,19 +88,13 @@ public function handleRequest(Request $request): Response |
88 | 88 | flags: \JSON_THROW_ON_ERROR |
89 | 89 | ); |
90 | 90 | } catch (\JsonException $e) { |
91 | | - throw JsonException::create( |
92 | | - reason: $e->getMessage(), |
93 | | - code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE, |
94 | | - previous:$e |
95 | | - ); |
| 91 | + return $this->invalidJsonBodyResponse($e); |
96 | 92 | } |
97 | 93 |
|
98 | 94 | if (!is_array($parsedBody)) { |
99 | | - throw JsonException::create( |
100 | | - reason: 'Expecting associative array from request, got ' . gettype($parsedBody), |
101 | | - code: Response::HTTP_UNPROCESSABLE_ENTITY |
102 | | - ); |
| 95 | + return $this->invalidRequestBodyExpectedAssociativeResponse($parsedBody); |
103 | 96 | } |
| 97 | + |
104 | 98 | $psr7Request = $psr7Request->withParsedBody($parsedBody); |
105 | 99 | } |
106 | 100 |
|
@@ -139,4 +133,51 @@ private function handlePsr7Request(ServerRequestInterface $request, Request $sym |
139 | 133 |
|
140 | 134 | throw new RuntimeException('Only SyncPromiseAdapter is supported'); |
141 | 135 | } |
| 136 | + |
| 137 | + private function invalidJsonBodyResponse(\JsonException $e): JsonResponse |
| 138 | + { |
| 139 | + $jsonException = JsonException::create( |
| 140 | + reason: $e->getMessage(), |
| 141 | + code: Response::HTTP_UNSUPPORTED_MEDIA_TYPE, |
| 142 | + previous: $e, |
| 143 | + ); |
| 144 | + $result = new ExecutionResult( |
| 145 | + null, |
| 146 | + [ |
| 147 | + new Error( |
| 148 | + 'Invalid JSON.', |
| 149 | + previous: $jsonException, |
| 150 | + extensions: $jsonException->getExtensions(), |
| 151 | + ), |
| 152 | + ] |
| 153 | + ); |
| 154 | + |
| 155 | + return new JsonResponse( |
| 156 | + $result->toArray($this->debug), |
| 157 | + $this->httpCodeDecider->decideHttpStatusCode($result) |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + private function invalidRequestBodyExpectedAssociativeResponse(mixed $parsedBody): JsonResponse |
| 162 | + { |
| 163 | + $jsonException = JsonException::create( |
| 164 | + reason: 'Expecting associative array from request, got ' . gettype($parsedBody), |
| 165 | + code: Response::HTTP_UNPROCESSABLE_ENTITY, |
| 166 | + ); |
| 167 | + $result = new ExecutionResult( |
| 168 | + null, |
| 169 | + [ |
| 170 | + new Error( |
| 171 | + 'Invalid JSON.', |
| 172 | + previous: $jsonException, |
| 173 | + extensions: $jsonException->getExtensions(), |
| 174 | + ), |
| 175 | + ] |
| 176 | + ); |
| 177 | + |
| 178 | + return new JsonResponse( |
| 179 | + $result->toArray($this->debug), |
| 180 | + $this->httpCodeDecider->decideHttpStatusCode($result) |
| 181 | + ); |
| 182 | + } |
142 | 183 | } |
0 commit comments