GraphQL allDocument query with inline fragment returns "Cannot read properties of undefined (reading 'length')

2 replies
Last updated: Apr 27, 2023
Hello everybody,we are experiencing some issues for the last 2 days with an
allDocument
GraphQL query.This is the original, very basic query:

query getRelated {
	allDocument {
		_id
		_type
	}
}
POSTing this query to
https://[project].<http://api.sanity.io/v1/graphql/[dataset]/default|api.sanity.io/v1/graphql/[dataset]/default>
works just fine.
When we pass it to our bff the query gets rewritten by our GraphQL client (as it always has been in the last months) as follows:

query getRelated {
  allDocument {
    ... on Document {
      _id
      _type
    }
  }
}
This query results in the following error, both if we call our bff or if we call directly Sanity’s GraphQL endpoint:

{
    "errors": [
        {
            "message": "Cannot read properties of undefined (reading 'length')",
            "locations": [
                {
                    "line": 2,
                    "column": 3
                }
            ],
            "path": [
                "allDocument"
            ]
        }
    ],
    "data": null
}
The strange thing is that we didn’t change anything our side, and we haven’t experienced any issue since the last Tuesday (when suddenly this errors started). We have multiple brands with shared code but different Sanity accounts, and everyone of them has the same exact problem on all datasets. We tried with a different GraphQL client (Apollo client instead of
graphql-js
+
cross-fetch
) but with no luck (same error).Anybody experiencing the same issue?
AI Update

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:

  1. Something changed in your GraphQL client library version that altered how it rewrites queries
  2. A Sanity GraphQL API update tightened validation around inline fragments
  3. 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 thread
2 replies
Nice. I didn’t know you could use
... on Document {
…I’ll have to try that for handling related content to our various page types.
I use
createSchemaCustomization
and
actions.createTypes
and
schema.buildObjectType
to attach related content of each type, and then access them in the template query like…

relatedPosts {
  ...cardNode
}
relatedAssets {
  ...cardNodeAsset
}
relatedPlaybooks {
  ...cardNodePlaybook
}
The error says the list is empty. You didn’t change the filter conditions?
Hey Josh, thanks for your reply. In fact I didn’t write that query, it was the GraphQL client rewriting our plain query (without
… on Document
) to that one. We investigated further and we found out that it was a single package (
@graphql-codegen/fragment-matcher
) that got updated to v4 and changed the Document Typescript type automatically generated by the codegen - and that broke the
allDocument
query.
We locked the package version to v3 and now everything is back to normal, I honestly found it a bit scary (that library is widely used) but no one else seemed to have any problem so maybe we are overreacting
🙂.

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?