Skip to content

Instantly share code, notes, and snippets.

@rijulg
Created August 26, 2019 02:31
Show Gist options
  • Save rijulg/3c72cf6c97c34b354789285dbf734022 to your computer and use it in GitHub Desktop.
Save rijulg/3c72cf6c97c34b354789285dbf734022 to your computer and use it in GitHub Desktop.
Monitor apollo client network requests globally
import { isNetworkRequestInFlight } from 'apollo-client/core/networkStatus';
/**
* Hooking into the onBroadcast function to save the inFlight data point in the client state
*/
apolloClient.queryManager.onBroadcast = () => {
const { queryManager: { queryStore: { store } } } = apolloClient;
const queries = Object.values(store);
// if we can find a way to escape early that will be better, but regular for loops don't sit well with eslint
const inFlight = queries.reduce(
(final, val) => final || isNetworkRequestInFlight(val.networkStatus),
false,
);
cache.writeData({ data: { inFlight } });
};
extend type Query {
inFlight: Boolean
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment