Issue with query string not returning results in Sanity client but working in Sanity Studio Vision
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:
- Set your client to use
previewDraftsperspective (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'
})- Use the
rawperspective (if you need to see everything):
const client = createClient({
// ... other config
perspective: 'raw',
useCdn: false
})- Ensure your documents are published - If this is for production, make sure both your
pageandgeneralSettingsdocuments 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 documentspreviewDrafts- 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!
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.