【发布时间】:2021-08-03 23:51:18
【问题描述】:
我正在使用 Gatsby 和 Shopify 构建网站。我想要做的是以编程方式为集合中的每个产品创建页面。根据tutorial,我得到了适用于所有产品的代码。现在我正在尝试编辑图形 QL 查询以仅返回集合中的产品。
gatsby develop 运行良好,但是当我尝试转到该页面时,我得到了 404
代码:
const path = require(`path`)
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
// Query for all products in Shopify
const result = await graphql(`
query {
shopifyCollection(id: {eq: "Shopify__Collection__Z2lkOi8vc2hvcGlmeS9Db2xsZWN0aW9uLzI3NDc2ODE2NzA3Mw=="}) {
products {
title
shopifyId
description
handle
priceRange {
minVariantPrice {
amount
}
}
images {
originalSrc
}
variants {
price
title
}
}
}
}
`)
// Iterate over all products and create a new page using a template
// The product "handle" is generated automatically by Shopify
result.data.shopifyCollection.products.forEach(({ node }) => {
createPage({
path: `/products/${node.handle}`,
component: path.resolve(`./src/pages/products.js`),
context: {
query: node,
},
})
})
}
当我在 ___graphq 工具中运行查询时,我会返回这些数据:
{
"data": {
"shopifyCollection": {
"products": [
{
"title": "Chicken and Roast Winter Vege",
"shopifyId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzY4ODUzNjc5NzIwMDE=",
"description": "",
"handle": "chicken-salad",
"priceRange": {
"minVariantPrice": {
"amount": "0.0"
}
},
"images": [
{
"originalSrc": "https://cdn.shopify.com/s/files/1/0439/1183/9905/products/Chicken_RoastVege.jpg?v=1627374225"
}
],
"variants": [
{
"price": "0.00",
"title": "Default Title"
},
{
"price": "0.00",
"title": "default"
}
]
},
{
"title": "Classic Dinners",
"shopifyId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzY4ODUzMzMzNjg5OTM=",
"description": "Experience Aotearoa's most affordable delivered meal kit. Packed full of ingredients for 5 classic family favourite meals with portion sizes to suit whānau small and large. $85 for 2 people, $95 for 4 and $130 for 6. Get recipes emailed each week or check out our community Facebook group to get inspired and share your own creations. Want to save a trip to the shop? Add on milk, bread, eggs and other pantry essentials. Try it out yourself and embrace the value!",
"handle": "classic-dinners",
"priceRange": {
"minVariantPrice": {
"amount": "85.0"
}
},
"images": [
{
"originalSrc": "https://cdn.shopify.com/s/files/1/0439/1183/9905/products/classic_dinners_socialV2.jpg?v=1627372974"
}
],
"variants": [
{
"price": "85.00",
"title": "2 / Reg"
},
{
"price": "100.00",
"title": "2 / GF"
},
{
"price": "95.00",
"title": "4 / Reg"
},
{
"price": "130.00",
"title": "4 / GF"
},
{
"price": "130.00",
"title": "6 / Reg"
},
{
"price": "160.00",
"title": "6 / GF"
}
]
}
]
}
},
"extensions": {}
}
有人可以指点我正确的方向吗?
【问题讨论】:
标签: reactjs shopify gatsby gatsby-plugin