Is there any way to get content type's data (dropdown data)?

505 views Asked by At

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.)

enter image description here

Please let me know if there is a solution. Thanks

2

There are 2 answers

0
A.Gomathi sankar On BEST ANSWER

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 }), })

0
Robban On

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.

https://cdn.contentful.com/spaces/{space_id}/environments/{environment_id}/entries?access_token={access_token}&content_type={content_type}&fields.category[in]={categoryValue}