[英]Variable "$productSlug" is never used in operation "SingleProduct". Graphql error. Variables are not working in my Gatsby graphql queries
I am getting this error: Variable "$productSlug" is never used in operation "SingleProduct".我收到此错误:变量“$productSlug”从未在“SingleProduct”操作中使用。
I also use gatsby-source-wordpress to query fields from wordpress to gatsby.我还使用 gatsby-source-wordpress 查询从 wordpress 到 gatsby 的字段。 I also uninstalled Gatsby and re-installed it to see if it works in different versions, but it didn't.
我还卸载了 Gatsby 并重新安装它以查看它是否适用于不同的版本,但它没有。
I searched all over the internet and stack overflow for an answer, I really believe it must be a bug with Gatsby or gatsby-source-wordpress,我在整个互联网上搜索并堆栈溢出以寻找答案,我真的相信这一定是 Gatsby 或 gatsby-source-wordpress 的错误,
this is the code:这是代码:
const path = require("path"); const { createFilePath } = require(`gatsby-source-filesystem`); exports.onCreatePage = ({ page, actions }) => { const { createPage } = actions; if (page.path.match(/products/)) { page.context.layout = "ArchiveProduct"; createPage(page); } if (page.path.match(/products\/([^\/]+$)/)) { page.context.layout = "SingleProduct"; createPage(page); } }; exports.onCreateNode = ({ node, getNode, actions }) => { const { createNodeField } = actions; if (node.internal.type === `allWpProduct`) { const slug = createFilePath({ node, getNode, basePath: `pages` }); createNodeField({ node, name: `slug`, value: slug, }); } }; exports.createPages = async function ({ graphql, actions }) { const { data } = await graphql(` query SingleProduct { allWpProduct { nodes { uri slug id } } } `); data.allWpProduct.nodes.forEach((node) => { // const slug = node.slug; actions.createPage({ path: "/products/" + node.slug, component: path.resolve("./src/templates/SingleProduct.js"), context: { productSlug: node.slug, productId: node.id, layout: "SingleProduct", }, }); }); };
And this is the query:这是查询:
export const query = graphql` query SingleProduct($productSlug: String:) { wpProduct(slug: { eq: "$productSlug" }) { title slug link id date(formatString, "MMMM DD; YYYY") product { description price slug photo { localFile { childImageSharp { gatsbyImageData } } } } } } `;
Try the following:尝试以下操作:
export const query = graphql`
query SingleProduct($productSlug: String!) {
wpProduct(filter: {slug: { eq: "$productSlug" }}) {
title
slug
link
id
date(formatString: "MMMM DD, YYYY")
product {
description
price
slug
photo {
localFile {
childImageSharp {
gatsbyImageData
}
}
}
}
}
}
`;
Your issue appears because $productSlug
is lifted properly via context but never used in any sort of filtering action inside the query.出现您的问题是因为
$productSlug
已通过上下文正确提升,但从未用于查询内的任何类型的过滤操作。
I'd recommend you check it before in the GraphiQL playground hardcoding the $productSlug
to check the output.我建议您在 GraphiQL 游乐场硬编码
$productSlug
之前检查它以检查 output。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.