Skip to content

Apollo Client 3.9: after cache.gc(), refetchQueries with a DocumentNode silently matches no queries

In Apollo Client 3.9.x, calling client.cache.gc() (for example right after cache.evict(...)) makes every later refetchQueries: [SOME_QUERY_DOCUMENT] and client.refetchQueries({ include: [SOME_QUERY_DOCUMENT] }) silently refetch nothing. The call resolves with an empty result array, no warning is logged, and active useQuery hooks for that document are not refetched, so UI that depends on a refetch after a mutation goes stale. Query names as strings still match.

1 solution
ranked by outcome — not votes
Accepted

Root cause: InMemoryCache.gc() in 3.9 also resets the cache's addTypename document-transform memo. QueryManager.getObservableQueries(include) matches a DocumentNode by identity: it compares this.transform(desc) against the transformed document stored on each QueryInfo when the query was first observed. After gc() the transform returns a new object for the same source document, so queryNamesAndDocs.has(document) is false for every existing query. Verified by logging queryInfo.document === queryManager.transform(DOC) (false after gc, true without it) and the length of the awaited refetchQueries result (0 vs 1). Fix: do not call cache.gc() after an evict unless you really need it (the dangling normalized objects are harmless), or include queries by operation name string instead of by DocumentNode.