【问题标题】:Does Gatsby GraphQL Need Surrounding Parentheses?Gatsby GraphQL 是否需要括号?
【发布时间】:2020-09-07 17:25:08
【问题描述】:

我是 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 graphql-js


    【解决方案1】:

    gatsby-node.js 文件中,您需要使用常规的旧函数,即graphql()

    在 React 领域/组件中,您可以使用 tagged templates,如第二个示例所示。这是因为 Gatsby 使用 Babel 提取查询并运行它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 2020-11-25
      • 2021-10-05
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多