-
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
Open
jerelmiller
wants to merge
28
commits into
jerel/stream-support
Choose a base branch
from
jerel/array-merge-strategy
base: jerel/stream-support
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+913
−231
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
1a2c04d
Add array merge strategy to deep merger
jerelmiller 92078c6
Default DeepMerger generic argument
jerelmiller 308702a
WIP truncate merge arrays
jerelmiller 67161f6
Check length before truncating
jerelmiller 16bf7bc
Add another test for truncate merge
jerelmiller 8442c84
Add past copies instead of copying twice
jerelmiller 2f00a39
Inline new merger
jerelmiller 05f9085
Move type to namespace
jerelmiller 07567ef
Use dynamic array merge strategies
jerelmiller 21a1928
Use property type for array merge
jerelmiller 296dc9a
Truncate arrays in defer20220824 handler
jerelmiller 156fa22
Update useBackgroundQuery tests to reflect updated nature on lists
jerelmiller 58b5269
Combine array items if merging defer arrays
jerelmiller ede1476
Make graphql17Alpha9 more like Defer20220824 when determining arrayMe…
jerelmiller 64d8536
Update useSuspenseQuery stream tests with updated behavior of list me…
jerelmiller 5d7626a
Update spyOnConsole statement
jerelmiller 32ad13a
Remove todo in useBackgroundQuery tests
jerelmiller ac33578
Update useQuery stream tests
jerelmiller 3715a17
Remove unneeded arg
jerelmiller f9e176b
Add test to ensure custom merge function can be used to combine cache…
jerelmiller 2aa31c7
Add changeset
jerelmiller 6dddd33
Update size limits
jerelmiller 1780c72
Add tests for refetches with defer arrays
jerelmiller f63c2b2
Fix changeset version type
jerelmiller dc712e7
Print object as array in warning
jerelmiller 01cace0
Add changeset
jerelmiller 31e405e
Update api report
jerelmiller c496039
Fix tag mix in api report
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 reteurned 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)( | ||
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.
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.