Reputation: 109
I'm working on contentful to create blog posts.
I have created a field named with category with dropdown data, like the below image.
I have created more blogs for each categories (Ex: game has 5 blogs, Tour has 10 blogs and etc).
I want to show the list of all categories with content counts,
Is there any possible to get all of the categories with content count? ( I can get it by getting all blogs using this query
const res = ContentfulService.getEntries({ content_type: 'blog'})
then I grouped with category, but to get the category only, I don't want to get all of the blogs.)
Please let me know if there is a solution. Thanks
Upvotes: 0
Views: 632
Reputation: 109
I have achieved that using graphQL,
const query = { categoryCollection { items{ linkedFrom { blogCollection { total } } name:category sys { id } } } }
let categories = await fetch(https://graphql.contentful.com/content/v1/spaces/${spaceId}
, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// Authenticate the request
Authorization: Bearer ${accessToken}
,
},
// send the GraphQL query
body: JSON.stringify({ query }),
})
Upvotes: 0
Reputation: 6802
The only way to do this through the API would be to make a request for each category and look at the total
property of the response and that would be less efficient than what you're already suggesting.
Upvotes: 1