class RequestController in GraphQL 8.3
Same name and namespace in other branches
- 8.4 src/Controller/RequestController.php \Drupal\graphql\Controller\RequestController
- 8 src/Controller/RequestController.php \Drupal\graphql\Controller\RequestController
- 8.2 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 13
Namespace
Drupal\graphql\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RequestController:: |
protected | property | The service configuration parameters. | |
RequestController:: |
protected | property | The query processor. | |
RequestController:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
RequestController:: |
protected | function | ||
RequestController:: |
public | function | Handles graphql requests. | |
RequestController:: |
protected | function | ||
RequestController:: |
public | function | RequestController constructor. |