Created
June 23, 2023 06:22
-
-
Save kmelve/f2e8bb859baeb9718c36ec1ef014e5bd to your computer and use it in GitHub Desktop.
How to join referenced category documents for a post with GROQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Try it out here: https://groq.dev/iZPFWScPZXANqgFGQgoUH2 | |
*[_type == "post"]{ | |
..., | |
// join into an array of just the titles | |
"categoryTitles": categories[]->title, | |
// join the completedocuments | |
"categoryDocs": categories[]->, | |
// join and project specific fields | |
categories[]->{ | |
title | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const post = defineType({ | |
name: 'post', | |
type: 'document', | |
fields: [ | |
{ | |
name: 'title', | |
type: 'string', | |
title: 'Title', | |
}, | |
{ | |
name: 'categories', | |
type: 'array', | |
title: 'Categories', | |
of: [ | |
{ | |
type: 'reference', | |
to: [{ type: 'category' }], | |
} | |
] | |
}, | |
] | |
}) | |
export const category = defineType({ | |
name: 'category', | |
type: 'document', | |
fields: [ | |
{ | |
name: 'title', | |
type: 'string', | |
title: 'Title', | |
}, | |
] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment