|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing\InternalUrl; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
6
|
|
|
use Drupal\Core\Url; |
|
7
|
|
|
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer; |
|
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
11
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
12
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Issue an internal request and retrieve the response object. |
|
16
|
|
|
* |
|
17
|
|
|
* @GraphQLField( |
|
18
|
|
|
* id = "internal_url_request", |
|
19
|
|
|
* secure = true, |
|
20
|
|
|
* name = "request", |
|
21
|
|
|
* type = "InternalResponse", |
|
22
|
|
|
* parents = {"InternalUrl"} |
|
23
|
|
|
* ) |
|
24
|
|
|
*/ |
|
25
|
|
|
class InternalRequest extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The http kernel service. |
|
29
|
|
|
* |
|
30
|
|
|
* @var \Symfony\Component\HttpKernel\HttpKernelInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $httpKernel; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The request stack. |
|
36
|
|
|
* |
|
37
|
|
|
* @var \Symfony\Component\HttpFoundation\RequestStack |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $requestStack; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The subrequest buffer service. |
|
43
|
|
|
* |
|
44
|
|
|
* @var \Drupal\graphql\GraphQL\Buffers\SubRequestBuffer |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $subRequestBuffer; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
52
|
|
|
return new static( |
|
53
|
|
|
$configuration, |
|
54
|
|
|
$pluginId, |
|
55
|
|
|
$pluginDefinition, |
|
56
|
|
|
$container->get('graphql.buffer.subrequest'), |
|
57
|
|
|
$container->get('http_kernel'), |
|
58
|
|
|
$container->get('request_stack') |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* InternalRequest constructor. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $configuration |
|
66
|
|
|
* The plugin configuration array. |
|
67
|
|
|
* @param string $pluginId |
|
68
|
|
|
* The plugin id. |
|
69
|
|
|
* @param mixed $pluginDefinition |
|
70
|
|
|
* The plugin definition array. |
|
71
|
|
|
* @param \Drupal\graphql\GraphQL\Buffers\SubRequestBuffer $subRequestBuffer |
|
72
|
|
|
* The sub-request buffer service. |
|
73
|
|
|
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel |
|
74
|
|
|
* The http kernel. |
|
75
|
|
|
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack |
|
76
|
|
|
* The request stack. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function __construct( |
|
79
|
|
|
array $configuration, |
|
80
|
|
|
$pluginId, |
|
81
|
|
|
$pluginDefinition, |
|
82
|
|
|
SubRequestBuffer $subRequestBuffer, |
|
83
|
|
|
HttpKernelInterface $httpKernel, |
|
84
|
|
|
RequestStack $requestStack |
|
85
|
|
|
) { |
|
86
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
87
|
|
|
$this->subRequestBuffer = $subRequestBuffer; |
|
88
|
|
|
$this->httpKernel = $httpKernel; |
|
89
|
|
|
$this->requestStack = $requestStack; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
|
96
|
|
|
if ($value instanceof Url) { |
|
|
|
|
|
|
97
|
|
|
$resolve = $this->subRequestBuffer->add($value, function () { |
|
98
|
|
|
$request = $this->requestStack->getCurrentRequest()->duplicate(); |
|
99
|
|
|
$request->attributes->set('_controller', $request->get('_graphql_controller')); |
|
100
|
|
|
$request->attributes->remove('_graphql_subrequest'); |
|
101
|
|
|
$request->attributes->remove('_graphql_controller'); |
|
102
|
|
|
|
|
103
|
|
|
$response = $this->httpKernel->handle($request, HttpKernelInterface::SUB_REQUEST); |
|
104
|
|
|
|
|
105
|
|
|
// TODO: |
|
106
|
|
|
// Remove the request stack manipulation once the core issue described at |
|
107
|
|
|
// https://www.drupal.org/node/2613044 is resolved. |
|
108
|
|
|
while ($this->requestStack->getCurrentRequest() === $request) { |
|
109
|
|
|
$this->requestStack->pop(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $response; |
|
113
|
|
|
}); |
|
114
|
|
|
|
|
115
|
|
|
return function ($value, array $args, ResolveInfo $info) use ($resolve) { |
|
|
|
|
|
|
116
|
|
|
yield $resolve(); |
|
117
|
|
|
}; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |