Search code examples
react-apolloapollo-client

How to get all cache data using reactjs @apollo/client v3


Is there anyway that I can check all the cache, eg: changes within apollo for debugging. Something like redux store, where you can view the whole state tree.

They mentioned:

The cache stores the objects by ID in a flat lookup table. https://www.apollographql.com/docs/react/caching/cache-configuration/

Any way to display/console the whole lookup table?


Solution

  • For @apollo/client v3

    Found the answer, if anyone is interested.

    1. Through InMemoryCache

    You can console log the cache object where you create with InMemoryCache. You should be able to find it under your created cache:

    const cache = new InMemoryCache({"...Your option"})
    console.log(cache.data) // <- Your cache query
    
    1. Through browser console

    Through browser, use console to log data

    __APOLLO_CLIENT__.cache.data
    
    1. Through apollo v3

    Access through apollo client cache

    const client = useApolloClient();
    const serializedState = client.cache.extract();
    console.log(serializedState) <- your cache query