Resolving references in Sanity Studio using GROQ queries

11 replies
Last updated: Feb 2, 2022
Hi everyone!I'm rendering a Sanity document inside a split pane in the Studio. It's very convenient to get the document object in the function so that I can easily display it. However, some fields are references... Does it mean that I need to make a Groq query to resolve those references?
Feb 1, 2022, 12:40 PM
hi User! As I’m currently learning some deeper groq tricks myself, let me see if I can write a quick conditional query function on at least a one-to-many field and maybe help you with yours.
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
category
as 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.
Feb 1, 2022, 10:03 PM
And here is a nice groq cheat sheet reference:
https://www.sanity.io/docs/query-cheat-sheet
Feb 1, 2022, 10:04 PM
Taking a step back, here is the small test document in the studio… and currently the category schema just has a title and a very lite portable text description field. I also have a ‘tags’ relation for many-to-many relations to get my basic document schema started - which I’m leaving blank in the test document on purpose to keep things simple.
Feb 1, 2022, 10:07 PM
Taking a step back, here is the small test document in the studio… and currently the category schema just has a title and a very lite portable text description field.
Feb 1, 2022, 10:07 PM
just pseudo-coding the ultimate outcome of what I ultimately want to happen, my own first step is to grab all documents from multiple “types” - in this very small example my types are “article” and “category”
*[
  _type in ["article", "category"]
]{title}
• I’ve filtered the return info to just return the
{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!
Feb 1, 2022, 10:19 PM
playing a bit more - so now I have a “category” field in article, and that category field has a “_ref” id … this ref id just happens to match the “_id” field in the
category
document - this could be handy!
“category”:{2 items
“_ref”:“4d27ad12-5d90-4371-9d9d-d3aa235ecac7"
“_type”:“reference”
}
Feb 1, 2022, 10:33 PM
playing a bit more - so now I have a “category” field in article, and that category field has a “_ref” id … this ref id just happens to match the “_id” field in the
category
document - this could be handy!
“category”:{2 items
“_ref”:“4d27ad12-5d90-4371-9d9d-d3aa235ecac7"
“_type”:“reference”
}
Feb 1, 2022, 10:33 PM
ok I think this will do the trick for the type of joined query you might be looking for… in my super minimal playground, I have a couple “article” documents with a reference to a “category” document - this ref is only defined in “article”this pretty simply returns the title of article, and the title of each related category
Feb 1, 2022, 11:59 PM
Again, my best guess to what you were trying to do may be way off the mark, but this may be a good learning opportunity for someone.Nice lessons:
• 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’!
Feb 2, 2022, 12:08 AM
Hi
user U
Thank you for your explaination and the links you shared
🙂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.
Feb 2, 2022, 10:13 AM
Ah thank you. It was a good practice opportunity for me regardless. I haven’t had the opportunity to build this into document views from scratch yet personally, but believe you are correct that additional queries would be necessary.
Feb 2, 2022, 4:45 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?