I have an array of references to a country schema type but it always just returns NULL values for me

10 replies
Last updated: Jan 30, 2021
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->. 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 _ref to 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 thread
10 replies
For that to work I would expect your schema to look something like this:

{
  name: "post",
  title: "Posts",
  type: "document",
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
    {
      type: 'array',
      name: 'countries',
      title: 'Countries',
      of: [{
        type: 'object',
        fields: [
          {
            name: 'country',
            title: 'Country',
            type: 'reference',
            to: [{ type: 'countries' }],
          },
        ],
      }],
    },
  ],
}
This is what my schema looks like. Would this be the ‘correct’ way to link multiple countries/categories to a blog post
{
  name: 'countries',
  title: 'Countries',
  type: 'array',
  of: [{ type: 'reference', to: { type: 'country' } }],
}, 
Yes, that should work. But you’re getting null values?
*[_type == "post" && slug.current == 'hello-world'] | order(_updatedAt desc) {
  title,
  'test': countries[]->{title, slug}
}

Aha! That worked. Seems like it should be done differently from what it says in the docs
Could you try that? My example had an array of objects of references. Yours is an array of references.
Great! Glad it worked.
The difference lies in whether the array contains objects or the direct items.
Yea makes sense now. Would you recommend to create an array of objects?
If you’re only linking directly to references, your way should work great. Where you’ll want to use objects is when you need to create arrays of arrays or arrays containing both objects and primitives. Let me find a link here…

https://www.sanity.io/docs/array-type#two-limitations-fNBIr84P
Thanks for sharing that and thanks for helping 🤙

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?