Skip to content

Commit b00e01c

Browse files
authored
Merge pull request #91 from phpclassic/v1.1
V1.1 - Merge to master
2 parents 2302c3f + 22ade8f commit b00e01c

File tree

8 files changed

+222
-2
lines changed

8 files changed

+222
-2
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,32 @@ $shopify->Blog($blogID)->Article($articleID)->put($updateArtilceInfo);
234234
$blogArticle = $shopify->Blog($blogID)->Article($articleID)->delete();
235235
```
236236

237+
### GraphQL <sup>*v1.1*</sup>
238+
The GraphQL Admin API is a GraphQL-based alternative to the REST-based Admin API, and makes the functionality of the Shopify admin available at a single GraphQL endpoint. The full set of supported types can be found in the [GraphQL Admin API reference](https://help.shopify.com/en/api/graphql-admin-api/reference).
239+
You can simply call the GraphQL resource and make a post request with a GraphQL string:
240+
241+
> The GraphQL Admin API requires an access token for making authenticated requests. So it won't work with `ApiKey` and `Password` which is allowed for REST API resources.
242+
243+
```php
244+
$graphQL = <<<Query
245+
query {
246+
shop {
247+
name
248+
primaryDomain {
249+
url
250+
host
251+
}
252+
}
253+
}
254+
Query;
255+
256+
$data = $shopify->GraphQL->post($graphQL);
257+
```
258+
259+
##### GraphQL Builder
260+
This SDK only accepts a GraphQL string as input. You can build your GraphQL from [Shopify GraphQL Builder](https://help.shopify.com/en/api/graphql-admin-api/graphiql-builder)
261+
262+
237263
### Resource Mapping
238264
Some resources are available directly, some resources are only available through parent resources and a few resources can be accessed both ways. It is recommended that you see the details in the related Shopify API Reference page about each resource. Each resource name here is linked to related Shopify API Reference page.
239265
> Use the resources only by listed resource map. Trying to get a resource directly which is only available through parent resource may end up with errors.
@@ -252,6 +278,7 @@ Some resources are available directly, some resources are only available through
252278
- Comment -> [Event](https://help.shopify.com/api/reference/event/)
253279
- [Country](https://help.shopify.com/api/reference/country/)
254280
- Country -> [Province](https://help.shopify.com/api/reference/province/)
281+
- [Currency](https://help.shopify.com/en/api/reference/store-properties/currency)
255282
- [CustomCollection]()
256283
- CustomCollection -> [Event](https://help.shopify.com/api/reference/event/)
257284
- CustomCollection -> [Metafield](https://help.shopify.com/api/reference/metafield)
@@ -304,6 +331,7 @@ Some resources are available directly, some resources are only available through
304331
- Theme -> [Asset](https://help.shopify.com/api/reference/asset/)
305332
- [User](https://help.shopify.com/api/reference/user) _(read only, Shopify Plus Only)_
306333
- [Webhook](https://help.shopify.com/api/reference/webhook)
334+
- [GraphQL](https://help.shopify.com/en/api/graphql-admin-api/reference)
307335

308336
### Custom Actions
309337
There are several action methods which can be called without calling the `get()`, `post()`, `put()`, `delete()` methods directly, but eventually results in a custom call to one of those methods.

lib/Currency.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Tareq
5+
* Date: 6/10/2019
6+
* Time: 11:22 PM
7+
*
8+
* @see https://help.shopify.com/en/api/reference/store-properties/currency Shopify API Reference for Currency
9+
*/
10+
11+
namespace PHPShopify;
12+
13+
14+
class Currency extends ShopifyResource
15+
{
16+
/**
17+
* @inheritDoc
18+
*/
19+
protected $resourceKey = 'currency';
20+
21+
/**
22+
* @inheritDoc
23+
*/
24+
public $readOnly = true;
25+
26+
/**
27+
* @inheritDoc
28+
*/
29+
public function pluralizeKey()
30+
{
31+
return 'currencies';
32+
}
33+
}

lib/GraphQL.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Tareq
5+
* Date: 5/27/2019
6+
* Time: 12:36 PM
7+
*
8+
* @see https://help.shopify.com/en/api/graphql-admin-api GraphQL Admin API
9+
*/
10+
11+
namespace PHPShopify;
12+
13+
14+
use PHPShopify\Exception\SdkException;
15+
16+
class GraphQL extends ShopifyResource
17+
{
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
protected function getResourcePath()
23+
{
24+
return 'graphql';
25+
}
26+
27+
/**
28+
* Call POST method for any GraphQL request
29+
*
30+
* @param string $graphQL A valid GraphQL String. @see https://help.shopify.com/en/api/graphql-admin-api/graphiql-builder GraphiQL builder - you can build your graphql string from here.
31+
* @param string $url
32+
* @param bool $wrapData
33+
*
34+
* @uses HttpRequestGraphQL::post() to send the HTTP request
35+
*
36+
* @return array
37+
*/
38+
public function post($graphQL, $url = null, $wrapData = false)
39+
{
40+
if (!$url) $url = $this->generateUrl();
41+
42+
$response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders);
43+
44+
return $this->processResponse($response);
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function get($urlParams = array(), $url = null, $dataKey = null)
51+
{
52+
throw new SdkException("Only POST method is allowed for GraphQL!");
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
public function put($dataArray, $url = null, $wrapData = true)
59+
{
60+
throw new SdkException("Only POST method is allowed for GraphQL!");
61+
}
62+
63+
/**
64+
* @inheritdoc
65+
*/
66+
public function delete($urlParams = array(), $url = null)
67+
{
68+
throw new SdkException("Only POST method is allowed for GraphQL!");
69+
}
70+
}

lib/HttpRequestGraphQL.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Tareq
5+
* Date: 5/30/2019
6+
* Time: 3:25 PM
7+
*/
8+
9+
namespace PHPShopify;
10+
11+
12+
use PHPShopify\Exception\SdkException;
13+
14+
class HttpRequestGraphQL extends HttpRequestJson
15+
{
16+
/**
17+
* Prepared GraphQL string to be posted with request
18+
*
19+
* @var string
20+
*/
21+
private static $postDataGraphQL;
22+
23+
/**
24+
* Prepare the data and request headers before making the call
25+
*
26+
* @param mixed $data
27+
* @param array $httpHeaders
28+
*
29+
* @return void
30+
*
31+
* @throws SdkException if $data is not a string
32+
*/
33+
protected static function prepareRequest($httpHeaders = array(), $data = array())
34+
{
35+
36+
if (is_string($data)) {
37+
self::$postDataGraphQL = $data;
38+
} else {
39+
throw new SdkException("Only GraphQL string is allowed!");
40+
}
41+
42+
if (!isset($httpHeaders['X-Shopify-Access-Token'])) {
43+
throw new SdkException("The GraphQL Admin API requires an access token for making authenticated requests!");
44+
}
45+
46+
self::$httpHeaders = $httpHeaders;
47+
self::$httpHeaders['Content-type'] = 'application/graphql';
48+
49+
}
50+
51+
/**
52+
* Implement a POST request and return json decoded output
53+
*
54+
* @param string $url
55+
* @param mixed $data
56+
* @param array $httpHeaders
57+
*
58+
* @return string
59+
*/
60+
public static function post($url, $data, $httpHeaders = array())
61+
{
62+
self::prepareRequest($httpHeaders, $data);
63+
64+
$response = CurlRequest::post($url, self::$postDataGraphQL, self::$httpHeaders);
65+
66+
return self::processResponse($response);
67+
}
68+
}

lib/HttpRequestJson.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class HttpRequestJson
2525
*
2626
* @var array
2727
*/
28-
private static $httpHeaders;
28+
protected static $httpHeaders;
2929

3030
/**
3131
* Prepared JSON string to be posted with request

lib/ShopifyResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ protected function wrapData($dataArray, $dataKey = null)
446446
*/
447447
protected function castString($array)
448448
{
449-
if (is_string($array)) return $array;
449+
if ( ! is_array($array)) return (string) $array;
450450

451451
$string = '';
452452
$i = 0;

lib/ShopifySDK.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
| //Get variants of a product (using Child resource)
6161
| $products = $shopify->Product($productID)->Variant->get();
6262
|
63+
| //GraphQL
64+
| $data = $shopify->GraphQL->post($graphQL);
65+
|
6366
*/
6467
use PHPShopify\Exception\SdkException;
6568

@@ -97,6 +100,7 @@
97100
* @property-read Theme $Theme
98101
* @property-read User $User
99102
* @property-read Webhook $Webhook
103+
* @property-read GraphQL $GraphQL
100104
*
101105
* @method AbandonedCheckout AbandonedCheckout(integer $id = null)
102106
* @method Blog Blog(integer $id = null)
@@ -131,6 +135,7 @@
131135
* @method Theme Theme(int $id = null)
132136
* @method User User(integer $id = null)
133137
* @method Webhook Webhook(integer $id = null)
138+
* @method GraphQL GraphQL()
134139
*/
135140
class ShopifySDK
136141
{
@@ -194,6 +199,7 @@ class ShopifySDK
194199
'Theme',
195200
'User',
196201
'Webhook',
202+
'GraphQL'
197203
);
198204

199205
/**

tests/CurrencyTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Tareq
5+
* Date: 6/10/2019
6+
* Time: 11:26 PM
7+
*/
8+
9+
namespace PHPShopify;
10+
11+
12+
class CurrencyTest
13+
{
14+
15+
}

0 commit comments

Comments
 (0)