1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Context; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
6
|
|
|
use Drupal\Core\Plugin\Context\ContextRepositoryInterface; |
7
|
|
|
use Drupal\Core\Url; |
8
|
|
|
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Request arbitrary drupal context objects with GraphQL. |
15
|
|
|
* |
16
|
|
|
* TODO: Move this to `InternalUrl` (breaking change). |
17
|
|
|
* |
18
|
|
|
* @GraphQLField( |
19
|
|
|
* id = "context", |
20
|
|
|
* secure = true, |
21
|
|
|
* parents = {"Url", "Root"}, |
22
|
|
|
* nullable = true, |
23
|
|
|
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\ContextDeriver" |
24
|
|
|
* ) |
25
|
|
|
*/ |
26
|
|
|
class Context extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The context repository service. |
30
|
|
|
* |
31
|
|
|
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface |
32
|
|
|
*/ |
33
|
|
|
protected $contextRepository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The subrequest buffer service. |
37
|
|
|
* |
38
|
|
|
* @var \Drupal\graphql\GraphQL\Buffers\SubRequestBuffer |
39
|
|
|
*/ |
40
|
|
|
protected $subRequestBuffer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
46
|
|
|
return new static( |
47
|
|
|
$configuration, |
48
|
|
|
$pluginId, |
49
|
|
|
$pluginDefinition, |
50
|
|
|
$container->get('graphql.buffer.subrequest'), |
51
|
|
|
$container->get('graphql.context_repository') |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
array $configuration, |
60
|
|
|
$pluginId, |
61
|
|
|
$pluginDefinition, |
62
|
|
|
SubRequestBuffer $subRequestBuffer, |
63
|
|
|
ContextRepositoryInterface $contextRepository |
64
|
|
|
) { |
65
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
66
|
|
|
$this->contextRepository = $contextRepository; |
67
|
|
|
$this->subRequestBuffer = $subRequestBuffer; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
74
|
|
|
if ($value instanceof Url) { |
|
|
|
|
75
|
|
|
$resolve = $this->subRequestBuffer->add($value, function () { |
76
|
|
|
$id = $this->getPluginDefinition()['context_id']; |
77
|
|
|
$contexts = $this->contextRepository->getRuntimeContexts([$id]); |
78
|
|
|
return isset($contexts[$id]) ? $contexts[$id]->getContextValue() : NULL; |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
return function ($value, array $args, ResolveInfo $info) use ($resolve) { |
|
|
|
|
82
|
|
|
yield $resolve(); |
83
|
|
|
}; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|