GraphQL allDocument query with inline fragment returns "Cannot read properties of undefined (reading 'length')
This looks like a misunderstanding about how Sanity's GraphQL API works. The error you're encountering is happening because allDocument isn't actually a standard Sanity GraphQL query pattern, and the inline fragment ... on Document is referencing a type that doesn't exist in your schema.
What's actually happening:
Sanity's GraphQL API automatically generates queries based on your specific document types - it creates queries like allPost, allPage, allProduct, etc., but there's no built-in allDocument union type or interface that covers all document types. When your GraphQL client adds the inline fragment ... on Document, it's trying to reference a type that doesn't exist in the generated schema, which is causing the "Cannot read properties of undefined (reading 'length')" error.
The reason your original query without the inline fragment works is likely because Sanity's GraphQL implementation is being lenient and returning results, but when you add the inline fragment referencing a non-existent Document type, it fails during query validation or execution.
Why this suddenly broke:
The timing suggests that either:
- Something changed in your GraphQL client library version that altered how it rewrites queries
- A Sanity GraphQL API update tightened validation around inline fragments
- Your schema deployment changed in a way that affected type generation
Solutions:
1. Query specific document types instead (recommended):
Rather than trying to query all documents at once, query each type you need:
query getRelated {
allPost {
_id
_type
}
allPage {
_id
_type
}
allProduct {
_id
_type
}
}2. Switch to GROQ for this use case:
If you genuinely need to query across all document types, GROQ is better suited for this. The equivalent query would be:
*[]{_id, _type}You can use GROQ alongside your GraphQL queries - many projects use GraphQL for most queries but GROQ for specific use cases where it's more powerful.
3. Prevent your GraphQL client from adding the inline fragment:
Since the original query works, configure your GraphQL client to stop automatically rewriting queries with inline fragments. The specific configuration depends on which client you're using, but both graphql-js and Apollo Client have options to control query transformations.
4. Create a union type in your schema (advanced):
If you really need a GraphQL-based "all documents" query, you could create a union type in your Sanity schema that includes all your document types, then redeploy your GraphQL API with sanity graphql deploy. However, this is more complex and the GROQ approach is simpler.
To debug further:
Check what types are actually available in your GraphQL schema by visiting your GraphQL playground at https://[project].api.sanity.io/v1/graphql/[dataset]/default and looking at the schema documentation. You won't find a Document interface or allDocument query unless you've explicitly created one.
The fact that this is happening across all your brands suggests they all use the same GraphQL client configuration that's doing this rewriting. Focus on either preventing that rewrite or switching to the appropriate query pattern for your schema.
Show original thread2 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.