Resolving references in Sanity Studio using GROQ queries
my first step if I’m new in a dataset is to search all of a particular type to try to get a general view of what is going on:
*[_type == "article"]in my personal example.
And in my case, I am effectively using
categoryas a one-to-many sort method. And you are correct that if I’m strictly searching for articles, what it returns isn’t going to be very useful or readable on its own.
Please, let me know if I’m on the right track for what you are looking for.
https://www.sanity.io/docs/query-cheat-sheet
*[
_type in ["article", "category"]
]{title}{title} fields from both to make it easier to readSo, now I have a big list of titles of all documents with the type article AND category. Not where I want yet, but getting closer!Now, I think what we want to return a list of at least category titles associated with each article document. Back to the cheat sheet reference, back in a moment!
categorydocument - this could be handy!
“category”:{2 items
“_ref”:“4d27ad12-5d90-4371-9d9d-d3aa235ecac7"
“_type”:“reference”
}categorydocument - this could be handy!
“category”:{2 items
“_ref”:“4d27ad12-5d90-4371-9d9d-d3aa235ecac7"
“_type”:“reference”
}• a really nice groq join walkthrough video:
https://www.youtube.com/watch?v=gCexbCXAaLU • to nest a query, create a new object/array with a new name just for this query (ie: “articleCategories” in my example
• use
^to reference results in the parent query‘just like how I used to bullseye womp rats in my excel vlookups back home’!
🙂I should have phrased my question better. Here's a concrete example:
https://www.sanity.io/docs/create-custom-document-views-with-structure-builder In this tutorial, a custom pane is created where the document is displayed as JSON. This is quite easy to achieve because the data are provided to the pane component: https://www.sanity.io/docs/create-custom-document-views-with-structure-builder#1c1f58025b3a Now lets say that I want to display the name of the categories. On the front-end, I know that I need to write a GROQ query to resolve the references, but I was wondering if there was an easier to achieve this inside the Studio, like a special method provided by the Studio.
But now I realised that the Studio is always making extra queries to resolve the references. So I'll also make write a query to resolve the categories.
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.