2

I created categories in craft and I'd like to get total amount of entries related to individual category. Right now I have a query that returns my categories:

query categoriesQuery {
  categories(group: "projectCategories") {
    id
    slug
    title
  }
}

but I would also like for each category to return the amount of related entries. So I would like to add another field that counts related entries - I wanted to do somethings like this, but it won't work:

query categoriesQuery {
  categories(group: "projectCategories") {
    id
    slug
    title
    _count(relatedToEntries(section: "projects"))
  }
}

Is there any way to get amount of entries related to single category? The result that I'm trying to achieve would look somethings like this:

{
  "data": {
    "categories": [
      {
        "id": "1",
        "slug": "Category-1",
        "title": "Category 1",
        "amountOfRelatedEntries": 1
      },
      {
        "id": "2",
        "slug": "Category-2",
        "title": "Category 2",
        "amountOfRelatedEntries": 2
      },
      {
        "id": "3",
        "slug": "Category-3",
        "title": "Category 3",
        "amountOfRelatedEntries": 3
      },
    ]
  }
}

0 Answers0