【问题标题】:Filtering multiple types of content in graphql在graphql中过滤多种类型的内容
【发布时间】:2019-06-27 15:37:12
【问题描述】:

我正在使用 GatsbyJS,这是我第一次使用 react 和 graphql。我想混合单个页面的内容以同时包含“blog-post”类型内容和“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")
          }
        }
      }
    }
  }
`

【问题讨论】:

    标签: reactjs graphql gatsby


    【解决方案1】:

    eq 在这里表示等于,templateKey 只是你的 markdown frontmatter 中的另一个键。您给出的过滤器本质上是说“如果frontmatter 中的templateKey 的值等于博客文章”。

    您可以更改过滤器 来自

    filter: { frontmatter: { templateKey: { eq: "blog-post" } }}
    

    filter: { frontmatter: { templateKey: { in: ["blog-post", "repo-post"] } }}
    

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

    【讨论】:

    • 知道了,所以画一个类似sql的,这里的“frontmatter”类似于“table”,“templatekey”是列,eq/in(others im sure)是比较操作,然后是值或数组。我知道它们不一样,但我正在尝试调整一种心理模型以使其有意义。
    猜你喜欢
    • 2022-10-04
    • 2011-08-05
    • 2020-11-09
    • 2018-08-05
    • 2014-06-08
    • 2012-08-13
    • 2019-01-03
    • 2018-12-24
    • 2021-07-15
    相关资源
    最近更新 更多