Skip to content

Commit 70a89f8

Browse files
committed
Merge branch '6.x' into port-6.x-changes
# Conflicts: # composer.json # src/Controller/GraphQLiteController.php
2 parents ecc9dbc + a227d09 commit 70a89f8

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"phpstan": "phpstan analyse -c phpstan.neon --no-progress"
5252
},
5353
"suggest": {
54+
"ecodev/graphql-upload": "If you want to support file upload inside GraphQL input types (v7/v8)",
5455
"symfony/security-bundle": "To use #[Logged] or #[Right] attributes"
5556
},
5657
"autoload" : {

src/Controller/GraphQLiteController.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
use Laminas\Diactoros\ServerRequestFactory;
88
use Laminas\Diactoros\StreamFactory;
99
use Laminas\Diactoros\UploadedFileFactory;
10+
use LogicException;
1011
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
12+
use TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils\DummyResponseWithRequest;
13+
use TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils\RequestExtractorMiddleware;
1114
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
1215
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
1316
use GraphQL\Executor\ExecutionResult;
@@ -27,27 +30,18 @@
2730

2831
use function array_map;
2932
use function class_exists;
33+
use function get_debug_type;
3034
use function json_decode;
3135

3236
/**
33-
* 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}.
3438
*/
3539
class GraphQLiteController
3640
{
37-
/**
38-
* @var HttpMessageFactoryInterface
39-
*/
40-
private $httpMessageFactory;
41-
/** @var int */
42-
private $debug;
43-
/**
44-
* @var ServerConfig
45-
*/
46-
private $serverConfig;
47-
/**
48-
* @var HttpCodeDeciderInterface
49-
*/
50-
private $httpCodeDecider;
41+
private HttpMessageFactoryInterface $httpMessageFactory;
42+
private int $debug;
43+
private ServerConfig $serverConfig;
44+
private HttpCodeDeciderInterface $httpCodeDecider;
5145

5246
public function __construct(ServerConfig $serverConfig, ?HttpMessageFactoryInterface $httpMessageFactory = null, ?int $debug = null, ?HttpCodeDeciderInterface $httpCodeDecider = null)
5347
{
@@ -98,11 +92,14 @@ public function handleRequest(Request $request): Response
9892
$psr7Request = $psr7Request->withParsedBody($parsedBody);
9993
}
10094

101-
// Let's parse the request and adapt it for file uploads.
95+
// Let's parse the request and adapt it for file uploads by extracting it from the middleware.
10296
if (class_exists(UploadMiddleware::class)) {
10397
$uploadMiddleware = new UploadMiddleware();
104-
$psr7Request = $uploadMiddleware->processRequest($psr7Request);
105-
\assert($psr7Request instanceof ServerRequestInterface);
98+
$dummyResponseWithRequest = $uploadMiddleware->process($psr7Request, new RequestExtractorMiddleware());
99+
if (! $dummyResponseWithRequest instanceof DummyResponseWithRequest) {
100+
throw new LogicException(DummyResponseWithRequest::class . ' expect, got ' . get_debug_type($dummyResponseWithRequest));
101+
}
102+
$psr7Request = $dummyResponseWithRequest->getRequest();
106103
}
107104

108105
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)