Gatsby GraphQL 是否需要括号?
Does Gatsby GraphQL Need Surrounding Parentheses?
我是 Gatsby 的新手,尤其是 有趣的 语法选择(仅对字符串使用模板文字,不包括结束分号)。
我的问题是:是否需要在 graphql 查询周围加上括号?该文档并没有真正解释使用 graphql 的语法,教程中有带括号和不带括号的查询。
Gatsby tutorial - uses surrounding parentheses
exports.createPages = async ({ graphql, actions }) => {
...
const result = await graphql(` <--- parentheses here
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`)
console.log(JSON.stringify(result, null, 4))
}
Gatsby tutorial - no surrounding parentheses
export const query = graphql` <--- no parentheses here
query {
site {
siteMetadata {
title
}
}
}
`
是因为第一个例子的await
,还是我没理解别的?提前致谢!
在 gatsby-node.js
文件中,您需要使用常规的旧函数,即 graphql()
。
在 React land/components 中,您可以使用 tagged templates,如第二个示例所示。这是因为除其他外,Gatsby 然后使用 Babel 提取该查询并运行它。
我是 Gatsby 的新手,尤其是 有趣的 语法选择(仅对字符串使用模板文字,不包括结束分号)。
我的问题是:是否需要在 graphql 查询周围加上括号?该文档并没有真正解释使用 graphql 的语法,教程中有带括号和不带括号的查询。
Gatsby tutorial - uses surrounding parentheses
exports.createPages = async ({ graphql, actions }) => {
...
const result = await graphql(` <--- parentheses here
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`)
console.log(JSON.stringify(result, null, 4))
}
Gatsby tutorial - no surrounding parentheses
export const query = graphql` <--- no parentheses here
query {
site {
siteMetadata {
title
}
}
}
`
是因为第一个例子的await
,还是我没理解别的?提前致谢!
在 gatsby-node.js
文件中,您需要使用常规的旧函数,即 graphql()
。
在 React land/components 中,您可以使用 tagged templates,如第二个示例所示。这是因为除其他外,Gatsby 然后使用 Babel 提取该查询并运行它。