Skip to content
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

Handle changed suspense query options #11025

Merged
merged 18 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ten-cherries-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': patch
---

`useSuspenseQuery` and `useBackgroundQuery` will now properly apply changes to its options between renders.
2 changes: 1 addition & 1 deletion .size-limit.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const checks = [
{
path: "dist/apollo-client.min.cjs",
limit: "37750"
limit: "37925"
},
{
path: "dist/main.cjs",
Expand Down
7 changes: 7 additions & 0 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,13 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`,
return this.reobserve(newOptions);
}

public silentSetOptions(
newOptions: Partial<WatchQueryOptions<TVariables, TData>>,
) {
const mergedOptions = compact(this.options, newOptions || {});
assign(this.options, mergedOptions);
}

/**
* Update the variables of this observable query, and fetch the new results
* if they've changed. Most users should prefer `refetch` instead of
Expand Down
39 changes: 39 additions & 0 deletions src/react/cache/QueryReference.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { equal } from '@wry/equality';
import type {
ApolloError,
ApolloQueryResult,
Expand Down Expand Up @@ -33,6 +34,15 @@ interface InternalQueryReferenceOptions {
autoDisposeTimeoutMs?: number;
}

const OBSERVED_CHANGED_OPTIONS: Array<keyof WatchQueryOptions> = [
'canonizeResults',
'context',
'errorPolicy',
'fetchPolicy',
'refetchWritePolicy',
'returnPartialData',
];

export class InternalQueryReference<TData = unknown> {
public result: ApolloQueryResult<TData>;
public readonly key: CacheKey;
Expand Down Expand Up @@ -102,6 +112,35 @@ export class InternalQueryReference<TData = unknown> {
return this.observable.options;
}

didChangeOptions(watchQueryOptions: WatchQueryOptions) {
return OBSERVED_CHANGED_OPTIONS.some(
(option) =>
!equal(this.watchQueryOptions[option], watchQueryOptions[option])
);
}

applyOptions(watchQueryOptions: WatchQueryOptions) {
const { fetchPolicy: currentFetchPolicy } = this.watchQueryOptions;

// "standby" is used when `skip` is set to `true`. Detect when we've
// enabled the query (i.e. `skip` is `false`) to execute a network request.
if (
currentFetchPolicy === 'standby' &&
currentFetchPolicy !== watchQueryOptions.fetchPolicy
Comment on lines +128 to +129
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also trigger if the user manually sets a fetchPolicy of "standby" and changes away from that. Is this really the intended behaviour in that case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a validation in the hook that ensures users can't set standby directly:

function validateFetchPolicy(
fetchPolicy: WatchQueryFetchPolicy = 'cache-first'
) {
const supportedFetchPolicies: WatchQueryFetchPolicy[] = [
'cache-first',
'network-only',
'no-cache',
'cache-and-network',
];
invariant(
supportedFetchPolicies.includes(fetchPolicy),
`The fetch policy \`%s\` is not supported with suspense.`,
fetchPolicy
);
}

Agreed though its not super apparent looking at this directly (which means we have an implicit dependency here). But even if we let that be the case (something I've considered), I would agree this should still the same behavior. "standby" is like a "wait to fetch" fetch policy, so we'd want to execute a request as soon as the user switches away from it. In this way, standby is like skip: true (which is exactly how skip is implemented!).

) {
this.promise = this.observable.reobserve(watchQueryOptions);
} else {
this.observable.silentSetOptions(watchQueryOptions);

// Maintain the previous result in case the current result does not return
// a `data` property.
this.result = { ...this.result, ...this.observable.getCurrentResult() };
this.promise = createFulfilledPromise(this.result);
}

return this.promise;
}

listen(listener: Listener<TData>) {
// As soon as the component listens for updates, we know it has finished
// suspending and is ready to receive updates, so we can remove the auto
Expand Down
Loading