You are here

class RequestController in GraphQL 8

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

Handles GraphQL requests.

Hierarchy

Expanded class hierarchy of RequestController

File

src/Controller/RequestController.php, line 20

Namespace

Drupal\graphql\Controller
View source
class RequestController implements ContainerInjectionInterface {

  /**
   * The GraphQL service.
   *
   * @var \Fubhy\GraphQL\GraphQL
   */
  protected $graphql;

  /**
   * The schema loader service.
   *
   * @var \Drupal\graphql\SchemaLoader
   */
  protected $schemaLoader;

  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Constructs a RequestController object.
   *
   * @param \Fubhy\GraphQL\GraphQL $graphql
   *   The GraphQL service.
   * @param \Drupal\graphql\SchemaLoader $schema_loader
   *   The schema loader service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager service.
   */
  public function __construct(GraphQL $graphql, SchemaLoader $schema_loader, LanguageManagerInterface $language_manager) {
    $this->graphql = $graphql;
    $this->schemaLoader = $schema_loader;
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('graphql.graphql'), $container
      ->get('graphql.schema_loader'), $container
      ->get('language_manager'));
  }

  /**
   * Handles GraphQL requests.
   *
   * @param \Symfony\Component\HttpFoundation\Request
   *   The request object.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   The JSON formatted response.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
   */
  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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RequestController::$graphql protected property The GraphQL service.
RequestController::$languageManager protected property The language manager service.
RequestController::$schemaLoader protected property The schema loader service.
RequestController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
RequestController::handleRequest public function Handles GraphQL requests.
RequestController::__construct public function Constructs a RequestController object.