gpt4 book ai didi

javascript - graphql 标签中不允许字符串插值。 ( Gatsby )

转载 作者:行者123 更新时间:2023-12-04 15:59:02 24 4
gpt4 key购买 nike

尝试在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com