How can I use GraphQL to get a list of unique tags associated with articles that match a given query?
I want to have a show a list of tags that are used by articles that have “drupal” in their title.
Without grouping, I need to fetch all tags of all articles via graphql, and then combine +
reduce the tags on the client side. This is very inefficient, because it could be 10000 articles that only have 2 tags in the end.
{
tags: nodeQuery(
filter: {
conditions: (
{operator: EQUAL, field: "type", value: "article"},
{operator: LIKE, field: "title", value: "%drupal%"}
)
}
) {
entities {
... on NodeArticle {
fieldTags {
entity {
entityLabel
}
}
}
}
}
}
Response:
{
"data": {
"tags": {
"entities": (
{
"fieldTags": (
{
"entity": {
"entityLabel": "Drupal"
}
},
{
"entity": {
"entityLabel": "Development"
}
}
)
},
{
"fieldTags": (
{
"entity": {
"entityLabel": "Drupal"
}
},
{
"entity": {
"entityLabel": "Development"
}
},
{
"entity": {
"entityLabel": "Hacking"
}
}
)
},
....
I’m using Drupal 8.7 with graphql 8.x-3.0.