尝试在我的 graphql 查询中为长而复杂的 Id 使用别名:
编译失败:graphql 标签中不允许字符串插值:
const query = graphql`
query MyQuery {
wordpress {
menu(id: "${wordpress("mainMenu")}") {
...rest of query
}
}
}
`
最佳答案
您应该改用查询变量。
Variables can be added to page queries (but not static queries) through the context object that is an argument of the
createPage
API. docs
// inside template file
export const query = graphql`
query MyQuery($id: String!) {
menu(id: { eq: $id }) {
...rest of query
}
}
`
然后你可以在 gatsby-node.js
中提供这样的变量作为页面上下文的一部分.例如:// gatsby-node.js
const postTemplate = path.resolve(`./src/templates/post.js`)
allWordpressPost.edges.forEach(edge => {
createPage({
path: `/${edge.node.slug}/`,
component: slash(postTemplate),
context: {
id: edge.node.id, // 👈
},
})
})
看看这个 using-wordpress example来自 Gatsby 。
关于javascript - graphql 标签中不允许字符串插值。 ( Gatsby ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62629823/