Skip to content

Commit 1389019

Browse files
author
Jeremiah VALERIE
committed
Refactor controller
1 parent a79a7e2 commit 1389019

File tree

1 file changed

+65
-16
lines changed

1 file changed

+65
-16
lines changed

Controller/GraphController.php

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class GraphController
3030
private $shouldHandleCORS;
3131

3232
/**
33-
* @var string
33+
* @var bool
3434
*/
35-
private $graphQLBatchingMethod;
35+
private $useApolloBatchingMethod;
3636

3737
public function __construct(
3838
GraphQLRequest\ParserInterface $batchParser,
@@ -45,19 +45,38 @@ public function __construct(
4545
$this->requestExecutor = $requestExecutor;
4646
$this->requestParser = $requestParser;
4747
$this->shouldHandleCORS = $shouldHandleCORS;
48-
$this->graphQLBatchingMethod = $graphQLBatchingMethod;
48+
$this->useApolloBatchingMethod = 'apollo' === $graphQLBatchingMethod;
4949
}
5050

51+
/**
52+
* @param Request $request
53+
* @param string|null $schemaName
54+
*
55+
* @return JsonResponse|Response
56+
*/
5157
public function endpointAction(Request $request, $schemaName = null)
5258
{
5359
return $this->createResponse($request, $schemaName, false);
5460
}
5561

62+
/**
63+
* @param Request $request
64+
* @param string|null $schemaName
65+
*
66+
* @return JsonResponse|Response
67+
*/
5668
public function batchEndpointAction(Request $request, $schemaName = null)
5769
{
5870
return $this->createResponse($request, $schemaName, true);
5971
}
6072

73+
/**
74+
* @param Request $request
75+
* @param string|null $schemaName
76+
* @param bool $batched
77+
*
78+
* @return JsonResponse|Response
79+
*/
6180
private function createResponse(Request $request, $schemaName, $batched)
6281
{
6382
if ('OPTIONS' === $request->getMethod()) {
@@ -66,43 +85,73 @@ private function createResponse(Request $request, $schemaName, $batched)
6685
if (!in_array($request->getMethod(), ['POST', 'GET'])) {
6786
return new Response('', 405);
6887
}
69-
70-
if ($batched) {
71-
$payload = $this->processBatchQuery($request, $schemaName);
72-
} else {
73-
$payload = $this->processNormalQuery($request, $schemaName);
74-
}
75-
88+
$payload = $this->processQuery($request, $schemaName, $batched);
7689
$response = new JsonResponse($payload, 200);
7790
}
91+
$this->addCORSHeadersIfNeeded($response, $request);
7892

93+
return $response;
94+
}
95+
96+
private function addCORSHeadersIfNeeded(Response $response, Request $request)
97+
{
7998
if ($this->shouldHandleCORS && $request->headers->has('Origin')) {
8099
$response->headers->set('Access-Control-Allow-Origin', $request->headers->get('Origin'), true);
81100
$response->headers->set('Access-Control-Allow-Credentials', 'true', true);
82101
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization', true);
83102
$response->headers->set('Access-Control-Allow-Methods', 'OPTIONS, GET, POST', true);
84103
$response->headers->set('Access-Control-Max-Age', 3600, true);
85104
}
105+
}
86106

87-
return $response;
107+
/**
108+
* @param Request $request
109+
* @param string|null $schemaName
110+
* @param bool $batched
111+
*
112+
* @return array
113+
*/
114+
private function processQuery(Request $request, $schemaName, $batched)
115+
{
116+
if ($batched) {
117+
$payload = $this->processBatchQuery($request, $schemaName);
118+
} else {
119+
$payload = $this->processNormalQuery($request, $schemaName);
120+
}
121+
122+
return $payload;
88123
}
89124

125+
/**
126+
* @param Request $request
127+
* @param string|null $schemaName
128+
*
129+
* @return array
130+
*/
90131
private function processBatchQuery(Request $request, $schemaName = null)
91132
{
92133
$queries = $this->batchParser->parse($request);
93-
$apolloBatching = 'apollo' === $this->graphQLBatchingMethod;
94134
$payloads = [];
95135

96136
foreach ($queries as $query) {
97-
$payloadResult = $this->requestExecutor->execute(
98-
$schemaName, ['query' => $query['query'], 'variables' => $query['variables']]
99-
);
100-
$payloads[] = $apolloBatching ? $payloadResult->toArray() : ['id' => $query['id'], 'payload' => $payloadResult->toArray()];
137+
$payload = $this->requestExecutor
138+
->execute($schemaName, ['query' => $query['query'], 'variables' => $query['variables']])
139+
->toArray();
140+
if (!$this->useApolloBatchingMethod) {
141+
$payload = ['id' => $query['id'], 'payload' => $payload];
142+
}
143+
$payloads[] = $payload;
101144
}
102145

103146
return $payloads;
104147
}
105148

149+
/**
150+
* @param Request $request
151+
* @param string|null $schemaName
152+
*
153+
* @return array
154+
*/
106155
private function processNormalQuery(Request $request, $schemaName = null)
107156
{
108157
$params = $this->requestParser->parse($request);

0 commit comments

Comments
 (0)