Querying references and counting references in Sanity.io

9 replies
Last updated: May 1, 2023
Is there a way to get everything from a reference without typing everything out? For example I have a "content" that is a reference to a "service" and a "post" document that includes a title, description, etc etc. However, in order to get that in my groq query I can just do
export const categoryBySlug = groq`*[_type == "category" && slug.current == $slug]{
  title,
  "slug": slug.current,
  description,
  "excerpt": array::join(string::split((pt::text(description)), "")[0..160], "") + "...",
  content
}[0]`
I have to do

export const categoryBySlug = groq`*[_type == "category" && slug.current == $slug]{
  title,
  "slug": slug.current,
  description,
  "excerpt": array::join(string::split((pt::text(description)), "")[0..160], "") + "...",
  "content": content[]->{
    title,
    description,
    audience,
    website,
    phone,
    address
  }
}[0]`
Or at least I
think I do? However, the issue arises where if a new field is added to the reference I now have to update my query. I guess one could argue that I would have to update the code to show the new field... but it would be nice to not have to write out every single field. This would as both documents it references don't have the same fields.
AI Update

Good news! You don't need to list every field when dereferencing references. The -> operator by itself automatically fetches all fields from the referenced document.

Your simplified query can just be:

export const categoryBySlug = groq`*[_type == "category" && slug.current == $slug]{
  title,
  "slug": slug.current,
  description,
  "excerpt": array::join(string::split((pt::text(description)), "")[0..160], "") + "...",
  "content": content[]->
}[0]`

The reference access operator (->) automatically resolves the reference and returns all fields from the referenced documents. When you add new fields to your "service" or "post" documents, they'll automatically appear in your query results without any changes needed.

What if I want to be selective?

If you later decide you do want to limit which fields are returned (for performance or clarity), you can add a projection:

"content": content[]->{
  title,
  description,
  website
}

But unless you have a specific reason to limit fields, just using content[]-> gives you everything and is the most maintainable approach.

A note about the ellipsis operator (...)

You might see the ellipsis operator (...) used in GROQ projections, but it serves a different purpose. It's used to include all fields from the current document being iterated, not for dereferencing. For example, *[_type == 'movie']{...} returns all fields from movie documents. It's not needed when dereferencing references—the -> operator already handles that for you.

Oh wow it is just as simple as doing
"content": content[]->{...}

However, it is now saying
Error: Unable to resolve image URL from source (undefined)
from
urlForImage(post.author.image).url()
with just doing
...
🤔
My guess is because author itself is also a reference and doing
...
does not cascade all the way down.
I ended up doing this, not sure if that is accurate or not, didn't really find any documentation around it
Now I am running into an issue where I want to query for the categories that have the MOST references to posts and services.
Let it never be said that you aren't brave. I love your questions.
Searching by type within a field set at the top document level would be amazing. In this case especially since you could literally count the references.

I didn't even know we could select conditionally within the array of blocks, so thank you for that.
😅 thanks! And yeah if I can count the references that would be amazing! Or else I’m probably going to have to make each service and post reference the category instead of the other way around which might be a better solution?
Oh, snap, yeah, I always forget they're bi-directional. Seems more future-proof too in case other things will depend upon them as well.
Yeah I guess putting the category on the post and service would mean I could add tags to them as well and they would show up for that as well. My client is basically trying to fit a DB into a blog format 😅 😅
I guess that could also help with pagination issues I think I would face if I just referenced the posts and services inside a category 🤔

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.

Was this answer helpful?