Skip to content

Instantly share code, notes, and snippets.

@kmelve
Created June 23, 2023 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmelve/f2e8bb859baeb9718c36ec1ef014e5bd to your computer and use it in GitHub Desktop.
Save kmelve/f2e8bb859baeb9718c36ec1ef014e5bd to your computer and use it in GitHub Desktop.
How to join referenced category documents for a post with GROQ
// 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
},
}
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