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