You are here

public function RequestController::handleRequest in GraphQL 8

Same name and namespace in other branches
  1. 8.4 src/Controller/RequestController.php \Drupal\graphql\Controller\RequestController::handleRequest()
  2. 8.2 src/Controller/RequestController.php \Drupal\graphql\Controller\RequestController::handleRequest()
  3. 8.3 src/Controller/RequestController.php \Drupal\graphql\Controller\RequestController::handleRequest()

Handles GraphQL requests.

Parameters

\Symfony\Component\HttpFoundation\Request: The request object.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON formatted response.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

1 string reference to 'RequestController::handleRequest'
graphql.routing.yml in ./graphql.routing.yml
graphql.routing.yml

File

src/Controller/RequestController.php, line 81

Class

RequestController
Handles GraphQL requests.

Namespace

Drupal\graphql\Controller

Code

public function handleRequest(Request $request) {
  $body = (array) json_decode($request
    ->getContent()) + [
    'query' => NULL,
    'variables' => NULL,
    'operation' => NULL,
  ];
  $query = $request->query
    ->has('query') ? $request->query
    ->get('query') : $body['query'];
  $variables = $request->query
    ->has('variables') ? $request->query
    ->get('variables') : $body['variables'];
  $operation = $request->query
    ->has('operation') ? $request->query
    ->get('operation') : $body['operation'];
  if (empty($query)) {
    throw new NotFoundHttpException();
  }
  $schema = $this->schemaLoader
    ->loadSchema($this->languageManager
    ->getCurrentLanguage());
  $variables = $variables ? (array) json_decode($variables) : NULL;
  $result = $this->graphql
    ->execute($schema, $query, NULL, $variables, $operation);
  $response = new JsonResponse($result);
  return $response
    ->setPrivate();
}