class RequestController in GraphQL 8
Same name and namespace in other branches
- 8.4 src/Controller/RequestController.php \Drupal\graphql\Controller\RequestController
 - 8.2 src/Controller/RequestController.php \Drupal\graphql\Controller\RequestController
 - 8.3 src/Controller/RequestController.php \Drupal\graphql\Controller\RequestController
 
Handles GraphQL requests.
Hierarchy
- class \Drupal\graphql\Controller\RequestController implements ContainerInjectionInterface
 
Expanded class hierarchy of RequestController
File
- src/
Controller/ RequestController.php, line 20  
Namespace
Drupal\graphql\ControllerView 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
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            RequestController:: | 
                  protected | property | The GraphQL service. | |
| 
            RequestController:: | 
                  protected | property | The language manager service. | |
| 
            RequestController:: | 
                  protected | property | The schema loader service. | |
| 
            RequestController:: | 
                  public static | function | 
            Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: | 
                  |
| 
            RequestController:: | 
                  public | function | Handles GraphQL requests. | |
| 
            RequestController:: | 
                  public | function | Constructs a RequestController object. |