Querying array of references in Sanity schema and resolving null values
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 referencescountries[]{country->}- Use this when the array items are objects with acountryreference 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.
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.