Skip to content
Open
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"phpstan": "phpstan analyse -c phpstan.neon --no-progress"
},
"suggest": {
"ecodev/graphql-upload": "If you want to support file upload inside GraphQL input types (v7/v8)",
"symfony/security-bundle": "To use #[Logged] or #[Right] attributes"
},
"autoload" : {
Expand Down
33 changes: 15 additions & 18 deletions src/Controller/GraphQLiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\StreamFactory;
use Laminas\Diactoros\UploadedFileFactory;
use LogicException;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils\DummyResponseWithRequest;
use TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils\RequestExtractorMiddleware;
use TheCodingMachine\GraphQLite\Http\HttpCodeDecider;
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
use GraphQL\Executor\ExecutionResult;
Expand All @@ -27,27 +30,18 @@

use function array_map;
use function class_exists;
use function get_debug_type;
use function json_decode;

/**
* Listens to every single request and forward Graphql requests to Graphql Webonix standardServer.
* Listens to every single request and forwards GraphQL requests to Webonyx's {@see \GraphQL\Server\StandardServer}.
*/
class GraphQLiteController
{
/**
* @var HttpMessageFactoryInterface
*/
private $httpMessageFactory;
/** @var int */
private $debug;
/**
* @var ServerConfig
*/
private $serverConfig;
/**
* @var HttpCodeDeciderInterface
*/
private $httpCodeDecider;
private HttpMessageFactoryInterface $httpMessageFactory;
private int $debug;
private ServerConfig $serverConfig;
private HttpCodeDeciderInterface $httpCodeDecider;

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

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

return $this->handlePsr7Request($psr7Request, $request);
Expand Down
27 changes: 27 additions & 0 deletions src/UploadMiddlewareUtils/DummyResponseWithRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils;

use Laminas\Diactoros\Response;
use Psr\Http\Message\ServerRequestInterface;

/**
* Class used to allow extraction of request from PSR-15 middleware
*
* @internal
*/
class DummyResponseWithRequest extends Response
{
private ServerRequestInterface $request;

public function __construct(ServerRequestInterface $request)
{
parent::__construct();
$this->request = $request;
}

public function getRequest(): ServerRequestInterface
{
return $this->request;
}
}
21 changes: 21 additions & 0 deletions src/UploadMiddlewareUtils/RequestExtractorMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace TheCodingMachine\GraphQLite\Bundle\UploadMiddlewareUtils;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
* Middleware to extract the request from the middleware chain
*
* @internal
*/
class RequestExtractorMiddleware implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new DummyResponseWithRequest($request);
}

}
Loading