👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

How to Make Referenced Objects Inlined in a Query

21 replies
Last updated: Apr 28, 2020
How do I make referenced objects inlined in a query?
Apr 28, 2020, 8:54 PM
Specifically, I have a
page
type that has
sections
. some sections types are references, others are not, but I want all the data inlined into the result
Apr 28, 2020, 8:57 PM
you could make an
array
that can have
references
and other types
then your query would be to fetch the
array
which would return all the different types
Apr 28, 2020, 9:10 PM
yes..? that is what I have
Apr 28, 2020, 9:10 PM
the problem is that the query result only contains the rerference IDs, and I don’t want to have to request them individually
Apr 28, 2020, 9:11 PM
what does your query look like?
Apr 28, 2020, 9:11 PM
*[_type == 'page']
Apr 28, 2020, 9:13 PM
now i'm tracking ... not entirely sure how you'd get there to be honest
have you looked at the GROQ cheat sheet

https://www.sanity.io/docs/query-cheat-sheet
Apr 28, 2020, 9:21 PM
yeah it gets me close, but the problem is that the array contains a mix of things¨
Apr 28, 2020, 9:22 PM
totally
Apr 28, 2020, 9:23 PM
so a projective query like the example there isn’t clear how would work
Apr 28, 2020, 9:23 PM
you might have to combine projections??
Apr 28, 2020, 9:23 PM
not sure what that would look like
Apr 28, 2020, 9:24 PM
user Y
any idea?
Apr 28, 2020, 9:24 PM
possibly something like this?

*[_type=="person"]{
  name,
  "relatedMovies": *[_type=='movie' && references(^._id)]{ title }
}

Apr 28, 2020, 9:25 PM
maybe not... now that i'm looking at it
Apr 28, 2020, 9:25 PM
*[_type == 'page']{
  sections[]{
    ...,
    defined(_ref) => @->
  }
}
Try this? (not knowing if the reference is nested or on root in the section object
Apr 28, 2020, 9:52 PM
fancy
Apr 28, 2020, 9:52 PM
thanks, will try!
Apr 28, 2020, 9:52 PM
that worked 🙂
Apr 28, 2020, 9:54 PM
thanks!
Apr 28, 2020, 9:54 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?