Structured Content 2022: Join our conference to explore fresh perspectives on content and digital experiences →

I’m having some trouble figuring out how to query inside references still.

9 replies
Last updated: Jan 14, 2021

I’m having some trouble figuring out how to query inside references still.

Jan 14, 2021, 3:08 AM

If I query this:

*[_type == "darkroomphoto"] {
  aperture,
  photo
}
and I get this:

Jan 14, 2021, 3:08 AM

Weird, I just came here to ask how to do the same from within a filter:

{
      name: "extras",
      title: "Extras",
      type: "array",
      of: [
        {
          type: "reference",
          to: {
            type: "product",
          },
          options: {
            filter: "menu._ref == $menu._id",
            filterParams: {
              menu: "Extras",
            },
          },
        },
      ],
    },

Jan 14, 2021, 3:09 AM

If I run this:

*[_type == "darkroomphoto"] {
  aperture,
  "photo": {
  	asset->
  }
}
It comes up empty like this:

Jan 14, 2021, 3:10 AM

if you use "photo" than you made a custom property, try just photo without quote.

Jan 14, 2021, 3:25 AM

yikes, I’m 2 for 2 on obvious questions now 😅. Thanks

user G
!

Jan 14, 2021, 3:26 AM

Try replacing

"photo": { asset-> }
with
"photo": photo.asset->

Jan 14, 2021, 3:27 AM

Or that works too. 😀

Jan 14, 2021, 3:27 AM

Is it possible to query for a field within a reference in

options: {filter: {}}
? 👀

Jan 14, 2021, 3:28 AM

both worked, but

"photo": photo.asset->
was a bit neater so both suggestions were really helpful

Jan 14, 2021, 3:28 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. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.