InternalRequest.php in GraphQL 8.3
File
modules/graphql_core/src/Plugin/GraphQL/Fields/Routing/InternalUrl/InternalRequest.php
View source
<?php
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing\InternalUrl;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer;
use Drupal\graphql\GraphQL\Cache\CacheableValue;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use GraphQL\Type\Definition\ResolveInfo;
class InternalRequest extends FieldPluginBase implements ContainerFactoryPluginInterface {
protected $httpKernel;
protected $requestStack;
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('http_kernel'), $container
->get('request_stack'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, SubRequestBuffer $subRequestBuffer, HttpKernelInterface $httpKernel, RequestStack $requestStack) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->subRequestBuffer = $subRequestBuffer;
$this->httpKernel = $httpKernel;
$this->requestStack = $requestStack;
}
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof Url) {
$resolve = $this->subRequestBuffer
->add($value, function () {
$request = $this->requestStack
->getCurrentRequest()
->duplicate();
$request->attributes
->set('_controller', $request
->get('_graphql_controller'));
$request->attributes
->remove('_graphql_subrequest');
$request->attributes
->remove('_graphql_controller');
$response = $this->httpKernel
->handle($request, HttpKernelInterface::SUB_REQUEST);
while ($this->requestStack
->getCurrentRequest() === $request) {
$this->requestStack
->pop();
}
return $response;
});
return function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($resolve) {
$response = $resolve();
(yield new CacheableValue($response
->getValue(), [
$response,
]));
};
}
}
}