RequestController.php in GraphQL 8.2
File
src/Controller/RequestController.php
View source
<?php
namespace Drupal\graphql\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\graphql\SchemaLoader;
use Drupal\graphql\SchemaProviderInterface;
use Fubhy\GraphQL\GraphQL;
use Fubhy\GraphQL\Schema;
use Fubhy\GraphQL\Type\Definition\Types\ObjectType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class RequestController implements ContainerInjectionInterface {
protected $graphql;
protected $schemaLoader;
protected $languageManager;
public function __construct(GraphQL $graphql, SchemaLoader $schema_loader, LanguageManagerInterface $language_manager) {
$this->graphql = $graphql;
$this->schemaLoader = $schema_loader;
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('graphql.graphql'), $container
->get('graphql.schema_loader'), $container
->get('language_manager'));
}
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();
}
}