使用Gatsby在graphQL查询中使用beginWith或startWith过滤器

在此处遵循盖茨比教程https://www.gatsbyjs.org/docs/adding-tags-and-categories-to-blog-posts/,可以按标签过滤帖子以轻松创建标签页面...

我要实现的目的是为具有相同的prefx的帖子创建索引页面:

  • / folder1 / sub1 / post-A
  • / folder1 / sub1 / post-B
  • / folder1 / sub2 / post-C

将创建3个索引页:

  • / folder1 /(包含三个帖子)
  • / folder1 / sub1 /(包含A和B帖子)
  • / folder1 / sub2 /(仅包含帖子C)

这将使用查询:

export const query = graphql`
  query tagListQuery($prefix: String,$skip: Int!,$limit: Int!) {
    allMarkdownRemark(
      sort: { fields: [frontmatter___date],order: DESC }
      filter: { fields: { slug: { startsWith: $prefix } } }
      limit: $limit
      skip: $skip
    ) {
      edges {
        node {
          id
          frontmatter {
            title
          }
          fields {
            slug
          }
        }
      }
    }
  }
`

但是startsWith过滤不存在​​:

  

“ message”:“字段\” startsWith \“未按类型定义   StringQueryOperatorInput。”

有没有一种方法可以使用带有graphQL的前缀匹配进行过滤?

kingpan998877 回答:使用Gatsby在graphQL查询中使用beginWith或startWith过滤器

您确定在atsapi:80/api/values内有fields吗?如果是这样,您应该向我们展示您的架构(位于node中),例如:

enter image description here

无论如何,我想您想查询http://localhost:8000/___graphql

fileAbsolutePath

如果要添加query tagListQuery($prefix: String,$skip: Int!,$limit: Int!) { allMarkdownRemark(sort: {order: DESC,fields: [frontmatter___date]},fileAbsolutePath: {regex: $prefix}},limit: $limit,skip: $skip) { edges { node { id frontmatter { title } } } } } 等,则需要customize the schema

本文链接:https://www.f2er.com/3044347.html

大家都在问