Skip to content

Commit a70d4f2

Browse files
authored
Fix parsing batched queries (#13)
1 parent 0bb5ffc commit a70d4f2

3 files changed

Lines changed: 48 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
## Unreleased
1111

12+
## v2.0.1
13+
14+
### Fixed
15+
16+
- Fix parsing batched queries
17+
1218
## v2.0.0
1319

1420
### Changed

src/RequestParser.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,27 @@ protected function bodyParams(Request $request): array
5858
return $this->inlineFiles($request);
5959
}
6060

61-
$bodyParams = $request->input();
62-
63-
if (is_array($bodyParams) && Arr::isAssoc($bodyParams)) {
64-
return $bodyParams;
61+
if (Str::startsWith($contentType, 'application/graphql') && ! $request->isJson()) {
62+
return ['query' => $request->getContent()];
6563
}
6664

67-
if (Str::startsWith($contentType, 'application/graphql')) {
68-
return ['query' => $request->getContent()];
65+
$bodyParams = $request->input();
66+
67+
if (is_array($bodyParams)) {
68+
if (Arr::isAssoc($bodyParams)) {
69+
return $bodyParams;
70+
}
71+
if (count($bodyParams) > 0) {
72+
$allAssoc = true;
73+
foreach ($bodyParams as $bodyParam) {
74+
if (! is_array($bodyParam) || ! Arr::isAssoc($bodyParam)) {
75+
$allAssoc = false;
76+
}
77+
}
78+
if ($allAssoc) {
79+
return $bodyParams;
80+
}
81+
}
6982
}
7083

7184
if ($request->isJson()) {

tests/Unit/RequestParserTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ public function formContentTypes(): iterable
108108
yield ['application/x-www-form-urlencoded;bla;blub'];
109109
}
110110

111+
public function testPostWithBatchedRequest(): void
112+
{
113+
$fooQuery = /** @lang GraphQL */ '{ foo }';
114+
$barQuery = /** @lang GraphQL */ '{ bar }';
115+
$request = $this->makeRequest(
116+
'POST',
117+
[],
118+
[],
119+
['Content-Type' => 'application/json'],
120+
\Safe\json_encode([
121+
['query' => $fooQuery],
122+
['query' => $barQuery],
123+
])
124+
);
125+
$params = (new RequestParser())->parseRequest($request);
126+
127+
self::assertIsArray($params);
128+
[$fooParams, $barParams] = $params;
129+
self::assertSame($fooQuery, $fooParams->query);
130+
self::assertSame($barQuery, $barParams->query);
131+
}
132+
111133
public function testPostDefaultsToRegularForm(): void
112134
{
113135
$query = /** @lang GraphQL */ '{ foo }';
@@ -291,7 +313,7 @@ public function testMultipartFormWithoutOperations(): void
291313
* @param array<mixed> $headers
292314
* @param string|resource|null $content
293315
*/
294-
public function makeRequest(string $method, array $parameters = [], array $files = [], array $headers = [], $content = null): Request
316+
private function makeRequest(string $method, array $parameters = [], array $files = [], array $headers = [], $content = null): Request
295317
{
296318
$symfonyRequest = SymfonyRequest::create(
297319
'http://foo.bar/graphql',

0 commit comments

Comments
 (0)