I have an array of references to a country schema type but it always just returns NULL values for me
I can see the issue with your query! The problem is with the syntax countries[].country->. The .country part is trying to access a property, but you're using it before the dereference operator, which won't work correctly.
Based on the GROQ reference access operator documentation, when you have an array of references, you need to apply the dereference operator directly to each item in the array.
Your query should look like this:
*[_type == "post" && slug.current == 'hello-world'] | order(_updatedAt desc) {
title,
'test': countries[]->{ title, slug }
}The key difference is countries[]-> instead of countries[].country->.
Why this works:
countries[]iterates through each item in the array->dereferences each reference (following the_refto fetch the actual document){ title, slug }projects only the fields you want from each referenced country document
If your schema actually has a nested structure where each array item has a country property that contains the reference (like {country: {_ref: "..."}} rather than just {_ref: "..."}), then you'd need:
'test': countries[].country->{ title, slug }But in that case, make sure your schema actually has that intermediate country object. Most commonly, arrays of references are structured as direct references without an intermediate property, which is why the first syntax should work.
Show original thread10 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.