-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathGraphQLController.php
73 lines (58 loc) · 2.23 KB
/
GraphQLController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types = 1);
namespace Rebing\GraphQL;
use GraphQL\Server\OperationParams as BaseOperationParams;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Laragraph\Utils\RequestParser;
use Rebing\GraphQL\Support\OperationParams;
class GraphQLController extends Controller
{
public function query(Request $request, RequestParser $parser, Repository $config, GraphQL $graphql): JsonResponse
{
$schemaName = $request->server('graphql.schemaName');
$operations = $parser->parseRequest($request);
$headers = $config->get('graphql.headers', []);
$jsonOptions = $config->get('graphql.json_encoding_options', 0);
$isBatch = \is_array($operations);
$supportsBatching = $config->get('graphql.batching.enable', true);
if ($isBatch && !$supportsBatching) {
$data = $this->createBatchingNotSupportedResponse($request->input());
return response()->json($data, 200, $headers, $jsonOptions);
}
$data = Helpers::applyEach(
function (BaseOperationParams $baseOperationParams) use ($schemaName, $graphql): array {
$operationParams = new OperationParams($baseOperationParams);
return $graphql->execute($schemaName, $operationParams);
},
$operations
);
return response()->json($data, 200, $headers, $jsonOptions);
}
/**
* In case batching is not supported, send an error back for each batch
* (with a hardcoded limit of 100).
*
* The returned format still matches the GraphQL specs
*
* @param array<string,mixed> $input
* @return array<array{errors:array<array{message:string}>}>
*/
protected function createBatchingNotSupportedResponse(array $input): array
{
$count = min(\count($input), 100);
$data = [];
for ($i = 0; $i < $count; $i++) {
$data[] = [
'errors' => [
[
'message' => 'Batch request received but batching is not supported',
],
],
];
}
return $data;
}
}