Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL filter Contentful posts

I'm trying to fetch posts like category->post1, post2, but some category has no post. So I want to skip/filter those categories. How can I do that?

Query example:

query MyQuery {
  allContentfulCategory(limit: 10, filter: {}) {
    edges {
      node {
        slug
        name
        blog_post {
          slug
        }
      }
    }
  }
}

Screenshot

Thanks

like image 620
Moni Avatar asked Feb 14 '26 23:02

Moni


1 Answers

If your GraphQL nodes are properly set, you just can:

query MyQuery {
  allContentfulCategory(limit: 10, filter: {blog_post: {slug: {ne: null}}}) {
    edges {
      node {
        slug
        name
        blog_post {
          slug
        }
      }
    }
  }
}

The ne (not equal) filter should do the trick.

Alternatively, you can filter directly using JavaScript code, since it's a null value, it can be easily removed from the node array with:

let filtered = data.allContentfulCategory.edges.node.filter(el => el.blog_post != null);
like image 180
Ferran Buireu Avatar answered Feb 16 '26 14:02

Ferran Buireu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!