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?
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
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
yes..? that is what I have
the problem is that the query result only contains the rerference IDs, and I don’t want to have to request them individually
what does your query look like?
*[_type == 'page']
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
yeah it gets me close, but the problem is that the array contains a mix of things¨
totally
so a projective query like the example there isn’t clear how would work
you might have to combine projections??
not sure what that would look like
user Y
any idea?
possibly something like this?

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

maybe not... now that i'm looking at it
*[_type == 'page']{
  sections[]{
    ...,
    defined(_ref) => @->
  }
}
Try this? (not knowing if the reference is nested or on root in the section object
fancy
thanks, will try!
that worked 🙂
thanks!

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?