Skip to content

Instantly share code, notes, and snippets.

@magician11
Last active July 28, 2022 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magician11/a56f9952282507169b68e353c66bd6af to your computer and use it in GitHub Desktop.
Save magician11/a56f9952282507169b68e353c66bd6af to your computer and use it in GitHub Desktop.
How to fetch all products from Shopify in a single query
const setup = async () => {
const data = `
mutation {
webhookSubscriptionCreate(
topic: BULK_OPERATIONS_FINISH
webhookSubscription: {
format: JSON,
callbackUrl: "https://firebase-app.cloudfunctions.net/processAllProducts"}
) {
userErrors {
field
message
}
webhookSubscription {
id
}
}
}`;
const client = new Shopify.Clients.Graphql(
'your-shop.myshopify.com',
process.env.SHOPIFY_ACCESS_TOKEN
);
const res = await client.query({ data });
console.log(JSON.stringify(res.body, null, 2));
};
const retrieveUrl = async admin_graphql_api_id => {
const data = `
query {
node(id: "${admin_graphql_api_id}") {
... on BulkOperation {
url
partialDataUrl
}
}
}`;
const client = new Shopify.Clients.Graphql(
'your-shop.myshopify.com',
process.env.SHOPIFY_ACCESS_TOKEN
);
const res = await client.query({ data });
return res.body.data.node.url;
};
import { Shopify } from '@shopify/shopify-api';
const fetchAllProducts = async () => {
// https://shopify.dev/api/usage/bulk-operations/queries#step-1-submit-a-query
const queryString = `mutation {
bulkOperationRunQuery(
query: """
{
products {
edges {
node {
id
title
variants {
edges {
node {
title
inventoryQuantity
id
sku
inventoryItem {
id
}
metafields {
edges {
node {
namespace
key
value
}
}
}
}
}
}
}
}
}
}
"""
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}`;
const client = new Shopify.Clients.Graphql(
'your-shop.myshopify.com',
process.env.SHOPIFY_ACCESS_TOKEN
);
const res = await client.query({
data: queryString
});
return res.body;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment