Joint session with Vercel: How to build intelligent storefronts (May 15th)

Query with nested reference works in Vision but returns zero results in Sanity client

23 repliesLast updated: Nov 29, 2025

hello! the query string
 *[_type == "page" && upper(market) == upper($market)
    &&  _id == *[_type == "generalSettings" && upper(market) == upper($market)][0].home->_id
    ]
works correctly inside of sanity studio vision, but when fetching using the sanity client in-app with the exact same string, there are zero results. seems to be stemming from the nested

 _id == *[_type == "generalSettings" && upper(market) == upper($market)][0].home->_id
AI Update

This is a classic perspective mismatch between Vision and your client! The issue is that Vision and your Sanity client are likely using different default perspectives.

The Root Cause

Vision uses the raw perspective by default, which returns ALL documents including both drafts (with drafts. prefix) and published documents. However, as of the API version 2025-02-19, the Sanity client now defaults to the published perspective, which only returns published documents without the drafts. prefix.

When your nested query *[_type == "generalSettings" && upper(market) == upper($market)][0].home->_id runs in Vision, it might be finding a draft version of your generalSettings document (like drafts.generalSettings-123). But when the same query runs through your client with the published perspective, that draft doesn't exist, so the nested query returns nothing, and your comparison fails.

The Solution

You have a few options to fix this:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  perspective: 'previewDrafts', // This prioritizes drafts over published
  useCdn: false, // CDN doesn't support drafts
  apiVersion: '2025-02-19'
})
const client = createClient({
  // ... other config
  perspective: 'raw',
  useCdn: false
})

Understanding the Behavior

The perspectives feature provides different views of your content:

When you dereference with ->, it respects the current perspective. So if you're in published perspective and reference a draft-only document, the dereference returns null, breaking your _id comparison.

Quick Debug Check

To verify this is the issue, temporarily set perspective: 'raw' in your client config. If your query suddenly works, you've confirmed the perspective mismatch!

Show original thread
23 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