Unlock seamless workflows and faster delivery with our latest releases ā€“ get the details

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.
Apr 30, 2023, 9:26 PM
Oh wow it is just as simple as doing
"content": content[]->{...}

May 1, 2023, 2:24 AM
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.
May 1, 2023, 2:34 AM
I ended up doing this, not sure if that is accurate or not, didn't really find any documentation around it
May 1, 2023, 2:48 AM
Now I am running into an issue where I want to query for the categories that have the MOST references to posts and services.
May 1, 2023, 3:57 AM
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.
May 1, 2023, 4:07 PM
šŸ˜… 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?
May 1, 2023, 4:10 PM
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.
May 1, 2023, 4:39 PM
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 šŸ˜… šŸ˜…
May 1, 2023, 4:40 PM
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 šŸ¤”
May 1, 2023, 7:53 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?