Querying references and counting references in Sanity.io
9 replies
Last updated: May 1, 2023
J
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
I have to do
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.
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]`
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]`
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
J
Oh wow it is just as simple as doing
"content": content[]->{...}
May 1, 2023, 2:24 AM
J
However, it is now saying from
My guess is because author itself is also a reference and doing
Error: Unable to resolve image URL from source (undefined)
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
J
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
J
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
V
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.
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
J
š
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
V
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
J
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
J
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.