在graphql中过滤多种类型的内容

mrp*_*atg 0 reactjs graphql gatsby

我正在使用 GatsbyJS,这是我第一次使用 react 和 graphql。我想混合单个页面的内容以包含“博客文章”类型的内容和“repo-post”类型的内容,但我不知道如何将其添加到查询中以对其进行过滤。

此外; 我知道 frontmatter 是什么(re:markdown),但我不知道在这种情况下什么 templateKey 和“eq”。我不太了解我在看什么,不知道什么叫开始寻找解释。

export const pageQuery = graphql`
  query IndexQuery {
    allMarkdownRemark(
      sort: { order: DESC, fields: [frontmatter___date] },
      filter: { frontmatter: { templateKey: { eq: "blog-post" } }}
    ) {
      edges {
        node {
          excerpt(pruneLength: 400)
          id
          fields {
            slug
          }
          frontmatter {
            title
            templateKey
            date(formatString: "MMMM DD, YYYY")
          }
        }
      }
    }
  }
`
Run Code Online (Sandbox Code Playgroud)

meh*_*sum 10

eq这里的意思是等于,templateKey只是你的降价前端中的另一个关键。您给出的过滤器实际上是在说“如果templateKey's 中的值frontmatter等于博客文章”。

您可以更改过滤器

filter: { frontmatter: { templateKey: { eq: "blog-post" } }}
Run Code Online (Sandbox Code Playgroud)

filter: { frontmatter: { templateKey: { in: ["blog-post", "repo-post"] } }}
Run Code Online (Sandbox Code Playgroud)

您应该查看graphql 上文档。它还有一个很棒的工具叫做 GraphiQL 来处理查询。