【问题标题】:What is the best way to do an API call to Shopify when multiple calls are needed?当需要多次调用时,对 Shopify 进行 API 调用的最佳方式是什么?
【发布时间】:2021-08-04 16:58:38
【问题描述】:

我正在使用 MERN 堆栈编写后端,需要向 Shopify 的 API 发出 GET 请求以返回一些订单信息。我还需要有关产品的一些信息,但这需要对另一个 API 端点的 GET 请求。如果我要返回最新的 100 个未结订单,那么我需要对 Shopify 的产品 API 端点进行 100 次 API 调用。这似乎非常昂贵并且不是一个好的解决方案。我觉得我在这里错过了一个关键要素。我返回订单信息的代码是:

// Get the order information
const orderURI = encodeURI(`${URL}/admin/api/2021-07/orders.json?status=${p.order_status}`);
// Get the product information
const productURI = encodeURI(`${URL}/admin/api/2020-10/products.json`);


const headers = {
    'user-agent': 'node.js'
}

// Get order object
const shopifyOrderResponse = await axios.get(orderURI, {headers});
const retrievedOrders = shopifyOrderResponse.data.orders;

// Get product object
const shopifyProductResponse = await axios.get(productURI, {headers})
const retrievedProducts = shopifyProductResponse.data.products




let modifiedFiltered2 = retrievedOrders.map(order => ({
    "order_id": order.id,
    "source_id": order.name.substring(1),
    "order_data": order.line_items.map(variants => ({
            "product_id": variants.product_id,
            "sku": variants.sku,
            "title": variants.title,
            "variant_title": variants.variant_title,
            "variant_id": variants.variant_id
        })
    )
}))
return (res.json(modifiedFiltered2))

理想情况下,我想在“order_data”对象中包含有关产品的一些信息,但这需要特定的 API 调用。最好的解决方案是什么?

【问题讨论】:

  • 使用 GraphQL API 而不是 REST API 在单个 API 调用中获取更大的数据。

标签: javascript node.js shopify shopify-app


【解决方案1】:

如果我正确理解了您的问题,您可以在一次调用 Shopify API 中获得 N 个订单,但您仍然需要有关这 N 个产品的更多信息,并且您不想调用 fetch product API N 次。

我在 Shopify Products API 文档中找到了这个 https://shopify.dev/api/admin/rest/reference/products/product#index-2021-07

这应该对你有用,因为它接受逗号分隔的 id 列表。

GET /admin/api/2021-07/products.json?ids=632910392,921728736

【讨论】:

  • 是的,没错。我可以从一个 API 调用中获得 N 个订单,但我需要执行额外的 N 个 API 调用才能获取更多特定于单个订单的信息。谢谢你的链接。我将尝试提出比上面发布的更可行的解决方案。
猜你喜欢
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 2021-06-22
  • 1970-01-01
相关资源
最近更新 更多