-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathGraph.php
More file actions
121 lines (104 loc) · 3.23 KB
/
Graph.php
File metadata and controls
121 lines (104 loc) · 3.23 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
<?php
declare(strict_types=1);
namespace Gnikyt\BasicShopifyAPI\Clients;
use Gnikyt\BasicShopifyAPI\Contracts\GraphRequester;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
/**
* GraphQL client.
*/
class Graph extends AbstractClient implements GraphRequester
{
/**
* {@inheritdoc}
*
* @throws \Exception When missing api password is missing for private apps.
* @throws \Exception When missing access key is missing for public apps.
*/
public function request(string $query, array $variables = [], bool $sync = true)
{
/**
* Run the request as sync or async.
*/
$requestFn = function (array $request) use ($sync) {
// Encode the request
$json = json_encode($request);
// Run the request
$fn = $sync ? 'request' : 'requestAsync';
return $this->getClient()->{$fn}(
'POST',
$this->getBaseUri()->withPath('/admin/api/graphql.json'),
['body' => $json]
);
};
// Build the request
$request = ['query' => $query];
if (count($variables) > 0) {
$request['variables'] = $variables;
}
if ($sync === false) {
// Async request
$promise = $requestFn($request);
return $promise->then([$this, 'handleSuccess'], [$this, 'handleFailure']);
}
// Sync request (default)
try {
$response = $requestFn($request);
return $this->handleSuccess($response);
} catch (RequestException $e) {
return $this->handleFailure($e);
}
}
/**
* Handle response from request.
*
* @param ResponseInterface $resp
*
* @return array
*/
public function handleSuccess(ResponseInterface $resp): array
{
// Convert data to response
$body = $this->toResponse($resp->getBody());
// Return Guzzle response and JSON-decoded body
return [
'errors' => $body->hasErrors() ? $body->getErrors() : false,
'response' => $resp,
'status' => $resp->getStatusCode(),
'body' => $body,
'timestamps' => $this->getTimeStore()->get($this->getSession()),
];
}
/**
* Handle failure of response.
*
* @param RequestException $e
*
* @return array
*/
public function handleFailure(RequestException $e): array
{
$resp = $e->getResponse();
$body = null;
$status = null;
if ($resp) {
// Get the body stream
$rawBody = $resp->getBody();
$status = $resp->getStatusCode();
// Build the error object
if ($rawBody !== null) {
// Convert data to response
$body = $this->toResponse($rawBody);
$body = $body->hasErrors() ? $body->getErrors() : null;
}
}
return [
'errors' => true,
'response' => $resp,
'status' => $status,
'body' => $body,
'exception' => $e,
'timestamps' => $this->getTimeStore()->get($this->getSession()),
];
}
}