I am getting this error: Variable "$productSlug" is never used in operation "SingleProduct".
I also use gatsby-source-wordpress to query fields from wordpress to gatsby. I also uninstalled Gatsby and re-installed it to see if it works in different versions, but it didn't.
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,
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.
I'd recommend you check it before in the GraphiQL playground hardcoding the $productSlug
to check the output.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.