Skip to content

Adding management of HTTP status code based on exceptions thrown #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/Controllers/GraphQLiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
use GraphQL\Executor\Promise\Promise;
use GraphQL\Server\StandardServer;
use GraphQL\Upload\UploadMiddleware;
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
use function array_map;
use function json_decode;
use function json_last_error;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use function max;


class GraphQLiteController
Expand Down Expand Up @@ -61,24 +64,25 @@ public function index(Request $request): JsonResponse
$uploadMiddleware = new UploadMiddleware();
$psr7Request = $uploadMiddleware->processRequest($psr7Request);

$result = $this->handlePsr7Request($psr7Request);

$response = new JsonResponse($result);

return $response;
return $this->handlePsr7Request($psr7Request);
}

private function handlePsr7Request(ServerRequestInterface $request): array
private function handlePsr7Request(ServerRequestInterface $request): JsonResponse
{
$result = $this->standardServer->executePsrRequest($request);

$httpCodeDecider = new HttpCodeDecider();
if ($result instanceof ExecutionResult) {
return $result->toArray($this->debug);
return new JsonResponse($result->toArray($this->debug), $httpCodeDecider->decideHttpStatusCode($result));
}
if (is_array($result)) {
return array_map(function (ExecutionResult $executionResult) {
return $executionResult->toArray($this->debug);
$finalResult = array_map(function (ExecutionResult $executionResult) {
return new JsonResponse($executionResult->toArray($this->debug));
}, $result);
// Let's return the highest result.
$statuses = array_map([$httpCodeDecider, 'decideHttpStatusCode'], $result);
$status = max($statuses);
return new JsonResponse($finalResult, $status);
}
if ($result instanceof Promise) {
throw new RuntimeException('Only SyncPromiseAdapter is supported');
Expand Down
2 changes: 1 addition & 1 deletion tests/Providers/GraphQLiteServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testHttpQuery()
public function testAuthentication()
{
$response = $this->json('POST', '/graphql', ['query' => '{ testLogged }']);
$this->assertSame(200, $response->getStatusCode(), $response->getContent());
$this->assertSame(401, $response->getStatusCode(), $response->getContent());
$response->assertJson(["errors" => [["message" => "You need to be logged to access this field"]]]);
}
}