✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

How to Expand a Reference on the Object Level

6 replies
Last updated: Jan 11, 2023
Hello all, does anyone know why
*[_type == 'hos_chapter' && references(@->_id)][0].slug.current
wouldn't work? I've tried
references(^._id)
too but no joy. Full code...
Jan 9, 2023, 1:10 PM
"relHos": relHos[]{
      "id": @->_id,
      "title": @->title,
      "status": @->meta.status,
      "slug": @->slug.current,
      @->_type == 'hos' => {
        "chapter": *[_type == 'hos_chapter' && references(@->_id)][0].slug.current
      },
    },
Jan 9, 2023, 1:11 PM
chapter
is returning
null
Jan 9, 2023, 1:16 PM
Also, just to confirm, hardcoding a relevant id does return the chapter, so its something to do with the
@->
or
^.
that's causing the issue.
Jan 9, 2023, 1:27 PM
It’ll probably be more performant if you expand the reference on the object level:
relHos[]->{
  _id,
  title,
  "status": meta.status,
  _type == "hos" => {
    "chapter":  *[_type == "hos_chapter" && references(^._id)]
    
  }
}
I’d think in this case that
^._id
should work for you, however you may have to go up another level (
^.^._id
) because of the conditional.
Jan 10, 2023, 9:47 PM
Thanks
user M
- I did actually implement that technique afterwards, not sure why I had it like that initially. And thanks, all sorted and working now 😊
Jan 11, 2023, 9:50 AM
Maybe changed things around when you were debugging? I’ve done that before! 🙂
Jan 11, 2023, 3:56 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?