Skip to content

Commit 4694583

Browse files
authored
Merge pull request #43 from thecodingmachine/http_code_decider
Adding an option to set HttpCodeDecider
2 parents 44a98e4 + 5a591ba commit 4694583

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

config/graphqlite.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use GraphQL\Error\DebugFlag;
4+
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
45

56
return [
67
/*
@@ -21,4 +22,7 @@
2122
'debug' => DebugFlag::RETHROW_UNSAFE_EXCEPTIONS,
2223
'uri' => env('GRAPHQLITE_URI', '/graphql'),
2324
'middleware' => ['web'],
25+
26+
// Sets the status code in the HTTP request where operations have errors.
27+
'http_code_decider' => HttpCodeDecider::class,
2428
];

src/Controllers/GraphQLiteController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use GraphQL\Server\StandardServer;
1212
use GraphQL\Upload\UploadMiddleware;
1313
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
14+
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
1415
use function array_map;
1516
use function json_decode;
1617
use function json_last_error;
@@ -32,10 +33,13 @@ class GraphQLiteController
3233
private $standardServer;
3334
/** @var bool|int */
3435
private $debug;
36+
/** @var HttpCodeDeciderInterface */
37+
private $httpCodeDecider;
3538

36-
public function __construct(StandardServer $standardServer, HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = DebugFlag::RETHROW_UNSAFE_EXCEPTIONS)
39+
public function __construct(StandardServer $standardServer, HttpCodeDeciderInterface $httpCodeDecider, HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = DebugFlag::RETHROW_UNSAFE_EXCEPTIONS)
3740
{
3841
$this->standardServer = $standardServer;
42+
$this->httpCodeDecider = $httpCodeDecider;
3943
$this->httpMessageFactory = $httpMessageFactory ?: new DiactorosFactory();
4044
$this->debug = $debug === null ? false : $debug;
4145
}
@@ -73,7 +77,7 @@ private function handlePsr7Request(ServerRequestInterface $request): JsonRespons
7377
{
7478
$result = $this->standardServer->executePsrRequest($request);
7579

76-
$httpCodeDecider = new HttpCodeDecider();
80+
$httpCodeDecider = $this->httpCodeDecider;
7781
if ($result instanceof ExecutionResult) {
7882
return new JsonResponse($result->toArray($this->debug), $httpCodeDecider->decideHttpStatusCode($result));
7983
}

src/Providers/GraphQLiteServiceProvider.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Component\Cache\Psr16Cache;
2020
use TheCodingMachine\GraphQLite\Context\Context;
2121
use TheCodingMachine\GraphQLite\Exceptions\WebonyxErrorHandler;
22+
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
23+
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
2224
use TheCodingMachine\GraphQLite\Laravel\Listeners\CachePurger;
2325
use TheCodingMachine\GraphQLite\Laravel\Mappers\Parameters\ValidateFieldMiddleware;
2426
use TheCodingMachine\GraphQLite\Laravel\Mappers\PaginatorTypeMapper;
@@ -83,13 +85,22 @@ public function register()
8385
if (!$this->app->has(ResponseFactoryInterface::class)) {
8486
$this->app->bind(ResponseFactoryInterface::class, ResponseFactory::class);
8587
}
88+
if (!$this->app->has(HttpCodeDeciderInterface::class)) {
89+
$this->app->bind(HttpCodeDeciderInterface::class, HttpCodeDecider::class);
90+
}
8691

8792
$this->app->bind(HttpMessageFactoryInterface::class, PsrHttpFactory::class);
8893

8994
$this->app->singleton(GraphQLiteController::class, function (Application $app) {
9095
$debug = config('graphqlite.debug', DebugFlag::RETHROW_UNSAFE_EXCEPTIONS);
96+
$decider = config('graphqlite.http_code_decider');
97+
if (!$decider) {
98+
$httpCodeDecider = $app[HttpCodeDeciderInterface::class];
99+
} else {
100+
$httpCodeDecider = $app[$decider];
101+
}
91102

92-
return new GraphQLiteController($app[StandardServer::class], $app[HttpMessageFactoryInterface::class], $debug);
103+
return new GraphQLiteController($app[StandardServer::class], $httpCodeDecider, $app[HttpMessageFactoryInterface::class], $debug);
93104
});
94105

95106
$this->app->singleton(StandardServer::class, static function (Application $app) {

tests/Providers/GraphQLiteServiceProviderTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33
namespace TheCodingMachine\GraphQLite\Laravel\Providers;
44

55

6+
use GraphQL\Error\DebugFlag;
7+
use GraphQL\Executor\ExecutionResult;
8+
use GraphQL\Server\StandardServer;
69
use Orchestra\Testbench\TestCase;
10+
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
711
use TheCodingMachine\GraphQLite\Laravel\Listeners\CachePurger;
812
use TheCodingMachine\GraphQLite\Schema;
913
use TheCodingMachine\TDBM\TDBMService;
1014
use function json_decode;
15+
use Illuminate\Http\Request;
16+
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
17+
use TheCodingMachine\GraphQLite\Laravel\Controllers\GraphQLiteController;
18+
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
19+
1120

1221
class GraphQLiteServiceProviderTest extends TestCase
1322
{
@@ -165,4 +174,45 @@ public function testCachePurger(): void
165174
$cachePurger->handle();
166175
$this->assertTrue(true);
167176
}
177+
178+
/**
179+
* Asserts that the status code has been taken from the HttpCodeDeciderInterface.
180+
*/
181+
public function testChangeTheCodeDecider()
182+
{
183+
$this->app->instance(HttpCodeDeciderInterface::class, $this->newCodeDecider(418));
184+
185+
$controller = $this->newGraphQLiteController();
186+
187+
$response = $controller->index($this->newRequest());
188+
189+
$this->assertEquals(418, $response->getStatusCode());
190+
}
191+
192+
private function newCodeDecider(int $statusCode): HttpCodeDeciderInterface
193+
{
194+
return new class implements HttpCodeDeciderInterface {
195+
public function decideHttpStatusCode(ExecutionResult $result): int
196+
{
197+
return 418;
198+
}
199+
};
200+
}
201+
202+
private function newGraphQLiteController(): GraphQLiteController
203+
{
204+
$server = $this->app->make(StandardServer::class);
205+
$httpCodeDecider = $this->app->make(HttpCodeDeciderInterface::class);
206+
$messageFactory = $this->app->make(PsrHttpFactory::class);
207+
return new GraphQLiteController($server, $httpCodeDecider, $messageFactory, DebugFlag::RETHROW_UNSAFE_EXCEPTIONS);
208+
}
209+
210+
private function newRequest(): Request
211+
{
212+
$baseRequest = SymfonyRequest::create('https://localhost', 'GET', [
213+
'query' => '{ testValidatorMultiple(foo:"192.168.1.1") }'
214+
]);
215+
216+
return Request::createFromBase($baseRequest);
217+
}
168218
}

0 commit comments

Comments
 (0)