1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Request as GraphQLRequest; |
15
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
|
19
|
|
|
final class GraphController |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var GraphQLRequest\BatchParser |
24
|
|
|
*/ |
25
|
|
|
private $batchParser; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var GraphQLRequest\Executor |
29
|
|
|
*/ |
30
|
|
|
private $requestExecutor; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var GraphQLRequest\Parser |
34
|
|
|
*/ |
35
|
|
|
private $requestParser; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
private $shouldHandleCORS; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $graphQLBatchingMethod; |
46
|
|
|
|
47
|
|
|
public function __construct( |
48
|
|
|
GraphQLRequest\BatchParser $batchParser, |
49
|
|
|
GraphQLRequest\Executor $requestExecutor, |
50
|
|
|
GraphQLRequest\Parser $requestParser, |
51
|
|
|
$shouldHandleCORS, |
52
|
|
|
$graphQLBatchingMethod |
53
|
|
|
) { |
54
|
|
|
$this->batchParser = $batchParser; |
55
|
|
|
$this->requestExecutor = $requestExecutor; |
56
|
|
|
$this->requestParser = $requestParser; |
57
|
|
|
$this->shouldHandleCORS = $shouldHandleCORS; |
58
|
|
|
$this->graphQLBatchingMethod = $graphQLBatchingMethod; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function endpointAction(Request $request, $schemaName = null) |
62
|
|
|
{ |
63
|
|
|
return $this->createResponse($request, $schemaName, false); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function batchEndpointAction(Request $request, $schemaName = null) |
67
|
|
|
{ |
68
|
|
|
return $this->createResponse($request, $schemaName, true); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function createResponse(Request $request, $schemaName, $batched) |
72
|
|
|
{ |
73
|
|
|
if ('OPTIONS' === $request->getMethod()) { |
74
|
|
|
$response = new Response('', 200); |
75
|
|
|
} else { |
76
|
|
|
if (!in_array($request->getMethod(), ['POST', 'GET'])) { |
77
|
|
|
return new Response('', 405); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($batched) { |
81
|
|
|
$payload = $this->processBatchQuery($request, $schemaName); |
82
|
|
|
} else { |
83
|
|
|
$payload = $this->processNormalQuery($request, $schemaName); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$response = new JsonResponse($payload, 200); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($this->shouldHandleCORS && $request->headers->has('Origin')) { |
90
|
|
|
$response->headers->set('Access-Control-Allow-Origin', $request->headers->get('Origin'), true); |
91
|
|
|
$response->headers->set('Access-Control-Allow-Credentials', 'true', true); |
92
|
|
|
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization', true); |
93
|
|
|
$response->headers->set('Access-Control-Allow-Methods', 'OPTIONS, GET, POST', true); |
94
|
|
|
$response->headers->set('Access-Control-Max-Age', 3600, true); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $response; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function processBatchQuery(Request $request, $schemaName = null) |
101
|
|
|
{ |
102
|
|
|
$queries = $this->batchParser->parse($request); |
103
|
|
|
$apolloBatching = 'apollo' === $this->graphQLBatchingMethod; |
104
|
|
|
$payloads = []; |
105
|
|
|
|
106
|
|
|
foreach ($queries as $query) { |
107
|
|
|
$payloadResult = $this->requestExecutor->execute( |
108
|
|
|
[ |
109
|
|
|
'query' => $query['query'], |
110
|
|
|
'variables' => $query['variables'], |
111
|
|
|
], |
112
|
|
|
[], |
113
|
|
|
$schemaName |
114
|
|
|
); |
115
|
|
|
$payloads[] = $apolloBatching ? $payloadResult->toArray() : ['id' => $query['id'], 'payload' => $payloadResult->toArray()]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $payloads; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function processNormalQuery(Request $request, $schemaName = null) |
122
|
|
|
{ |
123
|
|
|
$params = $this->requestParser->parse($request); |
124
|
|
|
|
125
|
|
|
return $this->requestExecutor->execute($params, [], $schemaName)->toArray(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|