apollo-feature-requests
apollo-feature-requests copied to clipboard
Logging queries which only hit the cache
It's easy enough to log over the network queries by adding an apollo link to your apollo client0. It would be helpful though to have a way to apply the same sort of logging to queries that get resolved at the cache level. As far as I could tell while perusing the API docs, there's no way to do this currently. A similar StackOverflow question also has no meaningful answers as of now.
While the dev tools are useful for this purpose, they're not yet available for react-native (though hopefully will be soon!) Even if the dev tools supported all platforms though, it still would be useful to be able to observe cached queries in order to be able to perform side effects.
Not sure of the ideal solution, but I imagine some sort of pre-cache link-like flow would be appropriate.
0: Something like the following code works well for logging queries that get sent over the network:
const loggingLink = new ApolloLink((operation, forward) => {
if (forward == null) {
throw new Error('Must not be a terminating link');
}
const startTime = Date.now();
const currentRequestNum = this.loggingLinkCount;
this.loggingLinkCount += 1;
console.debug(`GraphQL Request #${currentRequestNum}`, { query: print(operation.query) });
return forward(operation).map(data => {
const duration = Date.now() - startTime;
console.debug(`GraphQL Response #${currentRequestNum}`, `Duration: ${duration} ms`, {
data,
});
return data;
});