🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Trouble with GROQ API and query not working in new version

4 replies
Last updated: Apr 15, 2021
Hi everyone! I seem to be having trouble with the new GROQ api. I've got an old query that doesn't seem to be working at all in the new version and I cant seem to figure out how to fix it. Essentially I have an array of a mix of references and objects. Previously I was able to run the following query to follow the reference for specific types and get containing information. Is there any equivalent for this in the new api (2021-03-25+).

menuItems[]{
  _type == "menuPage" => ^->{
    ...,
  },
}
Schema is here as well to show the structure!


fields: [
    {
      name: "menuItems",
      title: "Menu Items",
      type: "array",
      of: [
        {
          title: "Menu Page",
          name: "menuPage",
          icon: MenuPageIcon,
          type: "reference",
          to: [{ type: "menuPage" }],
        },
        {
          title: "Internal Link",
          icon: InternalLinkIcon,
          type: "internalLink",
        },
        {
          title: "External Link",
          icon: ExternalLinkIcon,
          type: "externalLink",
        },
      ],
      validation: (Rule) => Rule.unique(),
    },
  ],
Apr 15, 2021, 6:47 AM
The reason it worked before, I believe, was due to a bug with the parent hat operator
^
 which wouldn't always select the upper scope. I believe you can get it to work if you change your query to using the "this" operator:
@


menuItems[]{
  _type == "menuPage" => @->{
    ...,
  },
}
Read more here:
https://www.sanity.io/changelog?platforms=GROQ#618800c971b3
Apr 15, 2021, 6:59 AM
🙌 That worked perfectly thanks
user Y
! I was just reading through the patch notes and noticed that bug fix but had no idea the
@
operator existed!
Apr 15, 2021, 7:02 AM
🙌 That worked perfectly thanks
user Y
! I was just reading through the patch notes and noticed that bug fix but had no idea the
@
operator existed!
Apr 15, 2021, 7:02 AM
The reason it worked before, I believe, was due to a bug with the parent hat operator
^
 which wouldn't always select the upper scope. I believe you can get it to work if you change your query to using the "this" operator:
@


menuItems[]{
  _type == "menuPage" => @->{
    ...,
  },
}
Read more here:
https://www.sanity.io/changelog?platforms=GROQ#618800c971b3
Apr 15, 2021, 6:59 AM

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?