You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The GraphQL Client can be used to interact with any Shopify's GraphQL APIs. Client users are expected to provide the full API URL and necessary headers.
Getting Started
Using your preferred package manager, install this package in a project:
yarn add @shopify/graphql-client
npm install @shopify/graphql-client --s
pnpm add @shopify/graphql-client
CDN
The UMD builds of each release version are available via the unpkg CDN
// The minified `v0.9.3` version of the GraphQL API Client
<scriptsrc="https://unpkg.com/@shopify/[email protected]/dist/umd/graphql-client.min.js"></script><script>constclient=ShopifyGraphQLClient.createGraphQLClient({...});</script>
Create a server enabled client using a custom Fetch API
In order to use the client within a server, a server enabled JS Fetch API will need to be provided to the client at initialization. By default, the client uses window.fetch for network requests.
The number of HTTP request retries if the request was abandoned or the server responded with a Too Many Requests (429) or Service Unavailable (503) response. Default value is 0. Maximum value is 3.
Additional and/or replacement headers to be used in the request
retries?
number
Alternative number of retries for the request. Retries only occur for requests that were abandoned or if the server responds with a Too Many Request (429) or Service Unavailable (503) response. Minimum value is 0 and maximum value is 3.
signal?
AbortSignal
If this option is set, the request can be canceled by calling abort() on the corresponding AbortController.
ClientResponse<TData>
Name
Type
Description
data?
TData | any
Data returned from the GraphQL API. If TData was provided to the function, the return type is TData, else it returns type any.
Errors object that contains any API or network errors that occured while fetching the data from the API. It does not include any UserErrors.
extensions?
Record<string, any>
Additional information on the GraphQL response data and context. It can include the context object that contains the context settings used to generate the returned API response.
ClientStreamResponse<TData>
Name
Type
Description
data?
TData | any
Currently available data returned from the GraphQL API. If TData was provided to the function, the return type is TData, else it returns type any.
Errors object that contains any API or network errors that occured while fetching the data from the API. It does not include any UserErrors.
extensions?
Record<string, any>
Additional information on the GraphQL response data and context. It can include the context object that contains the context settings used to generate the returned API response.
hasNext
boolean
Flag to indicate whether the response stream has more incoming data
ResponseErrors
Name
Type
Description
networkStatusCode?
number
HTTP response status code
message?
string
The provided error message
graphQLErrors?
any[]
The GraphQL API errors returned by the server
response?
Response
The raw response object from the network fetch call
Usage examples
Query for a product
constproductQuery=` query ProductQuery($handle: String) { product(handle: $handle) { id title handle } }`;const{data, errors, extensions}=awaitclient.request(productQuery,{variables: {handle: 'sample-product',},});
Query for product info using the @defer directive
constproductQuery=` query ProductQuery($handle: String) { product(handle: $handle) { id handle ... @defer(label: "deferredFields") { title description } } }`;constresponseStream=awaitclient.requestStream(productQuery,{variables: {handle: 'sample-product'},});// await available data from the async iteratorforawait(constresponseofresponseStream){const{data, errors, extensions, hasNext}=response;}
Add additional custom headers to the API request
constproductQuery=` query ProductQuery($handle: String) { product(handle: $handle) { id title handle } }`;const{data, errors, extensions}=awaitclient.request(productQuery,{variables: {handle: 'sample-product',},headers: {'Shopify-Storefront-Id': 'shop-id',},});
Use an updated API URL in the API request
constproductQuery=` query ProductQuery($handle: String) { product(handle: $handle) { id title handle } }`;const{data, errors, extensions}=awaitclient.request(productQuery,{variables: {handle: 'sample-product',},url: 'http://your-shop-name.myshopify.com/api/unstable/graphql.json',});
Set a custom retries value in the API request
constshopQuery=` query ShopQuery { shop { name id } }`;// Will retry the HTTP request to the server 2 times if the requests were abandoned or the server responded with a 429 or 503 errorconst{data, errors, extensions}=awaitclient.request(shopQuery,{retries: 2,});
Provide GQL query type to client.request() and client.requestStream()
import{print}from'graphql/language';// GQL operation types are usually auto generated during the application buildimport{CollectionQuery,CollectionDeferredQuery}from'types/appTypes';importcollectionQueryfrom'./collectionQuery.graphql';importcollectionDeferredQueryfrom'./collectionDeferredQuery.graphql';const{data, errors, extensions}=awaitclient.request<CollectionQuery>(print(collectionQuery),{variables: {handle: 'sample-collection',},});constresponseStream=awaitclient.requestStream<CollectionDeferredQuery>(print(collectionDeferredQuery),{variables: {handle: 'sample-collection'},});
Using client.fetch() to get API data
constshopQuery=` query shop { shop { name id } }`;constresponse=awaitclient.fetch(shopQuery);if(response.ok){const{errors, data, extensions}=awaitresponse.json();}
Log Content Types
HTTPResponseLog
This log content is sent to the logger whenever a HTTP response is received by the client.
Property
Type
Description
type
LogType['HTTP-Response']
The type of log content. Is always set to HTTP-Response
Contextual data regarding the upcoming retry attempt.
requestParams: parameters used in the request lastResponse: previous response retryAttempt: the current retry attempt count maxRetries: the maximum number of retries