Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Mar 20, 2023
1 parent c01e90c commit 0bb5ffc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## v2.0.0

### Changed

- Rely on `$request->input()` instead of manually json decoding request body https://github.com/laragraph/utils/pull/12

## v1.6.0
Expand Down
63 changes: 31 additions & 32 deletions src/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Laragraph\Utils;

use GraphQL\Server\Helper;
use GraphQL\Utils\Utils;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -45,6 +44,37 @@ public function parseRequest(Request $request)
return $this->helper->parseRequestParams($method, $bodyParams, $queryParams);
}

/**
* Extracts the body parameters from the request.
*
* @return array<mixed>
*/
protected function bodyParams(Request $request): array
{
$contentType = $request->header('Content-Type');
assert(is_string($contentType), 'Never null, since Symfony defaults to application/x-www-form-urlencoded.');

if (Str::startsWith($contentType, 'multipart/form-data')) {
return $this->inlineFiles($request);
}

$bodyParams = $request->input();

if (is_array($bodyParams) && Arr::isAssoc($bodyParams)) {
return $bodyParams;
}

if (Str::startsWith($contentType, 'application/graphql')) {
return ['query' => $request->getContent()];
}

if ($request->isJson()) {
throw new BadRequestGraphQLException("GraphQL Server expects JSON object or array, but got: {$request->getContent()}.");
}

throw new BadRequestGraphQLException("Could not decode request with content type: \"{$contentType}\".");
}

/**
* Inline file uploads given through a multipart request.
*
Expand Down Expand Up @@ -81,35 +111,4 @@ protected function inlineFiles(Request $request): array

return $operations;
}

/**
* Extracts the body parameters from the request.
*
* @return array<mixed>
*/
protected function bodyParams(Request $request): array
{
$contentType = $request->header('Content-Type');
assert(is_string($contentType), 'Never null, since Symfony defaults to application/x-www-form-urlencoded.');

if (Str::startsWith($contentType, 'multipart/form-data')) {
return $this->inlineFiles($request);
}

$bodyParams = $request->input();

if (is_array($bodyParams) && Arr::isAssoc($bodyParams)) {
return $bodyParams;
}

if (Str::startsWith($contentType, 'application/graphql')) {
return ['query' => $request->getContent()];
}

if ($request->isJson()) {
throw new BadRequestGraphQLException("GraphQL Server expects JSON object or array, but got: {$request->getContent()}.");
}

throw new BadRequestGraphQLException('Unexpected content type: ' . Utils::printSafeJson($contentType));
}
}
2 changes: 1 addition & 1 deletion tests/Unit/RequestParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testNonsensicalContentTypes(string $contentType): void
$parser = new RequestParser();

$this->expectException(BadRequestGraphQLException::class);
$this->expectExceptionMessage('Unexpected content type: "' . $contentType . '"');
$this->expectExceptionMessage("Could not decode request with content type: \"{$contentType}\"");
$parser->parseRequest($request);
}

Expand Down

0 comments on commit 0bb5ffc

Please sign in to comment.