Skip to content

Commit dd181b8

Browse files
committed
👽 Support ecodev/graphql-upload v8
1 parent cb3247a commit dd181b8

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

Controller/GraphQLiteController.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
use Laminas\Diactoros\ServerRequestFactory;
99
use Laminas\Diactoros\StreamFactory;
1010
use Laminas\Diactoros\UploadedFileFactory;
11+
use LogicException;
1112
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
13+
use TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils\DummyResponseWithRequest;
14+
use TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils\RequestExtractorMiddleware;
1215
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
1316
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
14-
use function array_map;
1517
use GraphQL\Executor\ExecutionResult;
1618
use GraphQL\Server\ServerConfig;
1719
use GraphQL\Server\StandardServer;
1820
use GraphQL\Upload\UploadMiddleware;
19-
use function class_exists;
20-
use function json_decode;
2121
use Psr\Http\Message\ServerRequestInterface;
2222
use RuntimeException;
2323
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
@@ -28,25 +28,20 @@
2828
use Symfony\Component\Routing\RouteCollection;
2929
use TheCodingMachine\GraphQLite\Bundle\Context\SymfonyGraphQLContext;
3030

31+
use function array_map;
32+
use function class_exists;
33+
use function get_class;
34+
use function json_decode;
35+
3136
/**
32-
* Listens to every single request and forward Graphql requests to Graphql Webonix standardServer.
37+
* Listens to every single request and forwards GraphQL requests to Webonyx's {@see \GraphQL\Server\StandardServer}.
3338
*/
3439
class GraphQLiteController
3540
{
36-
/**
37-
* @var HttpMessageFactoryInterface
38-
*/
39-
private $httpMessageFactory;
40-
/** @var int */
41-
private $debug;
42-
/**
43-
* @var ServerConfig
44-
*/
45-
private $serverConfig;
46-
/**
47-
* @var HttpCodeDeciderInterface
48-
*/
49-
private $httpCodeDecider;
41+
private HttpMessageFactoryInterface $httpMessageFactory;
42+
private int $debug;
43+
private ServerConfig $serverConfig;
44+
private HttpCodeDeciderInterface $httpCodeDecider;
5045

5146
public function __construct(ServerConfig $serverConfig, ?HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = null, ?HttpCodeDeciderInterface $httpCodeDecider = null)
5247
{
@@ -90,10 +85,14 @@ public function handleRequest(Request $request): Response
9085
$psr7Request = $psr7Request->withParsedBody($parsedBody);
9186
}
9287

93-
// Let's parse the request and adapt it for file uploads.
88+
// Let's parse the request and adapt it for file uploads by extracting it from the middleware.
9489
if (class_exists(UploadMiddleware::class)) {
9590
$uploadMiddleware = new UploadMiddleware();
96-
$psr7Request = $uploadMiddleware->processRequest($psr7Request);
91+
$dummyResponseWithRequest = $uploadMiddleware->process($psr7Request, new RequestExtractorMiddleware());
92+
if (! $dummyResponseWithRequest instanceof DummyResponseWithRequest) {
93+
throw new LogicException(DummyResponseWithRequest::class . ' expect, got ' . get_class($dummyResponseWithRequest));
94+
}
95+
$psr7Request = $dummyResponseWithRequest->getRequest();
9796
}
9897

9998
return $this->handlePsr7Request($psr7Request, $request);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils;
4+
5+
use Laminas\Diactoros\Response;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
8+
/**
9+
* Class used to allow extraction of request from PSR-15 middleware
10+
*
11+
* @internal
12+
*/
13+
class DummyResponseWithRequest extends Response
14+
{
15+
private ServerRequestInterface $request;
16+
17+
public function __construct(ServerRequestInterface $request)
18+
{
19+
parent::__construct();
20+
$this->request = $request;
21+
}
22+
23+
public function getRequest(): ServerRequestInterface
24+
{
25+
return $this->request;
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
use Psr\Http\Server\RequestHandlerInterface;
8+
9+
/**
10+
* Middleware to extract the request from the middleware chain
11+
*
12+
* @internal
13+
*/
14+
class RequestExtractorMiddleware implements RequestHandlerInterface
15+
{
16+
public function handle(ServerRequestInterface $request): ResponseInterface
17+
{
18+
return new DummyResponseWithRequest($request);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)