Querying array of references in Sanity schema and resolving null values

10 replies
Last updated: Jan 28, 2026
I have an array of references to a
country
schema type, does this query look like it should work? It always just returns NULL values for me
*[_type == "post" && slug.current == 'hello-world'] | order(_updatedAt desc) {
  title,
  'test': countries[].country->{title, slug}
}
AI Update

I can see the issue with your query! The problem is with the syntax countries[].country->. You're trying to dereference inside an array, but the .country part is creating a syntax issue.

The correct syntax depends on your schema structure:

If your array items ARE the references directly:

*[_type == "post" && slug.current == 'hello-world'] | order(_updatedAt desc) {
  title,
  'test': countries[]->{title, slug}
}

If your array items are objects that CONTAIN a reference field called country:

*[_type == "post" && slug.current == 'hello-world'] | order(_updatedAt desc) {
  title,
  'test': countries[]{
    country->{title, slug}
  }
}

The key difference is:

  • countries[]-> - Use this when the array items themselves are references
  • countries[]{country->} - Use this when the array items are objects with a country reference field inside

Based on your query mentioning .country, I suspect you have the second structure (objects containing a reference), so the second syntax should work for you.

As explained in the reference access operator documentation, when working with arrays containing references, you need to use the array projection syntax []{} and then apply the dereference operator -> to the reference field within each array item.

The reason you're getting NULL values is that countries[].country-> isn't valid GROQ syntax - the parser doesn't know how to handle the .country part before the dereference operator.

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