Instances of this class can make requests to the Shopify Admin GraphQL API.
Note: You can use the Shopify Admin API GraphiQL explorer to help build your queries.
// Requests to /my-endpoint must be made with authenticatedFetch from App Bridge for embedded apps
app.get('/my-endpoint', async () => {
const sessionId = await shopify.session.getCurrentId({
isOnline: true,
rawRequest: req,
rawResponse: res,
});
// use sessionId to retrieve session from app's session storage
// getSessionFromStorage() must be provided by application
const session = await getSessionFromStorage(sessionId);
const client = new shopify.clients.Graphql({
session,
apiVersion: ApiVersion.January23,
});
});
Receives an object containing:
Session
| ❗ required
The Shopify Session containing an access token to the API.
ApiVersion
This will override the default API version. Any requests made by this client will reach this version instead.
Sends a request to the Admin API.
const response = await client.request(
`{
products (first: 10) {
edges {
node {
id
title
descriptionHtml
}
}
}
}`,
);
console.log(response.data, response.extensions);
const response = await client.request(
`query GetProducts($first: Int!) {
products (first: $first) {
edges {
node {
id
title
descriptionHtml
}
}
}
}`,
{
variables: {
first: 10,
},
},
);
console.log(response.data, response.extensions);
Note: If using TypeScript, you can pass in a type argument for the response body:
// If using TypeScript, you can type the response body
interface MyResponseBodyType {
data: {
//...
};
}
const response = await client.request<MyResponseBodyType>(/* ... */);
// response.body will be of type MyResponseBodyType
console.log(response.body.data);
Note: If there are any errors in the response,
request
will throw aGraphqlQueryError
which includes details from the API response:
import {GraphqlQueryError} from '@shopify/shopify-api';
try {
const products = await client.request(/* ... */);
// No errors, proceed with logic
} catch (error) {
if (error instanceof GraphqlQueryError) {
// look at the GraphQL errors returned from the API response
error.body?.errors.graphQLErrors
// Also, error.headers contains the headers of the response received from Shopify
} else {
// handle other errors
}
}
string
| ❗ required
The query or mutation string.
{[key: string]: any}
The variables for the operation.
{[key: string]: string | number}
Add custom headers to the request.
number
| Must be between 0 and 3
The maximum number of times to retry the request.
Promise<ClientResponse>
Returns an object containing:
any
The data
component of the response.
any
The extensions
component of the response.