class RequestController in GraphQL 8.4
Same name and namespace in other branches
- 8 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
The main GraphQL request handler that will forward to the responsible server.
Hierarchy
- class \Drupal\graphql\Controller\RequestController implements ContainerInjectionInterface
Expanded class hierarchy of RequestController
File
- src/
Controller/ RequestController.php, line 14
Namespace
Drupal\graphql\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RequestController:: |
protected | property | The service configuration parameters. | |
RequestController:: |
public static | function |
@codeCoverageIgnore Overrides ContainerInjectionInterface:: |
|
RequestController:: |
protected | function | Execute multiple operations as batch and turn that into cacheable response. | |
RequestController:: |
public | function | Handles graphql requests. | |
RequestController:: |
protected | function | Execute a single operation and turn that into a cacheable response. | |
RequestController:: |
public | function | RequestController constructor. |