Context.php in GraphQL 8.3
File
modules/graphql_core/src/Plugin/GraphQL/Fields/Context/Context.php
View source
<?php
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Context;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Url;
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GraphQL\Type\Definition\ResolveInfo;
class Context extends FieldPluginBase implements ContainerFactoryPluginInterface {
protected $contextRepository;
protected $subRequestBuffer;
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static($configuration, $pluginId, $pluginDefinition, $container
->get('graphql.buffer.subrequest'), $container
->get('graphql.context_repository'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, SubRequestBuffer $subRequestBuffer, ContextRepositoryInterface $contextRepository) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->contextRepository = $contextRepository;
$this->subRequestBuffer = $subRequestBuffer;
}
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if (!$value instanceof Url) {
$value = Url::fromRoute('<front>');
}
$resolve = $this->subRequestBuffer
->add($value, function () {
$id = $this
->getPluginDefinition()['context_id'];
$contexts = $this->contextRepository
->getRuntimeContexts([
$id,
]);
return isset($contexts[$id]) ? $contexts[$id]
->getContextValue() : NULL;
});
return function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($resolve) {
(yield $resolve());
};
}
}
Classes
Name |
Description |
Context |
Request arbitrary drupal context objects with GraphQL. |