avianey
avianey

Reputation: 5833

Use a beginWith or startWith filter in a graphQL query with Gatsby

Following Gatsby tutorial here https://www.gatsbyjs.org/docs/adding-tags-and-categories-to-blog-posts/ it is possible to filter posts by tag to create tag pages easily...

What I'm trying to achieve is to create index pages for posts having the same slug prefx :

Will create 3 index pages :

This will use a query like :

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
          }
        }
      }
    }
  }
`

But startsWith filtering does not exists :

"message": "Field \"startsWith\" is not defined by type StringQueryOperatorInput."

Is there a way to filter using prefix matching with graphQL ?

Upvotes: 0

Views: 4154

Answers (1)

Dennis Vash
Dennis Vash

Reputation: 53894

Are you sure you got fields inside node? If so you should show us your schema (found in http://localhost:8000/___graphql), for example:

enter image description here

Anyway, I guess you want to query 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
        }
      }
    }
  }
}

If you want to add startWith etc, you need to customize the schema.

Upvotes: 1

Related Questions