InitialValue query fails but hardcoded reference works in Sanity schema

33 replies
Last updated: Jan 28, 2026
I have followed the YouTube tutorial by
user Y
on InitialValue.
From there, I personalise it to my case with this query.

*[_type == 'codeList' && code match 'VT' ]{
      
'_type': 'serviceCodeList',
      
'codeList': {
        
'_ref': _id,
        
'_type': 'reference'
      
}
  
}
giving this response:

"result":[1 item

0:{2 items

"_type":"serviceCodeList"
or
"_type":"codeList"
(I'm not sure)
"codeList":{2 items

"_ref":"56d99a7f-bd55-4cd7-9eeb-d5b65fe63d5e"

"_type":"reference"

But, nevertheless that the query is correct from tutorial, that it works in VISION and is giving me the appropriate
_ref
and
_type
(
"56d99a7f-bd55-4cd7-9eeb-d5b65fe63d5e"
), I run into this error:
Failed to resolve initial Value,

Check developer console for details

I know that this (under) in the schema is working, giving me the
VT
initialValue expected in the studio:
initialValue: {

codeList: {

_ref: '56d99a7f-bd55-4cd7-9eeb-d5b65fe63d5e',

_type: 'reference',

}
`},`①
(contrarily to the tutorial it is not
serviceCodeList
with the document name but simply
codeList
that works)

But this is not a QUERY!
Is there something I am doing wrong?
I mean in the tutorial,
user Y
is doing that for an array and I am adapting that to the schema of a document.
Is it an impossible case?!?
(because it's a document not an array?!?) (probably not because it should work also no?)


⚠️ something else:When it succeed with the direct _ref (driving me to the point like above, see ①), the behaviour of the studio is 
STRANGE.
By that I mean that its 
blinking (eg. bumping on the 9 elements of the promise target) to resolve (like hitting something).
It's very surprising, as in the video with 
user Y
 it is very smooth.
Or is there a way to have that done smooth_ly_ 
like normal?Or it's just my code that is clumsy?
If so what should I change?

Any help very much appreciated ☆
AI Update

I can see you're struggling with using a GROQ query to set initial values dynamically. Let me clarify what's happening and provide the correct solution.

The Core Problem

The "Failed to resolve initial value" error occurs because field-level initialValue in your schema doesn't execute GROQ queries. When you write a GROQ query string in initialValue, it's just a string - nothing is actually running that query against your content.

Field-level initialValue can be:

  • A static value
  • A function that returns a value
  • An async function that fetches from external APIs

But it doesn't have built-in access to query your Sanity content with GROQ.

The Correct Solution: Initial Value Templates

To set initial values based on GROQ queries, you need to use Initial Value Templates configured through the templates configuration in your sanity.config.ts:

// sanity.config.ts
import {defineConfig} from 'sanity'

export default defineConfig({
  // ... other config
  templates: (prev) => {
    return [
      ...prev,
      {
        id: 'serviceCodeList-with-vt',
        title: 'Service Code List (VT)',
        schemaType: 'serviceCodeList',
        value: async (params, context) => {
          // Use the context client to query your content
          const client = context.getClient({apiVersion: '2024-11-01'})
          
          // Query for the VT code list - get just the first result's ID
          const result = await client.fetch(
            `*[_type == 'codeList' && code match 'VT'][0]._id`
          )
          
          return {
            codeList: {
              _ref: result,
              _type: 'reference'
            }
          }
        }
      }
    ]
  }
})

Key points about this approach:

  • Templates are defined in the templates configuration in your config file
  • You use context.getClient() to get a Sanity client that can execute GROQ queries
  • Add [0] to get the first result instead of an array
  • Fetch just the _id and construct the reference object in the return value
  • When users create a new document, they'll see "Service Code List (VT)" as an option in the document creation menu

About the "Blinking" Behavior

The strange blinking/bumping behavior you're seeing with the hardcoded _ref suggests a validation or rendering issue. This commonly happens when:

  1. Type confusion - In your query result, you mentioned uncertainty about whether _type should be "serviceCodeList" or "codeList". In a reference object's structure, _type should always be "reference", never the document type name:

    {
      codeList: {
        _ref: '56d99a7f-bd55-4cd7-9eeb-d5b65fe63d5e',
        _type: 'reference'  // Always "reference", not the target document type
      }
    }
  2. Schema mismatch - Verify your schema field is properly defined as a reference type to codeList documents

  3. Validation loops - Studio might be repeatedly fetching the referenced document to validate it exists

The smooth behavior in tutorials comes from using Initial Value Templates properly - they resolve once during document creation, then the value is stored. No repeated fetching or validation loops.

Document vs Array

No, this isn't about document vs array - initialValue works for both. The issue is specifically about query execution context. Field-level initialValue can't execute GROQ queries; you need the template approach with context.getClient().

Alternative: Hardcode It (If Appropriate)

If the VT code list reference rarely changes, your hardcoded approach is actually perfectly valid:

initialValue: {
  codeList: {
    _ref: '56d99a7f-bd55-4cd7-9eeb-d5b65fe63d5e',
    _type: 'reference',
  }
}

This isn't a "query" but it's a legitimate pattern if the reference is essentially constant. The blinking might resolve once you ensure the _type is correctly set to "reference" and save/reopen the document.

My Recommendation

If you need dynamic lookup based on the code field matching "VT", use the Initial Value Templates approach in the templates configuration as shown above. If the VT reference is stable and won't change, the hardcoded field-level initialValue is simpler and perfectly acceptable - just make sure _type: 'reference' is correct and the schema field definition matches.

Show original thread
33 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?