I have several posts with different categories. How do I query all categories of the posts at once?
I have something like:
However, when I try to query something like this:
{
allMarkdownRemark(filter: {fileAbsolutePath: {regex: "/(/news/)/"}, frontmatter: {category: {}}}) {
edges {
node {
frontmatter {
category
}
}
}
}
}
It will return:
{
"data": {
"allMarkdownRemark": {
"edges": [
{
"node": {
"frontmatter": {
"category": "Event"
}
}
},
{
"node": {
"frontmatter": {
"category": "News"
}
}
},
{
"node": {
"frontmatter": {
"category": "News"
}
}
},
{
"node": {
"frontmatter": {
"category": "News"
}
}
}
]
}
}
}
Which I'd expect it should only return:
There's no way to query and get the category only. You can do this:
const categories = this.props.data.allMarkdownRemark.edges.map(
({node}) => node.frontmatter.category);
You can use the group query:
https://www.gatsbyjs.org/docs/graphql-reference/#group
fieldValue is what you're interested in then.
Thanks! the information is really helpful :)
Most helpful comment
You can use the
groupquery:https://www.gatsbyjs.org/docs/graphql-reference/#group
fieldValue is what you're interested in then.