1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing\ExternalUrl; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
7
|
|
|
use Drupal\Core\Url; |
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
9
|
|
|
use GuzzleHttp\ClientInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Issue an external request and retrieve the response object. |
15
|
|
|
* |
16
|
|
|
* @GraphQLField( |
17
|
|
|
* id = "external_url_request", |
18
|
|
|
* name = "request", |
19
|
|
|
* type = "ExternalResponse", |
20
|
|
|
* parents = {"ExternalUrl"} |
21
|
|
|
* ) |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
class ExternalRequest extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
24
|
|
|
use DependencySerializationTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \GuzzleHttp\ClientInterface |
28
|
|
|
*/ |
29
|
|
|
protected $httpClient; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public static function create( |
35
|
|
|
ContainerInterface $container, |
36
|
|
|
array $configuration, |
37
|
|
|
$plugin_id, |
38
|
|
|
$plugin_definition |
39
|
|
|
) { |
40
|
|
|
return new static( |
41
|
|
|
$configuration, |
42
|
|
|
$plugin_id, |
43
|
|
|
$plugin_definition, |
44
|
|
|
$container->get('http_client') |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* ExternalRequest constructor. |
50
|
|
|
* |
51
|
|
|
* @param array $configuration |
52
|
|
|
* The plugin configuration array. |
53
|
|
|
* @param string $pluginId |
54
|
|
|
* The plugin id. |
55
|
|
|
* @param mixed $pluginDefinition |
56
|
|
|
* The plugin definition array. |
57
|
|
|
* @param \GuzzleHttp\ClientInterface $httpClient |
58
|
|
|
* The http client service. |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
array $configuration, |
62
|
|
|
$pluginId, |
63
|
|
|
$pluginDefinition, |
64
|
|
|
ClientInterface $httpClient |
65
|
|
|
) { |
66
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
67
|
|
|
$this->httpClient = $httpClient; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
75
|
|
|
if ($value instanceof Url) { |
|
|
|
|
76
|
|
|
yield $this->httpClient->request('GET', $value->toString()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|