-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Improve handling of arrays in @defer and @stream payloads
#12923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jerelmiller
merged 29 commits into
release-4.1-baseline
from
jerel/array-merge-strategy
Nov 14, 2025
+913
−231
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
0d96e4b
Add array merge strategy to deep merger
jerelmiller ee7da21
Default DeepMerger generic argument
jerelmiller 6da0221
WIP truncate merge arrays
jerelmiller f9b8772
Check length before truncating
jerelmiller 108acd2
Add another test for truncate merge
jerelmiller bd60f05
Add past copies instead of copying twice
jerelmiller b156bef
Inline new merger
jerelmiller 138a110
Move type to namespace
jerelmiller 2c8180f
Use dynamic array merge strategies
jerelmiller b62fe24
Use property type for array merge
jerelmiller 6a39f32
Truncate arrays in defer20220824 handler
jerelmiller 43791bc
Update useBackgroundQuery tests to reflect updated nature on lists
jerelmiller 2cb8e98
Combine array items if merging defer arrays
jerelmiller 4bc8dd9
Make graphql17Alpha9 more like Defer20220824 when determining arrayMe…
jerelmiller 121a646
Update useSuspenseQuery stream tests with updated behavior of list me…
jerelmiller e5cb816
Update spyOnConsole statement
jerelmiller 0857af7
Remove todo in useBackgroundQuery tests
jerelmiller cc80296
Update useQuery stream tests
jerelmiller 37979b9
Remove unneeded arg
jerelmiller 34a29d2
Add test to ensure custom merge function can be used to combine cache…
jerelmiller c826bc3
Add changeset
jerelmiller a506a89
Update size limits
jerelmiller ff289b7
Add tests for refetches with defer arrays
jerelmiller c558fe5
Fix changeset version type
jerelmiller 04dbe02
Print object as array in warning
jerelmiller 1becedf
Add changeset
jerelmiller 0c156f1
Update api report
jerelmiller dd5eccb
Fix tag mix in api report
jerelmiller 059426e
Fix typo
jerelmiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@apollo/client": minor | ||
| --- | ||
|
|
||
| Fix an issue where deferred payloads that returned arrays with fewer items than the original cached array would retain items from the cached array. This change includes `@stream` arrays where stream arrays replace the cached arrays. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@apollo/client": patch | ||
| --- | ||
|
|
||
| Improve the cache data loss warning message when `existing` or `incoming` is an array. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 44194, | ||
| "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 39041, | ||
| "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33526, | ||
| "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27519 | ||
| "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 44386, | ||
| "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 39203, | ||
| "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33554, | ||
| "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27582 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { InMemoryCache } from "@apollo/client/cache"; | |
| import { Defer20220824Handler } from "@apollo/client/incremental"; | ||
| import { ApolloLink } from "@apollo/client/link"; | ||
| import { | ||
| markAsStreaming, | ||
| mockDefer20220824, | ||
| ObservableStream, | ||
| } from "@apollo/client/testing/internal"; | ||
|
|
@@ -163,3 +164,186 @@ test("deduplicates queries as long as a query still has deferred chunks", async | |
| // expect(query5).not.toEmitAnything(); | ||
| expect(outgoingRequestSpy).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it.each([["cache-first"], ["no-cache"]] as const)( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These were added from #11374 (with some tweaks) |
||
| "correctly merges deleted rows when receiving a deferred payload", | ||
| async (fetchPolicy) => { | ||
| const query = gql` | ||
| query Characters { | ||
| characters { | ||
| id | ||
| uppercase | ||
| ... @defer { | ||
| lowercase | ||
| } | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| const { httpLink, enqueueInitialChunk, enqueueSubsequentChunk } = | ||
| mockDefer20220824(); | ||
| const client = new ApolloClient({ | ||
| cache: new InMemoryCache(), | ||
| link: httpLink, | ||
| incrementalHandler: new Defer20220824Handler(), | ||
| }); | ||
|
|
||
| const observable = client.watchQuery({ query, fetchPolicy }); | ||
| const stream = new ObservableStream(observable); | ||
|
|
||
| await expect(stream).toEmitTypedValue({ | ||
| data: undefined, | ||
| dataState: "empty", | ||
| loading: true, | ||
| networkStatus: NetworkStatus.loading, | ||
| partial: true, | ||
| }); | ||
|
|
||
| enqueueInitialChunk({ | ||
| data: { | ||
| characters: [ | ||
| { __typename: "Character", id: 1, uppercase: "A" }, | ||
| { __typename: "Character", id: 2, uppercase: "B" }, | ||
| { __typename: "Character", id: 3, uppercase: "C" }, | ||
| ], | ||
| }, | ||
| hasNext: true, | ||
| }); | ||
|
|
||
| await expect(stream).toEmitTypedValue({ | ||
| data: markAsStreaming({ | ||
| characters: [ | ||
| { __typename: "Character", id: 1, uppercase: "A" }, | ||
| { __typename: "Character", id: 2, uppercase: "B" }, | ||
| { __typename: "Character", id: 3, uppercase: "C" }, | ||
| ], | ||
| }), | ||
| dataState: "streaming", | ||
| loading: true, | ||
| networkStatus: NetworkStatus.streaming, | ||
| partial: true, | ||
| }); | ||
|
|
||
| enqueueSubsequentChunk({ | ||
| incremental: [{ data: { lowercase: "a" }, path: ["characters", 0] }], | ||
| hasNext: true, | ||
| }); | ||
|
|
||
| await expect(stream).toEmitTypedValue({ | ||
| data: markAsStreaming({ | ||
| characters: [ | ||
| { __typename: "Character", id: 1, uppercase: "A", lowercase: "a" }, | ||
| { __typename: "Character", id: 2, uppercase: "B" }, | ||
| { __typename: "Character", id: 3, uppercase: "C" }, | ||
| ], | ||
| }), | ||
| dataState: "streaming", | ||
| loading: true, | ||
| networkStatus: NetworkStatus.streaming, | ||
| partial: true, | ||
| }); | ||
|
|
||
| enqueueSubsequentChunk({ | ||
| incremental: [ | ||
| { data: { lowercase: "b" }, path: ["characters", 1] }, | ||
| { data: { lowercase: "c" }, path: ["characters", 2] }, | ||
| ], | ||
| hasNext: false, | ||
| }); | ||
|
|
||
| await expect(stream).toEmitTypedValue({ | ||
| data: { | ||
| characters: [ | ||
| { __typename: "Character", id: 1, uppercase: "A", lowercase: "a" }, | ||
| { __typename: "Character", id: 2, uppercase: "B", lowercase: "b" }, | ||
| { __typename: "Character", id: 3, uppercase: "C", lowercase: "c" }, | ||
| ], | ||
| }, | ||
| dataState: "complete", | ||
| loading: false, | ||
| networkStatus: NetworkStatus.ready, | ||
| partial: false, | ||
| }); | ||
|
|
||
| void observable.refetch(); | ||
|
|
||
| await expect(stream).toEmitTypedValue({ | ||
| data: { | ||
| characters: [ | ||
| { __typename: "Character", id: 1, uppercase: "A", lowercase: "a" }, | ||
| { __typename: "Character", id: 2, uppercase: "B", lowercase: "b" }, | ||
| { __typename: "Character", id: 3, uppercase: "C", lowercase: "c" }, | ||
| ], | ||
| }, | ||
| dataState: "complete", | ||
| loading: true, | ||
| networkStatus: NetworkStatus.refetch, | ||
| partial: false, | ||
| }); | ||
|
|
||
| // on refetch, the list is shorter | ||
| enqueueInitialChunk({ | ||
| data: { | ||
| characters: [ | ||
| { __typename: "Character", id: 1, uppercase: "A" }, | ||
| { __typename: "Character", id: 2, uppercase: "B" }, | ||
| ], | ||
| }, | ||
| hasNext: true, | ||
| }); | ||
|
|
||
| await expect(stream).toEmitTypedValue({ | ||
| data: markAsStreaming({ | ||
| characters: | ||
| // no-cache fetch policy doesn't merge with existing cache data, so | ||
| // the lowercase field is not added to each item | ||
| fetchPolicy === "no-cache" ? | ||
| [ | ||
| { __typename: "Character", id: 1, uppercase: "A" }, | ||
| { __typename: "Character", id: 2, uppercase: "B" }, | ||
| ] | ||
| : [ | ||
| { | ||
| __typename: "Character", | ||
| id: 1, | ||
| uppercase: "A", | ||
| lowercase: "a", | ||
| }, | ||
| { | ||
| __typename: "Character", | ||
| id: 2, | ||
| uppercase: "B", | ||
| lowercase: "b", | ||
| }, | ||
| ], | ||
| }), | ||
| dataState: "streaming", | ||
| loading: true, | ||
| networkStatus: NetworkStatus.streaming, | ||
| partial: true, | ||
| }); | ||
|
|
||
| enqueueSubsequentChunk({ | ||
| incremental: [ | ||
| { data: { lowercase: "a" }, path: ["characters", 0] }, | ||
| { data: { lowercase: "b" }, path: ["characters", 1] }, | ||
| ], | ||
| hasNext: false, | ||
| }); | ||
|
|
||
| await expect(stream).toEmitTypedValue({ | ||
| data: { | ||
| characters: [ | ||
| { __typename: "Character", id: 1, uppercase: "A", lowercase: "a" }, | ||
| { __typename: "Character", id: 2, uppercase: "B", lowercase: "b" }, | ||
| ], | ||
| }, | ||
| dataState: "complete", | ||
| loading: false, | ||
| networkStatus: NetworkStatus.ready, | ||
| partial: false, | ||
| }); | ||
|
|
||
| await expect(stream).not.toEmitAnything(); | ||
| } | ||
| ); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm marking this as a minor change. While it does fix the array merging behavior, it's a big enough difference to warrant a minor.