GROQ query with nested count and references returns empty in Nuxt Sanity Module
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
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 } }`Verify your API version: Check which API version your Nuxt Sanity module is using in your config, as different versions have different default behaviors.
Try disabling CDN: If you're using the CDN (
useCdn: true), try setting it tofalseas the CDN may handle complex nested queries differently.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 thread6 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.