apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

Allow `stopPolling` to abort in flight queries.

Open jsamr opened this issue 3 years ago • 0 comments

Why

I'm running in an issue involving polling with cache writes. Let's say I have a network request on Q which returns a collection of type A. This type A is joined with a collection of type B.

type A = {
  id: string;
  bList: B[];
}

A is being polled every 5 seconds. I have a mutation M which creates new instances of B. In the context of a React app, I use the update function of parameters for mutation M to write to the cache of query Q with cache.updateQuery, and append created items of type B to instances of type A.

Because I don't want polling to occur while mutation M is ongoing, I stopPolling right before initiating M. However, since stopPolling doesn't abort in flight queries, it will sometimes happen that Q cache writes are overridden by a query Q initiated before polling was stopped, causing UI inconsistencies.

Proposed solution

  • Add an abortInFlightQueries flag to stopPolling function;
  • Add the ability to configure abortInFlightQueriesWhenPollingStops in useXQuery hook.
const { data, loading, stopPolling } = useXQuery({
  fetchPolicy: "cache-and-network",
  pollInterval: 3000,
  abortInFlightQueriesWhenPollingStops: true,
  variables: { /* ... */ }
});

// ...

stopPolling({ abortInFlightQueries: true });

Related Feature Requests

  • https://github.com/apollographql/apollo-feature-requests/issues/107
  • https://github.com/apollographql/apollo-feature-requests/issues/362

jsamr avatar Oct 11 '22 17:10 jsamr