ExternalRequest.php in GraphQL 8.3
File
modules/graphql_core/src/Plugin/GraphQL/Fields/Routing/ExternalUrl/ExternalRequest.php
View source
<?php
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing\ExternalUrl;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GraphQL\Type\Definition\ResolveInfo;
class ExternalRequest extends FieldPluginBase implements ContainerFactoryPluginInterface {
use DependencySerializationTrait;
protected $httpClient;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('http_client'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, ClientInterface $httpClient) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->httpClient = $httpClient;
}
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof Url) {
(yield $this->httpClient
->request('GET', $value
->toString()));
}
}
}