【发布时间】: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 调用。最好的解决方案是什么?
【问题讨论】:
-
使用
GraphQLAPI 而不是RESTAPI 在单个 API 调用中获取更大的数据。
标签: javascript node.js shopify shopify-app