我试图运行graphql查询从shopify graphql api检索所有产品,但由于速率限制,我们不能一次查询所有数据。那么,如何在不面临利率限制问题的情况下有效地查询所有数据呢?任何帮助都将不胜感激。
const myproducts = await axios({ url: `https://${req.session.shop}/admin/api/2022-01/graphql.json`, method: 'post', headers: { "X-Shopify-Access-Token": req.session.token, data: { query: ` query ($numProducts: Int!) { products(first: $numProducts) { edges { cursor node { title pageInfo { hasNextPage variables: { numProducts: 3 });
发布于 2022-09-08 10:09:10
如果是用于Shopify,则应该明确地检查它们关于分页的文档: https://shopify.dev/api/usage/pagination-graphql
您可以使用"while循环“来迭代,直到满足条件,在这种情况下,当"hasNextPage”为false时。下面是一个示例,说明如何从一个集合中获取所有产品:
//Query const getProdCollection = `query nodes($ids: [ID!]!, $cursor: String) { nodes(ids: $ids) { ... on Collection { products(first: 50, after: $cursor) { pageInfo { hasNextPage endCursor edges { cursor node { title handle descriptionHtml app.post("/api/getcollection", async (req, res) => { const session = await Shopify.Utils.loadCurrentSession( app.get("use-online-tokens") if (!session) { res.status(401).send("Could not find a Shopify session"); return; const client = new Shopify.Clients.Graphql( session.shop, session.accessToken //Set cursor to null for first iteration let cursor = null; let allData = []; while (true) { // place you looping request here let prodCollection = await client.query({ data: { query: getProdCollection, variables: { ids: [req.body['data']], cursor: cursor //update nextPage and curpor let nextPage = prodCollection.body.data.nodes[0].products.pageInfo.hasNextPage; cursor = prodCollection.body.data.nodes[0].products.pageInfo.endCursor; let myProductData = prodCollection.body.data.nodes[0].products.edges; allData = allData.concat(myProductData); //Finish the loop here if (nextPage == false) { break;