-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathGraphql.php
More file actions
132 lines (118 loc) · 3.76 KB
/
Graphql.php
File metadata and controls
132 lines (118 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
namespace Shopify\Clients;
use Shopify\Context;
use Shopify\Exception\MissingArgumentException;
class Graphql
{
/** @var Http */
private $client;
/** @var string|null */
protected $token;
/**
* GraphQL Client constructor.
*
* @param string $domain
* @param string|null $token
*
* @throws \Shopify\Exception\MissingArgumentException
*/
public function __construct(
string $domain,
?string $token = null
) {
if (!Context::$IS_CUSTOM_APP && empty($token)) {
throw new MissingArgumentException('Missing access token when creating GraphQL client');
}
$this->client = new Http($domain);
$this->token = $token;
}
/**
* Sends a GraphQL query to this client's domain.
*
* @param string|array $data Query to be posted to endpoint
* @param array $query Parameters on a query to be added to the URL
* @param array $extraHeaders Any extra headers to send along with the request
* @param int|null $tries How many times to attempt the request
*
* @return HttpResponse
* @throws \Shopify\Exception\HttpRequestException
* @throws \Shopify\Exception\MissingArgumentException
*/
public function query(
$data,
array $query = [],
array $extraHeaders = [],
?int $tries = null
): HttpResponse {
if (empty($data)) {
throw new MissingArgumentException('Query missing');
}
list($accessTokenHeader, $accessToken) = $this->getAccessTokenHeader();
$extraHeaders[$accessTokenHeader] = $accessToken;
if (is_array($data)) {
$dataType = Http::DATA_TYPE_JSON;
$data = json_encode($data);
} else {
$dataType = Http::DATA_TYPE_GRAPHQL;
}
return $this->client->post(
$this->getApiPath(),
$data,
$extraHeaders,
$query,
$tries,
$dataType,
);
}
/**
* Proxy string query to this client's domain.
*
* @param string $data Query to be posted to endpoint
* @param array $extraHeaders Any extra headers to send along with the request
* @param int|null $tries How many times to attempt the request
*
* @return \Shopify\Clients\HttpResponse
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \Shopify\Exception\MissingArgumentException
* @throws \Shopify\Exception\UninitializedContextException
*/
public function proxy(
string $data,
array $extraHeaders = [],
?int $tries = null
): HttpResponse {
if (empty($data)) {
throw new MissingArgumentException('Query missing');
}
list($accessTokenHeader, $accessToken) = $this->getAccessTokenHeader();
$extraHeaders[$accessTokenHeader] = $accessToken;
return $this->client->post(
$this->getApiPath(),
$data,
$extraHeaders,
[],
$tries,
Http::DATA_TYPE_JSON,
);
}
/**
* Fetches the URL path to be used for API requests.
*
* @return string
*/
protected function getApiPath(): string
{
return 'admin/api/' . Context::$API_VERSION . '/graphql.json';
}
/**
* Fetches the access token header and value to be used for API requests.
*
* @return array [$accessTokenHeader, $accessToken]
*/
protected function getAccessTokenHeader(): array
{
$accessToken = Context::$IS_CUSTOM_APP ? Context::$API_SECRET_KEY : $this->token;
return [HttpHeaders::X_SHOPIFY_ACCESS_TOKEN, $accessToken];
}
}