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

23 replies
Last updated: Jan 28, 2026
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:

  1. Set your client to use previewDrafts perspective (recommended for preview/development):
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'
})
  1. Use the raw perspective (if you need to see everything):
const client = createClient({
  // ... other config
  perspective: 'raw',
  useCdn: false
})
  1. Ensure your documents are published - If this is for production, make sure both your page and generalSettings documents are actually published (not just drafts).

Understanding the Behavior

The perspectives feature provides different views of your content:

  • raw - Returns all documents (drafts and published)
  • published - Returns only published documents
  • previewDrafts - Prioritizes drafts when they exist, falls back to published

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

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.

Was this answer helpful?