Skip to content

Commit

Permalink
Unmounting a component before a query is resolved via useLazyQuery
Browse files Browse the repository at this point in the history
…lets the promise resolve instead of aborting the promise. (#10698)

* Restore useLazyQuery to original non-resolved promise.

* Allow pending promises kicked off by useLazyQuery to fully resolve rather than aborting them when unmounting the component

* Ensure the query can be updated in useLazyQuery when rerendering
  • Loading branch information
jerelmiller authored Mar 31, 2023
1 parent 4175af5 commit 38508a2
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 113 deletions.
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
Loading

0 comments on commit 38508a2

Please sign in to comment.