diff --git a/CHANGELOG.md b/CHANGELOG.md index c448308..7f12621 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/RequestParser.php b/src/RequestParser.php index 05c1118..4f045c8 100644 --- a/src/RequestParser.php +++ b/src/RequestParser.php @@ -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; @@ -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 + */ + 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. * @@ -81,35 +111,4 @@ protected function inlineFiles(Request $request): array return $operations; } - - /** - * Extracts the body parameters from the request. - * - * @return array - */ - 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)); - } } diff --git a/tests/Unit/RequestParserTest.php b/tests/Unit/RequestParserTest.php index fa8e961..d56ad86 100644 --- a/tests/Unit/RequestParserTest.php +++ b/tests/Unit/RequestParserTest.php @@ -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); }