Skip to content

Commit dbc5ba3

Browse files
committed
Allow disabling batched requests
1 parent fcb5eda commit dbc5ba3

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ CHANGELOG
44
[Next release](https://github.com/rebing/graphql-laravel/compare/7.1.0...master)
55
--------------
66

7+
### Added
8+
- Allow disabling batched requests [\#738 / mfn](https://github.com/rebing/graphql-laravel/pull/738)
9+
710
2021-04-08, 7.1.0
811
-----------------
912
### Added

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,8 @@ There are tools that help with this and can handle the batching for you, e.g. [A
18201820
> - No limitations on the number of queries/mutations
18211821
> Currently there's no way to limit this.
18221822
1823+
Support for batching can be disabled by setting the config `batching` to `false`.
1824+
18231825
### Scalar types
18241826

18251827
GraphQL comes with built-in scalar types for string, int, boolean, etc. It's possible to create custom scalar types to special purpose fields.

config/config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
// parameter.
5555
'default_schema' => 'default',
5656

57+
// Whether to support GraphQL batching or not.
58+
// See e.g. https://www.apollographql.com/blog/batching-client-graphql-queries-a685f5bcd41b/
59+
// for pro and con
60+
'batching' => true,
61+
5762
// The schemas for query and/or mutation. It expects an array of schemas to provide
5863
// both the 'query' fields and the 'mutation' fields.
5964
//

src/GraphQLController.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,20 @@ public function query(Request $request, string $schema = null): JsonResponse
4040
$schema = config('graphql.default_schema');
4141
}
4242

43+
$headers = config('graphql.headers', []);
44+
$jsonOptions = config('graphql.json_encoding_options', 0);
45+
4346
// check if is batch (check if the array is associative)
4447
$isBatch = !Arr::isAssoc($request->input());
48+
49+
$supportsBatching = config('graphql.batching', true);
50+
51+
if ($isBatch && !$supportsBatching) {
52+
$data = $this->createBatchingNotSupportedResponse($request->input());
53+
54+
return response()->json($data, 200, $headers, $jsonOptions);
55+
}
56+
4557
$inputs = $isBatch ? $request->input() : [$request->input()];
4658

4759
$completedQueries = [];
@@ -53,9 +65,6 @@ public function query(Request $request, string $schema = null): JsonResponse
5365

5466
$data = $isBatch ? $completedQueries : $completedQueries[0];
5567

56-
$headers = config('graphql.headers', []);
57-
$jsonOptions = config('graphql.json_encoding_options', 0);
58-
5968
return response()->json($data, 200, $headers, $jsonOptions);
6069
}
6170

@@ -185,4 +194,32 @@ protected function getRouteParameters(Request $request): array
185194

186195
return $request->route()->parameters;
187196
}
197+
198+
/**
199+
* In case batching is not supported, send an error back for each batch
200+
* (with a hardcoded limit of 100).
201+
*
202+
* The returned format still matches the GraphQL specs
203+
*
204+
* @param array<string,mixed> $input
205+
* @return array<array{errors:array<array{message:string}>}>
206+
*/
207+
protected function createBatchingNotSupportedResponse(array $input): array
208+
{
209+
$count = min(count($input), 100);
210+
211+
$data = [];
212+
213+
for ($i = 0; $i < $count; $i++) {
214+
$data[] = [
215+
'errors' => [
216+
[
217+
'message' => 'Batch request received but batching is not supported',
218+
],
219+
],
220+
];
221+
}
222+
223+
return $data;
224+
}
188225
}

tests/Unit/EndpointTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,48 @@ public function testBatchedQueries(): void
157157
]);
158158
}
159159

160+
public function testBatchedQueriesButBatchingDisabled(): void
161+
{
162+
config(['graphql.batching' => false]);
163+
164+
$response = $this->call('GET', '/graphql', [
165+
[
166+
'query' => $this->queries['examplesWithVariables'],
167+
'variables' => [
168+
'index' => 0,
169+
],
170+
],
171+
[
172+
'query' => $this->queries['examplesWithVariables'],
173+
'variables' => [
174+
'index' => 0,
175+
],
176+
],
177+
]);
178+
179+
self::assertEquals(200, $response->getStatusCode());
180+
181+
$actual = $response->getData(true);
182+
183+
$expected = [
184+
[
185+
'errors' => [
186+
[
187+
'message' => 'Batch request received but batching is not supported',
188+
],
189+
],
190+
],
191+
[
192+
'errors' => [
193+
[
194+
'message' => 'Batch request received but batching is not supported',
195+
],
196+
],
197+
],
198+
];
199+
self::assertEquals($expected, $actual);
200+
}
201+
160202
public function testGetGraphqiQL(): void
161203
{
162204
$response = $this->call('GET', '/graphiql');

0 commit comments

Comments
 (0)