GROQ query with nested count and references returns empty in Nuxt Sanity Module

6 replies
Last updated: Oct 28, 2022
Hi, I’m having problems with my GROQ query result:
*Problem*:
the “countries” part of my query is ignored on the front and doesn’t return any documents. All “authorities” and “partners” documents are returned correctly. I don’t get an error message on fetching the query on my frontend (like “bad request” or any api/permission related errors).
In Vision it works as expected and all documents that are referenced by a ‘network’ document are returned.

*Query*:

{
  'countries': *[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)],
  'authorities': *[_type == 'network' && subtype == 'authority']|order(title, asc),
  'partners': *[_type == 'network' && subtype == 'partner']|order(title, asc)
}

Frontend (Nuxt 3, Nuxt Sanity):
const query = groq`{
  'countries': *[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)],
  'authorities': *[_type == 'network' && subtype == 'authority']|order(title, asc),
  'partners': *[_type == 'network' && subtype == 'partner']|order(title, asc)
}`
const { data, refresh } = useSanityQuery(query, { lang: locale.value })

Troubleshooting:• The proper documents are returned if I delete the string key and enclosing brackets. Full Query:
*[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)]
• The proper documents are returned if I delete the “count” part:

{
  'countries': *[_type == 'country'],
  'authorities': *[_type == 'network' && type == 'authority']|order(title, asc),
  'partners': *[_type == 'network' && type == 'partner']|order(title, asc)
}
While I’m typing this I got the feeling that this might be an error in the Nuxt Sanity Module I’m using.
AI Update

Based on your excellent troubleshooting, you've identified a real issue that's specific to how the query behaves when executed through the Nuxt Sanity module versus Vision. Let me walk through what's likely happening here.

The Most Likely Cause: API Version and Perspective

The key clue is that your query works in Vision but returns empty results for the countries key on the frontend. Given that your query uses count() with a nested subquery containing references(^._id), this is likely related to which documents are being returned by the API.

As documented in the API version 2025-02-19 release, there was a breaking change where the default perspective switched from raw to published. Vision might be using a different perspective than your frontend client, which would explain why:

  • The simpler queries work (they don't depend on finding references)
  • The query without count() works (it still returns country documents even if the reference check behaves differently)
  • The full query with count(*[_type=='network' && references(^._id)]) returns empty (the nested subquery might not be finding references due to perspective differences)

Solution: Configure Your API Client Perspective

Check your Nuxt Sanity module configuration. You'll want to explicitly set the perspective to ensure consistent behavior:

// nuxt.config.ts
export default defineNuxtConfig({
  sanity: {
    projectId: 'your-project-id',
    dataset: 'your-dataset',
    apiVersion: '2024-03-15', // or your current version
    perspective: 'previewDrafts', // or 'raw' to see all documents including drafts
    useCdn: false, // Important: CDN doesn't support all perspectives
  }
})

Or if you need to set it per-query, you can pass perspective as a parameter to useSanityQuery (check your @nuxtjs/sanity version documentation for the exact syntax).

Why This Affects Your Query Specifically

The references() function checks if documents contain references to a specific ID. When your perspective is set to published, it might not be seeing draft network documents that reference your countries, causing the count() to return 0, which then filters out all countries.

Your troubleshooting confirms this pattern:

  • Without the object wrapper, the query might be processed differently by the module
  • Without count(), the filter isn't excluding anything
  • Vision likely uses a different default perspective that includes drafts

Additional Debugging Steps

  1. Check what's actually being queried: Add this temporarily to see what's happening:

    const query = groq`{
      'countries': *[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)],
      'countriesDebug': *[_type == 'country']{
        _id,
        title,
        "networkCount": count(*[_type=='network' && references(^._id)]),
        "networks": *[_type=='network' && references(^._id)]._id
      }
    }`
  2. Verify your API version: Check which API version your Nuxt Sanity module is using in your config, as different versions have different default behaviors.

  3. Try disabling CDN: If you're using the CDN (useCdn: true), try setting it to false as the CDN may handle complex nested queries differently.

  4. Check the browser network tab: Look at the actual HTTP request being made to the Sanity API to see what URL parameters are being sent (especially perspective).

The fact that removing the object wrapper works suggests there may also be a quirk in how the Nuxt module serializes or processes object-wrapped queries, but the perspective/API version issue is the most likely root cause for why the nested references() check isn't finding the expected documents.

Show original thread
6 replies
Are the network documents referencing the country published?
Yes, and the weird thing is, the exact same query works just fine when used in vision.
after some testing it got even weirder:

This doesn’t work (attribute inside object and
count()
):
{
  'countries': *[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)]
}
But the next two work just fine:
No object, no attribute but
count()

*[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)]
the same query inside another anrelated query:

*[_type == 'network']{
  'countries': *[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)]
}
ALL queries work in vision so there seems to be no mistake. I’m 90% convinced it’s the frontend tools I’m using. I’ll create an issue on the github page
Updating the API version fixed this.
Oh great! What version were you previously on?
I didn’t specify a Version in the js client, so that would be v1 I guess?
Ah, got it. It would default to v1 in that case.

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?