graphql 标记中不允许进行字符串插值。 (盖茨比)

String interpolation is not allowed in graphql tag. (Gatsby)

正在尝试为我的 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, // 
    },
  })
})

看看这个来自 gatsby 的 using-wordpress example