GraphQL and counting related results

Hi,

I have a GraphQL query to retrieve BlogCategories. Which looks somewhat like this:

query GetBlogCategories($locale: I18NLocaleCode) {
  blogCategories(locale: $locale) {
    data {
       id
       attributes {
         title
         description
       }
    }
  }
}

The BlogCategoryFields are shown, now what I need is to get the count of articles for each BlogCategory. I’ve tried to retrieve the article count like this:

{
  blogCategories(locale: "en") {
    data {
      id
      attributes {
        title
        description
      	blogs {
          data {
            id
            attributes {
              title 
            }
          }
          aggregate {
            count
          }
        }
      }
    }
  }
}

But it throws an error “message”: “Cannot query field "aggregate" on type "BlogArticleRelationResponseCollection".”. I understand that aggregate can only be used in top level queries.

Can someone show me how to get the count for the related articles inside the blogCategory graphql query?

1 Like