Skip to content

Feature: Storefront GraphQL Requests #308

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<GQL
// retrieve the french canadian translations for a product
query getProductById(\$id: ID!) @inContext(country: CA, language: FR) {
product(id: \$id) {
id
title
handle
productType
tags
descriptionHtml
options {
id
name
values
}
}
}
GQL;

// product global id - 'gid://shopify/Product/123456789'
$variables = [
'id' => $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.
Expand Down
31 changes: 30 additions & 1 deletion lib/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -75,4 +104,4 @@ public function delete($urlParams = array(), $url = null)
{
throw new SdkException("Only POST method is allowed for GraphQL!");
}
}
}
4 changes: 4 additions & 0 deletions lib/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down