Created
August 26, 2019 02:31
-
-
Save rijulg/3c72cf6c97c34b354789285dbf734022 to your computer and use it in GitHub Desktop.
Monitor apollo client network requests globally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } }); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extend type Query { | |
inFlight: Boolean | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment