-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
ObservableQuery.getCurrentResult: skip the cache #10631
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
52c433b
Write failing test to demonstrate nextFetchPolicy is used after compo…
jerelmiller b37bed2
ObservableQuery.currentResult: skip the cache
0a67647
format "ObservableQuery" test
8227e66
add another test for `ObservableQuery` behaviour
2f0175a
appease CI
0e2d94a
Merge remote-tracking branch 'origin/main' into pr/10222
phryneas 66e8c83
fixup test
phryneas 088dc51
re-add test that was lost in merge
phryneas 0206d72
ensure `updateWatch` is called when skipping `getDiff`
phryneas b976e79
Merge remote-tracking branch 'origin/main' into pr/10222
phryneas 96933af
update bundlesize
phryneas 71f9d89
minimize diff
phryneas 28910a3
test readability
phryneas aff4d65
add more tests
phryneas b360183
Update src/core/ObservableQuery.ts
phryneas 6cf89bc
Update src/core/ObservableQuery.ts
phryneas ee8dda5
Merge branch 'main' into pr/10222
phryneas File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 | ||
--- | ||
|
||
ObservableQuery.getCurrentResult: skip the cache if the running query should not access the cache |
This file contains 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,5 +1,6 @@ | ||
# format "ObservableQuery" test | ||
0a67647b73abd94b706242f32b88d21a1400ad50 | ||
# format "ObservableQuery" test (in #10597) | ||
104bf11765b1db50292f9656aa8fe48e2d749a83 | ||
|
||
# Format "DisplayClientError.js" (#10909) | ||
0cb68364f2c3828badde8c74de44e9c1864fb6d4 |
This file contains 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 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 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 |
---|---|---|
|
@@ -28,6 +28,7 @@ import { | |
FetchMoreQueryOptions, | ||
SubscribeToMoreOptions, | ||
NextFetchPolicyContext, | ||
WatchQueryFetchPolicy, | ||
} from './watchQueryOptions'; | ||
import { QueryInfo } from './QueryInfo'; | ||
import { MissingFieldError } from '../cache'; | ||
|
@@ -86,6 +87,7 @@ export class ObservableQuery< | |
private observers = new Set<Observer<ApolloQueryResult<TData>>>(); | ||
private subscriptions = new Set<ObservableSubscription>(); | ||
|
||
private waitForOwnResult: boolean; | ||
private last?: Last<TData, TVariables>; | ||
|
||
private queryInfo: QueryInfo; | ||
|
@@ -152,6 +154,7 @@ export class ObservableQuery< | |
this.queryManager = queryManager; | ||
|
||
// active state | ||
this.waitForOwnResult = skipCacheDataFor(options.fetchPolicy); | ||
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. This has to be initialized in the constructor, as |
||
this.isTornDown = false; | ||
|
||
const { | ||
|
@@ -240,16 +243,19 @@ export class ObservableQuery< | |
if ( | ||
// These fetch policies should never deliver data from the cache, unless | ||
// redelivering a previously delivered result. | ||
fetchPolicy === 'network-only' || | ||
fetchPolicy === 'no-cache' || | ||
fetchPolicy === 'standby' || | ||
skipCacheDataFor(fetchPolicy) || | ||
// If this.options.query has @client(always: true) fields, we cannot | ||
// trust diff.result, since it was read from the cache without running | ||
// local resolvers (and it's too late to run resolvers now, since we must | ||
// return a result synchronously). | ||
this.queryManager.transform(this.options.query).hasForcedResolvers | ||
) { | ||
// Fall through. | ||
// Fall through. | ||
} else if (this.waitForOwnResult) { | ||
// This would usually be a part of `QueryInfo.getDiff()`. | ||
// which we skip in the waitForOwnResult case since we are not | ||
// interested in the diff. | ||
this.queryInfo['updateWatch'](); | ||
phryneas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
const diff = this.queryInfo.getDiff(); | ||
|
||
|
@@ -833,13 +839,22 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`); | |
} | ||
} | ||
|
||
this.waitForOwnResult &&= skipCacheDataFor(options.fetchPolicy); | ||
const finishWaitingForOwnResult = () => { | ||
if (this.concast === concast) { | ||
this.waitForOwnResult = false; | ||
} | ||
}; | ||
|
||
const variables = options.variables && { ...options.variables }; | ||
const { concast, fromLink } = this.fetch(options, newNetworkStatus); | ||
const observer: Observer<ApolloQueryResult<TData>> = { | ||
next: result => { | ||
finishWaitingForOwnResult(); | ||
this.reportResult(result, variables); | ||
}, | ||
error: error => { | ||
finishWaitingForOwnResult(); | ||
this.reportError(error, variables); | ||
}, | ||
}; | ||
|
@@ -990,3 +1005,7 @@ export function logMissingFieldErrors( | |
}`, missing); | ||
} | ||
} | ||
|
||
function skipCacheDataFor(fetchPolicy?: WatchQueryFetchPolicy /* `undefined` would mean `"cache-first"` */) { | ||
return fetchPolicy === "network-only" || fetchPolicy === "no-cache" || fetchPolicy === "standby"; | ||
} |
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 had to add this with some commit - the Jest VSCode plugin cannot display it when two projects run the same test twice, so I just set it to exclude the React17 tests. Also adds
--runInBand
for the extension, which makes tests less flaky in general.