Last active
July 28, 2022 23:21
-
-
Save magician11/a56f9952282507169b68e353c66bd6af to your computer and use it in GitHub Desktop.
How to fetch all products from Shopify in a single query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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