Skip to content

Commit

Permalink
Merge pull request #598 from simPod/fix-psr-impl
Browse files Browse the repository at this point in the history
Implement PSR-7 RequestInterface support
  • Loading branch information
vladar committed Jun 8, 2020
2 parents 3887d8f + 10e62cf commit ed8fb62
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 1,182 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -16,6 +16,7 @@
"require-dev": {
"amphp/amp": "^2.3",
"doctrine/coding-standard": "^6.0",
"nyholm/psr7": "^1.2",
"phpbench/phpbench": "^0.14",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "0.12.18",
Expand Down
4 changes: 2 additions & 2 deletions docs/executing-queries.md
Expand Up @@ -66,11 +66,11 @@ Server also supports [PSR-7 request/response interfaces](http://www.php-fig.org/
<?php
use GraphQL\Server\StandardServer;
use GraphQL\Executor\ExecutionResult;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

/** @var ServerRequestInterface $psrRequest */
/** @var RequestInterface $psrRequest */
/** @var ResponseInterface $psrResponse */
/** @var StreamInterface $psrBodyStream */
$server = new StandardServer([/* server options, see below */]);
Expand Down
6 changes: 3 additions & 3 deletions docs/reference.md
Expand Up @@ -1717,7 +1717,7 @@ function executeRequest($parsedBody = null)
* @api
*/
function processPsrRequest(
Psr\Http\Message\ServerRequestInterface $request,
Psr\Http\Message\RequestInterface $request,
Psr\Http\Message\ResponseInterface $response,
Psr\Http\Message\StreamInterface $writableBodyStream
)
Expand All @@ -1732,7 +1732,7 @@ function processPsrRequest(
*
* @api
*/
function executePsrRequest(Psr\Http\Message\ServerRequestInterface $request)
function executePsrRequest(Psr\Http\Message\RequestInterface $request)
```

```php
Expand Down Expand Up @@ -2005,7 +2005,7 @@ function sendResponse($result, $exitWhenDone = false)
*
* @api
*/
function parsePsrRequest(Psr\Http\Message\ServerRequestInterface $request)
function parsePsrRequest(Psr\Http\Message\RequestInterface $request)
```

```php
Expand Down
18 changes: 11 additions & 7 deletions src/Server/Helper.php
Expand Up @@ -18,18 +18,20 @@
use GraphQL\Utils\AST;
use GraphQL\Utils\Utils;
use JsonSerializable;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use function file_get_contents;
use function header;
use function html_entity_decode;
use function is_array;
use function is_callable;
use function is_string;
use function json_decode;
use function json_encode;
use function json_last_error;
use function json_last_error_msg;
use function parse_str;
use function sprintf;
use function stripos;

Expand Down Expand Up @@ -521,7 +523,7 @@ static function ($executionResult, $index) : void {
*
* @api
*/
public function parsePsrRequest(ServerRequestInterface $request)
public function parsePsrRequest(RequestInterface $request)
{
if ($request->getMethod() === 'GET') {
$bodyParams = [];
Expand All @@ -533,13 +535,13 @@ public function parsePsrRequest(ServerRequestInterface $request)
}

if (stripos($contentType[0], 'application/graphql') !== false) {
$bodyParams = ['query' => $request->getBody()->getContents()];
$bodyParams = ['query' => (string) $request->getBody()];
} elseif (stripos($contentType[0], 'application/json') !== false) {
$bodyParams = $request->getParsedBody();
$bodyParams = json_decode((string) $request->getBody(), true);

if ($bodyParams === null) {
throw new InvariantViolation(
'PSR-7 request is expected to provide parsed body for "application/json" requests but got null'
'Did not receive valid JSON array in PSR-7 request body with Content-Type "application/json"'
);
}

Expand All @@ -550,18 +552,20 @@ public function parsePsrRequest(ServerRequestInterface $request)
);
}
} else {
$bodyParams = $request->getParsedBody();
parse_str((string) $request->getBody(), $bodyParams);

if (! is_array($bodyParams)) {
throw new RequestError('Unexpected content type: ' . Utils::printSafeJson($contentType[0]));
}
}
}

parse_str(html_entity_decode($request->getUri()->getQuery()), $queryParams);

return $this->parseRequestParams(
$request->getMethod(),
$bodyParams,
$request->getQueryParams()
$queryParams
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Server/StandardServer.php
Expand Up @@ -9,8 +9,8 @@
use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Promise\Promise;
use GraphQL\Utils\Utils;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Throwable;
use function is_array;
Expand Down Expand Up @@ -146,7 +146,7 @@ public function executeRequest($parsedBody = null)
* @api
*/
public function processPsrRequest(
ServerRequestInterface $request,
RequestInterface $request,
ResponseInterface $response,
StreamInterface $writableBodyStream
) {
Expand All @@ -163,7 +163,7 @@ public function processPsrRequest(
*
* @api
*/
public function executePsrRequest(ServerRequestInterface $request)
public function executePsrRequest(RequestInterface $request)
{
$parsedBody = $this->helper->parsePsrRequest($request);

Expand Down

0 comments on commit ed8fb62

Please sign in to comment.