Unlock seamless workflows and faster delivery with our latest releases- get the details

Issue with reference fields not returning expected data in GROQ query.

13 replies
Last updated: Oct 26, 2022
Hey! I am struggling with following reference fields when they are two arrays deep. I have this query:
*[_type == "landingPage" && slug.current == "get-started"][0]{

    landingPageBuilder[] {
      _type == 'industries' => {
          industries[]->{title}
      },
    ...
    }
  }
I would expect that each of the industries (which are an array of references) would be returned with just the
title
. But I only get back the
_ref
,
_key
, and
_type
without the title. Can anyone spot what I am doing wrong?
Oct 24, 2022, 3:37 PM
Hi Todd. To be sure it’s not overriding your conditional, could you please try moving the
before the
_type == ‘industries’ => {
line?
Oct 25, 2022, 2:20 AM
Ah you are right - it was the spread that was preventing it work! Thanks
user A
Oct 25, 2022, 8:21 AM
Hey
user A
So the spread seems to break a few things – for example this works:
landingPageBuilder[] {
        _type == 'industries' => {
         industries[]->{title,summary,headline,icon},
         ...
       },
       _type == 'organisationalRoles' => {
         roles[]{
           role->,

         },
       }
    }
But this doesn't - so how might I get the rest of the fields in the roles[] array?

landingPageBuilder[] {
        _type == 'industries' => {
         industries[]->{title,summary,headline,icon},
         ...
       },
       _type == 'organisationalRoles' => {
         roles[]{
           ..., // how do I make this work without breaking the role-> reference?
           role->,

         },
         ...,
       }
    }
Oct 25, 2022, 9:27 AM
is this a bug, as by the docs it appears you should be able to use the spread operator to catch everything else (like an 'else' statement) for conditionals?
Oct 25, 2022, 9:33 AM
Hi
user A
I added this as an issue in the GROQ github considering it sounds like a bug of some kind? https://github.com/sanity-io/GROQ/issues/97
Oct 26, 2022, 10:00 AM
hello, could you please try this:

..., 
landingPageBuilder[] {
   ...,
   _type == 'industries' => {
         ...,
         industries[]->{title, summary, headline, icon},
       },
       _type == 'organisationalRoles' => {
         ...,
         roles[]{..,role->},
       }
    }

Oct 26, 2022, 1:11 PM
(and I will answer your additional questions as soon as a get a bit breathing room, had a tight schedule for some pushes 😅 )
Oct 26, 2022, 1:12 PM
Ah that works,
user J
, thank you... But why? The only difference i can see is that the spread operator catch all is before the reference fields, not after?
Oct 26, 2022, 2:57 PM
Its made by people who don’t love chaos 😉jup you basically say: we’re gonna need everything and then do …
Oct 26, 2022, 4:13 PM
ohh, I was thinking of them like 'else' statement - eg, custom functions for these elements, then after that, catch everything else.
Oct 26, 2022, 4:16 PM
Hi Todd. There’s some explanation here that I hope helps.
Oct 26, 2022, 4:17 PM
Thanks
user A
. I'm off for a much needed beer after a long day, but will read through that properly with a fresh mind tomorrow morning 🙂
Oct 26, 2022, 4:21 PM
I noticed your note when you closed the issue, which essentially aligns with that link. Still worth a read, IMO, but in the meantime, enjoy that beer!
Oct 26, 2022, 4:23 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?