You are here

class RequestController in GraphQL 8.4

Same name and namespace in other branches
  1. 8 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

The main GraphQL request handler that will forward to the responsible server.

Hierarchy

Expanded class hierarchy of RequestController

File

src/Controller/RequestController.php, line 14

Namespace

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

  /**
   * The service configuration parameters.
   *
   * @var array
   */
  protected $parameters;

  /**
   * {@inheritdoc}
   *
   * @codeCoverageIgnore
   */
  public static function create(ContainerInterface $container) : self {
    return new static($container
      ->getParameter('graphql.config'));
  }

  /**
   * RequestController constructor.
   *
   * @param array $parameters
   *   The service configuration parameters.
   *
   * @codeCoverageIgnore
   */
  public function __construct(array $parameters) {
    $this->parameters = $parameters;
  }

  /**
   * Handles graphql requests.
   *
   * @param \Drupal\graphql\Entity\ServerInterface $graphql_server
   *   The server instance.
   * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $operations
   *   The graphql operation(s) to execute.
   *
   * @return \Drupal\Core\Cache\CacheableJsonResponse
   *   The JSON formatted response.
   *
   * @throws \Exception
   */
  public function handleRequest(ServerInterface $graphql_server, $operations) {
    if (is_array($operations)) {
      return $this
        ->handleBatch($graphql_server, $operations);
    }

    /** @var \GraphQL\Server\OperationParams $operations */
    return $this
      ->handleSingle($graphql_server, $operations);
  }

  /**
   * Execute a single operation and turn that into a cacheable response.
   *
   * @param \Drupal\graphql\Entity\ServerInterface $server
   * @param \GraphQL\Server\OperationParams $operation
   *
   * @return \Drupal\Core\Cache\CacheableJsonResponse
   *
   * @throws \Exception
   */
  protected function handleSingle(ServerInterface $server, OperationParams $operation) {
    $result = $server
      ->executeOperation($operation);
    $response = new CacheableJsonResponse($result);
    $response
      ->addCacheableDependency($result);
    return $response;
  }

  /**
   * Execute multiple operations as batch and turn that into cacheable response.
   *
   * @param \Drupal\graphql\Entity\ServerInterface $server
   * @param \GraphQL\Server\OperationParams[] $operations
   *
   * @return \Drupal\Core\Cache\CacheableJsonResponse
   *
   * @throws \Exception
   */
  protected function handleBatch(ServerInterface $server, array $operations) {
    $result = $server
      ->executeBatch($operations);
    $response = new CacheableJsonResponse($result);

    // In case of a batch request, the result is an array.
    foreach ($result as $dependency) {
      $response
        ->addCacheableDependency($dependency);
    }
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RequestController::$parameters protected property The service configuration parameters.
RequestController::create public static function @codeCoverageIgnore Overrides ContainerInjectionInterface::create
RequestController::handleBatch protected function Execute multiple operations as batch and turn that into cacheable response.
RequestController::handleRequest public function Handles graphql requests.
RequestController::handleSingle protected function Execute a single operation and turn that into a cacheable response.
RequestController::__construct public function RequestController constructor.