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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With