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
29 changes: 11 additions & 18 deletions src/Controller/GraphQLiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Laminas\Diactoros\StreamFactory;
use Laminas\Diactoros\UploadedFileFactory;
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 @@ -30,24 +32,14 @@
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 \GraphQL\Server\StandardServer.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Listens to every single request and forwards GraphQL requests to Webonyx's \GraphQL\Server\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 +90,12 @@ 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());
assert($dummyResponseWithRequest instanceof DummyResponseWithRequest);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not so strict as to be sure that you will have a DummyResponseWithRequest instance.

I suggest to use if + throw in case of unexpected result

$psr7Request = $dummyResponseWithRequest->getRequest();
}

return $this->handlePsr7Request($psr7Request, $request);
Expand Down
25 changes: 25 additions & 0 deletions src/UploadMiddlewareUtils/DummyResponseWithRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please mark introduced classes with @internal tag to avoid using them from the outside

*/
class DummyResponseWithRequest extends Response
{
private ServerRequestInterface $request;

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

public function getRequest(): ServerRequestInterface
{
return $this->request;
}
}
19 changes: 19 additions & 0 deletions src/UploadMiddlewareUtils/RequestExtractorMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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
*/
class RequestExtractorMiddleware implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface

This comment was marked as resolved.

{
return new DummyResponseWithRequest($request);
}

}
Loading