Get a peek at our latest innovations at Sanity Product Day on Dec 8th →

In a GROQ query, how do I inline an array of references?

10 replies
Last updated: May 8, 2020

In a GROQ query, how do I inline an array of references? I have this field:

{
  title: "Products",
  name: "products",
  type: "array",
  of: [{
    type: "reference",
    to: [{type: "product"}]
  }]
}
And if I include
products
in the projection I get this back:

[{_key: "546c20518da0",
  _ref: "d415d183-444d-48c3-85c6-59c8e1151d24",
  _type: "reference"}]
If I include
products->
in the projection, I get nothing, same with
products->{title}

May 8, 2020, 11:17 AM

You need to traverse the array, so:

products[]->
Should work

May 8, 2020, 11:18 AM

Thanks! Sorry for bombarding you guys with questions today 🙂

May 8, 2020, 11:24 AM

Keep’em coming! I guess you have seen the cheat sheet and groq.dev?

May 8, 2020, 11:24 AM

Not the latter, I'll check it out

May 8, 2020, 11:25 AM

Would be helpful if this was also mentioned in https://www.sanity.io/docs/array-type

May 8, 2020, 11:25 AM

Good point!

May 8, 2020, 11:25 AM

I did find it on the "how queries work" page now that I knew what to look for, but didn't earlier when I was impatient 😄

May 8, 2020, 11:25 AM

You’re not the first one to stumble in array traversal :)

May 8, 2020, 11:26 AM

An example on the array page would've solved it for me

May 8, 2020, 11:27 AM

Anyway, I'm just happy that there are answers to all my questions. Easy ones, even 🙂

May 8, 2020, 11:27 AM

Sanity.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Free to get started, and pay-as-you-go on all plans. Find out more.