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

Unmounting a component before a query is resolved via useLazyQuery lets the promise resolve instead of aborting the promise. #10698

Merged
merged 15 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/nine-lemons-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@apollo/client': patch
---

Changes the behavior of `useLazyQuery` introduced in [#10427](https://github.com/apollographql/apollo-client/pull/10427) where unmounting a component before a query was resolved would reject the promise with an abort error. Instead, the promise will now resolve naturally with the result from the request.

Other notable fixes:
- Kicking off multiple requests in parallel with the execution function will now ensure each returned promise is resolved with the data from its request. Previously, each promise was resolved with data from the last execution.
- Re-rendering `useLazyQuery` with a different query document will now ensure the execution function uses the updated query document. Previously, only the query document rendered the first time would be used for the request.
13 changes: 10 additions & 3 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,10 +782,10 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);
return this.last;
}

public reobserve(
public reobserveAsConcast(
newOptions?: Partial<WatchQueryOptions<TVariables, TData>>,
newNetworkStatus?: NetworkStatus,
): Promise<ApolloQueryResult<TData>> {
): Concast<ApolloQueryResult<TData>> {
this.isTornDown = false;

const useDisposableConcast =
Expand Down Expand Up @@ -858,7 +858,14 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`);

concast.addObserver(observer);

return concast.promise;
return concast;
}

public reobserve(
newOptions?: Partial<WatchQueryOptions<TVariables, TData>>,
newNetworkStatus?: NetworkStatus,
) {
return this.reobserveAsConcast(newOptions, newNetworkStatus).promise;
}

// (Re)deliver the current result to this.observers without applying fetch
Expand Down