How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Query multiple document types with conditional projections returning null

11 repliesLast updated: Nov 29, 2025

Hey all, I'm trying to grab data from two different documents with one query. From my understanding of the documentation this should be possible by doing something like
// Specify sets of projections for different content types in an array
content[]{
  _type == 'type1' => {
    // Your selection of fields for type1
  },
  _type == 'type2' => {
    // Your selection of fields for type2
    "url": file.asset->url // Use joins to get data of referenced document
  }
}
However, when I do this I get
null
I'll put my actual queries in the comments.Thanks,
AI Update

Looking at your query, you're very close! The syntax you're trying to use is correct - conditional projections with the _type == 'value' => pattern is indeed the right approach for querying arrays with multiple document types.

Based on the example from the Query Cheat Sheet, here's the correct syntax:

*[_type == "resourcesPage"][0] {
  mainHeading,
  subHeading,
  bodyText,
  resourceItems[]-> {
    _type == "youTube" => {
      _type,
      title,
      url,
      description,
      thumbnail
    },
    _type == "blogPost" => {
      _type,
      title,
      summary,
      mainImage,
      author->{ name, avatar },
      "tags": tags[]-> tagName,
      "slug": slug.current
    }
  }
}

The key points about conditional projections:

This approach is much cleaner than:

The conditional projection syntax gives you exactly what you need: an array of mixed document types, each with their own specific fields, in the original order. This is part of GROQ's projection features that allow you to transform and reshape data based on conditions.

If you're getting null as a result, double-check that:

You can test this step-by-step in Vision by first querying just resourceItems[]-> to see what the dereferenced documents look like, then adding the conditional projections.

Show original thread
11 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions