Skip to content

Commit

Permalink
Refactor controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mcg-web committed Feb 18, 2018
1 parent a79a7e2 commit 1389019
Showing 1 changed file with 65 additions and 16 deletions.
81 changes: 65 additions & 16 deletions Controller/GraphController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class GraphController
private $shouldHandleCORS;

/**
* @var string
* @var bool
*/
private $graphQLBatchingMethod;
private $useApolloBatchingMethod;

public function __construct(
GraphQLRequest\ParserInterface $batchParser,
Expand All @@ -45,19 +45,38 @@ public function __construct(
$this->requestExecutor = $requestExecutor;
$this->requestParser = $requestParser;
$this->shouldHandleCORS = $shouldHandleCORS;
$this->graphQLBatchingMethod = $graphQLBatchingMethod;
$this->useApolloBatchingMethod = 'apollo' === $graphQLBatchingMethod;
}

/**
* @param Request $request
* @param string|null $schemaName
*
* @return JsonResponse|Response
*/
public function endpointAction(Request $request, $schemaName = null)
{
return $this->createResponse($request, $schemaName, false);
}

/**
* @param Request $request
* @param string|null $schemaName
*
* @return JsonResponse|Response
*/
public function batchEndpointAction(Request $request, $schemaName = null)
{
return $this->createResponse($request, $schemaName, true);
}

/**
* @param Request $request
* @param string|null $schemaName
* @param bool $batched
*
* @return JsonResponse|Response
*/
private function createResponse(Request $request, $schemaName, $batched)
{
if ('OPTIONS' === $request->getMethod()) {
Expand All @@ -66,43 +85,73 @@ private function createResponse(Request $request, $schemaName, $batched)
if (!in_array($request->getMethod(), ['POST', 'GET'])) {
return new Response('', 405);
}

if ($batched) {
$payload = $this->processBatchQuery($request, $schemaName);
} else {
$payload = $this->processNormalQuery($request, $schemaName);
}

$payload = $this->processQuery($request, $schemaName, $batched);
$response = new JsonResponse($payload, 200);
}
$this->addCORSHeadersIfNeeded($response, $request);

return $response;
}

private function addCORSHeadersIfNeeded(Response $response, Request $request)
{
if ($this->shouldHandleCORS && $request->headers->has('Origin')) {
$response->headers->set('Access-Control-Allow-Origin', $request->headers->get('Origin'), true);
$response->headers->set('Access-Control-Allow-Credentials', 'true', true);
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization', true);
$response->headers->set('Access-Control-Allow-Methods', 'OPTIONS, GET, POST', true);
$response->headers->set('Access-Control-Max-Age', 3600, true);
}
}

return $response;
/**
* @param Request $request
* @param string|null $schemaName
* @param bool $batched
*
* @return array
*/
private function processQuery(Request $request, $schemaName, $batched)
{
if ($batched) {
$payload = $this->processBatchQuery($request, $schemaName);
} else {
$payload = $this->processNormalQuery($request, $schemaName);
}

return $payload;
}

/**
* @param Request $request
* @param string|null $schemaName
*
* @return array
*/
private function processBatchQuery(Request $request, $schemaName = null)
{
$queries = $this->batchParser->parse($request);
$apolloBatching = 'apollo' === $this->graphQLBatchingMethod;
$payloads = [];

foreach ($queries as $query) {
$payloadResult = $this->requestExecutor->execute(
$schemaName, ['query' => $query['query'], 'variables' => $query['variables']]
);
$payloads[] = $apolloBatching ? $payloadResult->toArray() : ['id' => $query['id'], 'payload' => $payloadResult->toArray()];
$payload = $this->requestExecutor
->execute($schemaName, ['query' => $query['query'], 'variables' => $query['variables']])
->toArray();
if (!$this->useApolloBatchingMethod) {
$payload = ['id' => $query['id'], 'payload' => $payload];
}
$payloads[] = $payload;
}

return $payloads;
}

/**
* @param Request $request
* @param string|null $schemaName
*
* @return array
*/
private function processNormalQuery(Request $request, $schemaName = null)
{
$params = $this->requestParser->parse($request);
Expand Down

0 comments on commit 1389019

Please sign in to comment.