【发布时间】:2020-10-23 06:40:44
【问题描述】:
我在我的项目中使用 Gatsby。我正在尝试创建一个需要分页的页面。
我遵循了这个 https://www.gatsbyjs.org/docs/adding-pagination/ 和这个其他指南 https://nickymeuleman.netlify.app/blog/gatsby-pagination/ 以及如何做到这一点,但它不起作用。
我对@987654325@ 有多个查询:
exports.createPages = async ({ graphql, actions, page }) => {
const { createPage } = actions
const shop = path.resolve("./src/templates/shop.js")
const productPageTemplate = path.resolve("./src/templates/ProductPage/index.js")
const singleProduct = await graphql(`
query {
allShopifyProduct {
edges {
node {
handle
}
}
}
}
`)
// This is the query which I need for the pagination
// Create shop pages
const products = await graphql(`
query allShopifyProduct($skip: Int!, $limit: Int!) {
allShopifyProduct(sort: { fields: [createdAt], order: DESC }
skip: $skip
limit: 5
) {
edges {
node {
id
title
handle
}
}
}
}
`)
const posts = products.data.allShopifyProduct.edges
const postsPerPage = 5
const numPages = Math.ceil(posts.length / postsPerPage)
Array.from({ length: numPages }).forEach((_, i) => {
const withPrefix = (pageNumber) =>
pageNumber === 1 ? `/shop` : `/shop/${pageNumber}`
const pageNumber = i + 1
createPage({
path: withPrefix(pageNumber),
component: shop,
context: {
limit: postsPerPage,
skip: i * postsPerPage,
current: pageNumber,
total: numPages,
hasNext: pageNumber < numPages,
nextPath: withPrefix(pageNumber + 1),
hasPrev: i > 0,
prevPath: withPrefix(pageNumber - 1),
},
})
})
// Adding the single product stuff to show my multiple queries
singleProduct.data.allShopifyProduct.edges.forEach(({ node }) => {
createPage({
path: `/product/${node.handle}/`,
component: productPageTemplate,
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
handle: node.handle,
},
})
})
})
这是我的 React 组件:
import React from "react"
import { graphql, Link } from "gatsby"
// Components
import ProductGrid from "@components/ProductGrid/productGrid"
const Shop = ({ data: { allShopifyProduct }, pageContext }) => {
return (
<div className="product-grid">
<ProductGrid allShopifyProduct={allShopifyProduct}
/>
{!pageContext.hasPrev && (
<Link to={pageContext.prevPage} rel="prev">
← Previous Page
</Link>
)}
{!pageContext.hasNext && (
<Link to={pageContext.nextPage} rel="next">
Next Page →
</Link>
)}
</div>
)
}
export default Shop
export const query = graphql`
query allShopifyProduct($skip: Int!, $limit: Int!) {
allShopifyProduct(
sort: { fields: [createdAt], order: DESC }
skip: $skip
limit: $limit
) {
edges {
node {
id
title
handle
}
}
}
}
`
而且它不断抛出类似这样的错误:
ERROR #85927 GRAPHQL
There was an error in your GraphQL query:
Variable "$skip" is never used in operation "allShopifyProduct".
See if $skip has a typo or allShopifyProduct doesn't actually require this variable.
File: gatsby-node.js:74:26
ERROR #85927 GRAPHQL
There was an error in your GraphQL query:
Variable "$limit" is never used in operation "allShopifyProduct".
See if $limit has a typo or allShopifyProduct doesn't actually require this variable.
File: gatsby-node.js:74:26
ERROR #11321 PLUGIN
"gatsby-node.js" threw an error while running the createPages lifecycle:
Cannot read property 'allShopifyProduct' of undefined
108 | `)
109 |
> 110 | const posts = products.data.allShopifyProduct.edges
| ^
111 |
File: gatsby-node.js:110:31
TypeError: Cannot read property 'allShopifyProduct' of undefined
- gatsby-node.js:110 Object.exports.createPages
/Users/marcelo/Work/gatsby-on-demand/gatsby-node.js:110:31
failed createPages - 0.113s
如果我在我的 GraphiQL 界面上运行这些精确查询,一切都会完美运行:
关于我可能在哪里失败的任何想法?
【问题讨论】:
标签: javascript reactjs ecmascript-6 graphql gatsby