Currently I’m trying to work with the Contentful GraphQL API, although I can’t seem to find the option to fetch all tags at once. In the docs I find this article (https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/content-tags/tag-collection/get-all-tags/console/js) suggesting there is an option to do this.
Unfortunately this only seems to apply for the REST API and not for the GraphQL API? Or am I overlooking / misunderstanding something? When I use the GraphQL explore function, I also can’t find an option to query the tags.. Any tips?
1 Answer
This is from the future but If I get you correctly, you are trying to get all tags in a Contentful environment.
For Graphql, there is no option to get the tags directly unlike the rest API approach. If you insist on Graphql, you will be required to query a content type and then add the metadata to be returned with it. Your query should look like the following:
query {
articleCollection(limit: 100) {
items{
title
contentfulMetadata {
tags {
id
name
}
}
}
}
}
Check docs for more info on this.
To get all tags with the rest API approach:
const contentful = require('contentful-management')
const client = contentful.createClient({
accessToken: '<content_management_api_key>'
})
client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment_id>'))
.then((env) => env.getTags())
.then((tags) => console.log(tags))
.catch(console.error)
Note: Only public tags will be retrieved.