@@ -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}
0 commit comments