diff --git a/README.md b/README.md index 8459ff7..08282fb 100644 --- a/README.md +++ b/README.md @@ -311,10 +311,54 @@ $variables = [ $shopify->GraphQL->post($graphQL, null, null, $variables); ``` - ##### 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) +### Storefront GraphQL +The [Storefront GraphQL API](https://shopify.dev/docs/api/storefront) allows you to make authenticated requests to access data from a store's storefront. See [Storefront API Authentication](https://shopify.dev/docs/api/storefront#authentication) for more details. + +The Storefront GraphQL API uses an alternative url to make requests which follows the format: +`https://{shop}.myshopify.com/api/{version}/graphql.json` + +Example Usage: +```php +$shopify = \PHPShopify\ShopifySDK::config([ + 'AccessToken' => 'SHOPIFY_ADMIN_TOKEN', + 'ShopUrl' => 'my-store.myshopify.com', + // Required to make requests to the Storefront API + 'StoreFrontAccessToken' => 'STOREFRONT_ACCESS_TOKEN' +]); + +$query = << $productGID +]; + +// use the storefront() method +$data = $shopify->GraphQL->storefront($query, null null, $variables); +``` + +The `storefront` method uses the same arguments as the `post` method, so you can use variables as well. + ### 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. diff --git a/lib/GraphQL.php b/lib/GraphQL.php index 185593b..56018eb 100644 --- a/lib/GraphQL.php +++ b/lib/GraphQL.php @@ -49,6 +49,35 @@ public function post($graphQL, $url = null, $wrapData = false, $variables = null return $this->processResponse($response); } + /** + * Call POST method to the Storefront GraphQL API + * + * + * @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 + * @param array|null $variables + * + * @uses HttpRequestGraphQL::post() to send the HTTP request + * @throws ApiException if the response has an error specified + * @throws CurlException if response received with unexpected HTTP code. + * + * @return array + * @see https://shopify.dev/docs/api/storefront + */ + public function storefront($graphQL, $url = null, $wrapData = false, $variables = null) + { + $config = ShopifySDK::$config; + + if (!$url) { + $url = 'https://'.$config['ShopUrl'].'/api/'.$config['ApiVersion'].'/graphql.json'; + } + + $response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders, $variables); + + return $this->processResponse($response); + } + /** * @inheritdoc * @throws SdkException @@ -75,4 +104,4 @@ public function delete($urlParams = array(), $url = null) { throw new SdkException("Only POST method is allowed for GraphQL!"); } -} \ No newline at end of file +} diff --git a/lib/ShopifyResource.php b/lib/ShopifyResource.php index dd3b2cb..13b7c1f 100644 --- a/lib/ShopifyResource.php +++ b/lib/ShopifyResource.php @@ -150,6 +150,10 @@ public function __construct($id = null, $parentResourceUrl = '') throw new SdkException("Either AccessToken or ApiKey+Password Combination (in case of private API) is required to access the resources. Please check SDK configuration!"); } + if (isset($config['StoreFrontAccessToken'])) { + $this->httpHeaders['X-Shopify-Storefront-Access-Token'] = $config['StoreFrontAccessToken']; + } + if (isset($config['ShopifyApiFeatures'])) { foreach($config['ShopifyApiFeatures'] as $apiFeature) { $this->httpHeaders['X-Shopify-Api-Features'] = $apiFeature;