From 3789014d2aae4925d38e01159bc7796e2a9ab099 Mon Sep 17 00:00:00 2001 From: Steve Barbera Date: Fri, 29 Sep 2023 10:33:32 -0600 Subject: [PATCH 1/5] Added the GraphQL Storefront API method to make requests to the storefront api. --- README.md | 37 ++++++++++++++++++++++++++++++++++++- lib/GraphQL.php | 31 ++++++++++++++++++++++++++++++- lib/ShopifyResource.php | 4 ++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8459ff7..993035d 100644 --- a/README.md +++ b/README.md @@ -311,10 +311,45 @@ $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 = <<GraphQL->storefront($query); +``` + +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; From 2f507441c1f7c4a7e7f9503c23c60d2ce1c303d5 Mon Sep 17 00:00:00 2001 From: Steve Barbera Date: Fri, 29 Sep 2023 10:59:30 -0600 Subject: [PATCH 2/5] Updated the README example to use a storegront locale query --- README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 993035d..916d4ca 100644 --- a/README.md +++ b/README.md @@ -330,18 +330,23 @@ $shopify = \PHPShopify\ShopifySDK::config([ ]); $query = << Date: Fri, 29 Sep 2023 11:00:32 -0600 Subject: [PATCH 3/5] Fixed the example --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 916d4ca..b0706ea 100644 --- a/README.md +++ b/README.md @@ -330,7 +330,7 @@ $shopify = \PHPShopify\ShopifySDK::config([ ]); $query = <<GraphQL->storefront($query); From 2df34185281134e80c18c13fe752a82b36e0ffd2 Mon Sep 17 00:00:00 2001 From: Steve Barbera Date: Fri, 29 Sep 2023 11:28:14 -0600 Subject: [PATCH 4/5] Forgot to include the varialbes --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b0706ea..0226c9e 100644 --- a/README.md +++ b/README.md @@ -348,8 +348,12 @@ $query = << $productId +]; + // use the storefront() method -$products = $shopify->GraphQL->storefront($query); +$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. From 94dcbb0f6a7cb5e802b4c36edc3fd54840c82659 Mon Sep 17 00:00:00 2001 From: Steve Barbera Date: Fri, 29 Sep 2023 11:34:07 -0600 Subject: [PATCH 5/5] using product gid --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0226c9e..08282fb 100644 --- a/README.md +++ b/README.md @@ -348,8 +348,9 @@ $query = << $productId + 'id' => $productGID ]; // use the storefront() method