Skip to content

V1.1 - Merge to master #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,32 @@ $shopify->Blog($blogID)->Article($articleID)->put($updateArtilceInfo);
$blogArticle = $shopify->Blog($blogID)->Article($articleID)->delete();
```

### GraphQL <sup>*v1.1*</sup>
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).
You can simply call the GraphQL resource and make a post request with a GraphQL string:

> 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.

```php
$graphQL = <<<Query
query {
shop {
name
primaryDomain {
url
host
}
}
}
Query;

$data = $shopify->GraphQL->post($graphQL);
```

##### GraphQL Builder
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)


### Resource Mapping
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.
> 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.
Expand All @@ -252,6 +278,7 @@ Some resources are available directly, some resources are only available through
- Comment -> [Event](https://help.shopify.com/api/reference/event/)
- [Country](https://help.shopify.com/api/reference/country/)
- Country -> [Province](https://help.shopify.com/api/reference/province/)
- [Currency](https://help.shopify.com/en/api/reference/store-properties/currency)
- [CustomCollection]()
- CustomCollection -> [Event](https://help.shopify.com/api/reference/event/)
- CustomCollection -> [Metafield](https://help.shopify.com/api/reference/metafield)
Expand Down Expand Up @@ -304,6 +331,7 @@ Some resources are available directly, some resources are only available through
- Theme -> [Asset](https://help.shopify.com/api/reference/asset/)
- [User](https://help.shopify.com/api/reference/user) _(read only, Shopify Plus Only)_
- [Webhook](https://help.shopify.com/api/reference/webhook)
- [GraphQL](https://help.shopify.com/en/api/graphql-admin-api/reference)

### Custom Actions
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.
Expand Down
33 changes: 33 additions & 0 deletions lib/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Created by PhpStorm.
* User: Tareq
* Date: 6/10/2019
* Time: 11:22 PM
*
* @see https://help.shopify.com/en/api/reference/store-properties/currency Shopify API Reference for Currency
*/

namespace PHPShopify;


class Currency extends ShopifyResource
{
/**
* @inheritDoc
*/
protected $resourceKey = 'currency';

/**
* @inheritDoc
*/
public $readOnly = true;

/**
* @inheritDoc
*/
public function pluralizeKey()
{
return 'currencies';
}
}
70 changes: 70 additions & 0 deletions lib/GraphQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Created by PhpStorm.
* User: Tareq
* Date: 5/27/2019
* Time: 12:36 PM
*
* @see https://help.shopify.com/en/api/graphql-admin-api GraphQL Admin API
*/

namespace PHPShopify;


use PHPShopify\Exception\SdkException;

class GraphQL extends ShopifyResource
{

/**
* @inheritdoc
*/
protected function getResourcePath()
{
return 'graphql';
}

/**
* Call POST method for any GraphQL request
*
* @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.
* @param string $url
* @param bool $wrapData
*
* @uses HttpRequestGraphQL::post() to send the HTTP request
*
* @return array
*/
public function post($graphQL, $url = null, $wrapData = false)
{
if (!$url) $url = $this->generateUrl();

$response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders);

return $this->processResponse($response);
}

/**
* @inheritdoc
*/
public function get($urlParams = array(), $url = null, $dataKey = null)
{
throw new SdkException("Only POST method is allowed for GraphQL!");
}

/**
* @inheritdoc
*/
public function put($dataArray, $url = null, $wrapData = true)
{
throw new SdkException("Only POST method is allowed for GraphQL!");
}

/**
* @inheritdoc
*/
public function delete($urlParams = array(), $url = null)
{
throw new SdkException("Only POST method is allowed for GraphQL!");
}
}
68 changes: 68 additions & 0 deletions lib/HttpRequestGraphQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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 mixed $data
* @param array $httpHeaders
*
* @return void
*
* @throws SdkException if $data is not a string
*/
protected static function prepareRequest($httpHeaders = array(), $data = array())
{

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!");
}

self::$httpHeaders = $httpHeaders;
self::$httpHeaders['Content-type'] = 'application/graphql';

}

/**
* Implement a POST request and return json decoded output
*
* @param string $url
* @param mixed $data
* @param array $httpHeaders
*
* @return string
*/
public static function post($url, $data, $httpHeaders = array())
{
self::prepareRequest($httpHeaders, $data);

$response = CurlRequest::post($url, self::$postDataGraphQL, self::$httpHeaders);

return self::processResponse($response);
}
}
2 changes: 1 addition & 1 deletion lib/HttpRequestJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HttpRequestJson
*
* @var array
*/
private static $httpHeaders;
protected static $httpHeaders;

/**
* Prepared JSON string to be posted with request
Expand Down
2 changes: 1 addition & 1 deletion lib/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ protected function wrapData($dataArray, $dataKey = null)
*/
protected function castString($array)
{
if (is_string($array)) return $array;
if ( ! is_array($array)) return (string) $array;

$string = '';
$i = 0;
Expand Down
6 changes: 6 additions & 0 deletions lib/ShopifySDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
| //Get variants of a product (using Child resource)
| $products = $shopify->Product($productID)->Variant->get();
|
| //GraphQL
| $data = $shopify->GraphQL->post($graphQL);
|
*/
use PHPShopify\Exception\SdkException;

Expand Down Expand Up @@ -97,6 +100,7 @@
* @property-read Theme $Theme
* @property-read User $User
* @property-read Webhook $Webhook
* @property-read GraphQL $GraphQL
*
* @method AbandonedCheckout AbandonedCheckout(integer $id = null)
* @method Blog Blog(integer $id = null)
Expand Down Expand Up @@ -131,6 +135,7 @@
* @method Theme Theme(int $id = null)
* @method User User(integer $id = null)
* @method Webhook Webhook(integer $id = null)
* @method GraphQL GraphQL()
*/
class ShopifySDK
{
Expand Down Expand Up @@ -194,6 +199,7 @@ class ShopifySDK
'Theme',
'User',
'Webhook',
'GraphQL'
);

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/CurrencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: Tareq
* Date: 6/10/2019
* Time: 11:26 PM
*/

namespace PHPShopify;


class CurrencyTest
{

}