Skip to content
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

Feature: Storefront GraphQL Requests #308

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

whobutsb
Copy link
Contributor

@whobutsb whobutsb commented Sep 29, 2023

I propose adding a new feature to the library enabling users to interact with the Shopify Storefront GraphQL API. This API necessitates an alternative endpoint for authenticated requests: https://my-store.myshopify.com/api/2023-04/graphql.json.

Key Modifications:

  1. Updated lib/ShopifyResource.php:
    • Modified __construct() to accept and set a new configuration variable StoreFrontAccessToken, obtainable via Shopify Admin under "Private App" setup.
  2. Extended lib/GraphQL.php:
    • Introduced storefront() method, mirroring post() method’s signature. If $url argument remains unset, it assembles the correct Shopify GraphQL storefront URL for requests.

Example Usage:

$shopify = \PHPShopify\ShopifySDK::config([
    'AccessToken' => 'SHOPIFY_ADMIN_TOKEN',
    'ShopUrl' => 'my-store.myshopify.com',
    'StoreFrontAccessToken' => 'STOREFRONT_ACCESS_TOKEN'  // Required for Storefront API requests
]);

$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
];

$data = $shopify->GraphQL->storefront($query, null null, $variables);

Motivation:
I Encountered a scenario requiring product locale translations retrieval for a client's store. The existing Shopify Admin GraphQL API fell short, necessitating the Storefront API. I appreciate the ease of use this library brings and think this feature would be beneficial to the developers that work with this great library.

I appreciate any feedback and am open to making any necessary changes. Thank you for considering this enhancement!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant