-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathHttpRequestGraphQL.php
78 lines (64 loc) · 1.99 KB
/
HttpRequestGraphQL.php
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
<?php
/**
* Created by PhpStorm.
* User: Tareq
* Date: 5/30/2019
* Time: 3:25 PM
*/
namespace PHPShopify;
use PHPShopify\Exception\SdkException;
class HttpRequestGraphQL extends HttpRequestJson
{
/**
* Prepared GraphQL string to be posted with request
*
* @var string
*/
private static $postDataGraphQL;
/**
* Prepare the data and request headers before making the call
*
* @param array $httpHeaders
* @param mixed $data
* @param array|null $variables
*
* @return void
*
* @throws SdkException if $data is not a string
*/
protected static function prepareRequest($httpHeaders = array(), $data = array(), $variables = null)
{
if (is_string($data)) {
self::$postDataGraphQL = $data;
} else {
throw new SdkException("Only GraphQL string is allowed!");
}
if (!isset($httpHeaders['X-Shopify-Access-Token'])) {
throw new SdkException("The GraphQL Admin API requires an access token for making authenticated requests!");
}
if (is_array($variables)) {
self::$postDataGraphQL = json_encode(['query' => $data, 'variables' => $variables]);
$httpHeaders['Content-type'] = 'application/json';
} else {
$httpHeaders['Content-type'] = 'application/graphql';
}
$httpHeaders['X-Shopify-Access-Token'] = $httpHeaders['X-Shopify-Access-Token'];
self::$httpHeaders = $httpHeaders;
}
/**
* Implement a POST request and return json decoded output
*
* @param string $url
* @param mixed $data
* @param array $httpHeaders
* @param array|null $variables
*
* @return string
*/
public static function post($url, $data, $httpHeaders = array(), $variables = null)
{
self::prepareRequest($httpHeaders, $data, $variables);
self::$postDataJSON = self::$postDataGraphQL;
return self::processRequest('POST', $url);
}
}