Skip to content

Conversation

@phryneas
Copy link
Member

This bug was brought to my attention by @fernando-apollo. We'll have to discuss how we can best fix this. I bet there are a few cases where this behaviour is really useful.

The bug showcased here is caused by this part of mergeResult:

// Detect the first chunk of a deferred query and merge it with existing
// cache data. This ensures a `cache-first` fetch policy that returns
// partial cache data or a `cache-and-network` fetch policy that already
// has full data in the cache does not complain when trying to merge the
// initial deferred server data with existing cache data.
} else if ("hasNext" in result && result.hasNext) {
const diff = this.getDiff();
result.data = merger.merge(diff.result, result.data);
}

Checklist:

  • If this PR contains changes to the library itself (not necessary for e.g. docs updates), please include a changeset (see CONTRIBUTING.md)
  • If this PR is a new feature, please reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests

that occurs with arrays in deferred queries
@changeset-bot
Copy link

changeset-bot bot commented Nov 20, 2023

⚠️ No Changeset found

Latest commit: e5136c4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Contributor

size-limit report 📦

Path Size
dist/apollo-client.min.cjs 37.08 KB (0%)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" 43.51 KB (0%)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" (production) 42.01 KB (0%)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" 32.61 KB (0%)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" (production) 31.27 KB (0%)
import { ApolloProvider } from "dist/react/index.js" 1.22 KB (0%)
import { ApolloProvider } from "dist/react/index.js" (production) 1.21 KB (0%)
import { useQuery } from "dist/react/index.js" 4.28 KB (0%)
import { useQuery } from "dist/react/index.js" (production) 4.1 KB (0%)
import { useLazyQuery } from "dist/react/index.js" 4.59 KB (0%)
import { useLazyQuery } from "dist/react/index.js" (production) 4.41 KB (0%)
import { useMutation } from "dist/react/index.js" 2.55 KB (0%)
import { useMutation } from "dist/react/index.js" (production) 2.54 KB (0%)
import { useSubscription } from "dist/react/index.js" 2.24 KB (0%)
import { useSubscription } from "dist/react/index.js" (production) 2.2 KB (0%)
import { useSuspenseQuery } from "dist/react/index.js" 4.62 KB (0%)
import { useSuspenseQuery } from "dist/react/index.js" (production) 4.05 KB (0%)
import { useBackgroundQuery } from "dist/react/index.js" 4.14 KB (0%)
import { useBackgroundQuery } from "dist/react/index.js" (production) 3.56 KB (0%)
import { useReadQuery } from "dist/react/index.js" 3 KB (0%)
import { useReadQuery } from "dist/react/index.js" (production) 2.94 KB (0%)
import { useFragment } from "dist/react/index.js" 2.09 KB (0%)
import { useFragment } from "dist/react/index.js" (production) 2.04 KB (0%)

@phryneas phryneas changed the title Add test that shows a bug when merging deffered results for arrays. Add test that shows a bug when merging deferred results for arrays. Nov 20, 2023
@alessbell alessbell self-assigned this Nov 21, 2023
@jerelmiller
Copy link
Member

Converting this to a draft since this contains a reproduction with a failing test.

@jerelmiller jerelmiller marked this pull request as draft July 5, 2024 22:01
@phryneas phryneas linked an issue Jan 6, 2025 that may be closed by this pull request
@uerbin
Copy link

uerbin commented Jul 7, 2025

@phryneas Is this problem solved ?

@phryneas
Copy link
Member Author

phryneas commented Jul 7, 2025

@uerbin It is not solved yet - this code path has been refactored heavily for Apollo Client 4.0 and we won't be able to fix this in time for 4.0, as this will need a lot of thorough testing.
That said, we are making preparations to fix this in a follow-up to 4.0:

With Apollo Client 4.0, the whole defer-merging solution will be abstracted away from the Apollo Client core, so different defer algorithms can be swapped out based on the server implementation. That will allow us to iterate on fixing this much better than would currently be possible.

@jerelmiller
Copy link
Member

Superceded by #12923 which adds these tests to each incremental format.

@jerelmiller jerelmiller deleted the pr/bug-reproduction-deferred-array-merge branch September 15, 2025 23:30
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache inconsistency with @defer and refetch

5 participants