Strapi v4: graphQL query filters for notEmpty items

System Information
  • Strapi Version: 4
  • Operating System: macOS
  • Database: SQLite
  • Node Version: 16
  • NPM Version: 6

I have a blog with content types «post» and «category». Post can have and belongs to multiple categories.

There are categories that are created, but don’t have posts yet.

I would like to query GraphQL: show me all categories that have posts.

I tried several variants of requests and failed.

My example:

query getCategoriesWithPosts {
  categories(filters: { posts: {ne: "data"} }) {
    data {
      id
      attributes {
        title
        posts {
          data {
            id
          }
        }
      }
    }
  }
}

Error: "message": "Field \"ne\" is not defined by type \"PostFiltersInput\».»,

If category doesn’t have posts, categories.data.attributes.posts is empty array.

What kind of filter should I apply to get only not empty categories?

Answer:

query getCategoriesWithPosts {
  categories(filters: {posts: {id: {null: false}}}) {
    data {
      id 
      attributes {
        title
        slug
        posts(sort: "publishedAt:desc") {
          data {
            id
            attributes {
              title
            }
          }
        }
      }
    }
  }
}