You are here

class RequestController in GraphQL 8.3

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

Handles GraphQL requests.

Hierarchy

Expanded class hierarchy of RequestController

File

src/Controller/RequestController.php, line 13

Namespace

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

  /**
   * The query processor.
   *
   * @var \Drupal\graphql\GraphQL\Execution\QueryProcessor
   */
  protected $processor;

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

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

  /**
   * RequestController constructor.
   *
   * @param \Drupal\graphql\GraphQL\Execution\QueryProcessor $processor
   *   The query processor.
   * @param array $parameters
   *   The service configuration parameters.
   */
  public function __construct(QueryProcessor $processor, array $parameters) {
    $this->processor = $processor;
    $this->parameters = $parameters;
  }

  /**
   * Handles graphql requests.
   *
   * @param string $schema
   *   The name of the schema.
   * @param \GraphQL\Server\OperationParams|\GraphQL\Server\OperationParams[] $operations
   *   The graphql operation(s) to execute.
   *
   * @return \Drupal\Core\Cache\CacheableJsonResponse
   *   The JSON formatted response.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function handleRequest($schema, $operations) {
    if (is_array($operations)) {
      return $this
        ->handleBatch($schema, $operations);
    }
    return $this
      ->handleSingle($schema, $operations);
  }

  /**
   * @param $schema
   * @param $operations
   * @param array $globals
   *
   * @return \Drupal\Core\Cache\CacheableJsonResponse
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  protected function handleSingle($schema, $operations) {
    $result = $this->processor
      ->processQuery($schema, $operations);
    $response = new CacheableJsonResponse($result);
    $response
      ->addCacheableDependency($result);
    return $response;
  }

  /**
   * @param $schema
   * @param $operations
   * @param array $globals
   *
   * @return \Drupal\Core\Cache\CacheableJsonResponse
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  protected function handleBatch($schema, $operations) {
    $result = $this->processor
      ->processQuery($schema, $operations);
    $response = new CacheableJsonResponse($result);

    // In case of a batch request, the result is an array.
    $dependencies = is_array($operations) ? $result : [
      $result,
    ];
    foreach ($dependencies as $dependency) {
      $response
        ->addCacheableDependency($dependency);
    }
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RequestController::$parameters protected property The service configuration parameters.
RequestController::$processor protected property The query processor.
RequestController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
RequestController::handleBatch protected function
RequestController::handleRequest public function Handles graphql requests.
RequestController::handleSingle protected function
RequestController::__construct public function RequestController constructor.