Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Understand Urql's Document Cache Exchange and Request Policies


    We learn about the default cacheExchange that urql provides. The default cacheExchange is a document cache. Document cache's hash the query and variables as the key as store the result under that key.

    Urql's cacheExchange has multiple request policies:

    • cache-first
    • cache-only
    • network-only
    • cache-and-network

    cache-first is the default request policy.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Ian Jones: Here we have a React app that uses urql as its GraphQL client. We're passing urql url GraphQL endpoint and then the default exchanges. The order of the exchanges in this array matters. An operation will first go through the dedupExchange, and any duplicate requests will be filtered out.

    The operation goes through the cacheExchange, and depending on your requestPolicy, the cacheExchange will return a cached value and then pass the operation off to the fetchExchange. The fetchExchange makes the actual request, and then updates the cacheExchange with any resulting data.

    An urql client has multiple requestPolicies. The default requestPolicy is cache-first. This means that urql will look in its cacheExchange given an operation and if it finds that operation has already fired, it will return the data from the cache and the operation won't be passed to the fetchExchange.

    To demonstrate this, I'll hop over to my app and open the Developer Tools. I'm on the Network's tab. I go to the Next Page. You see the graphql request fired. When I go to the Previous Page, you don't see another graphql request, because it hits the cache and urql has already loaded this data before, so it doesn't pass this operation over to the fetchExchange.

    If I go Next, there's no request fired and then Next, urql hasn't seen this request before. It goes and grabs the data.

    The next request policy is cache-only. Cache-only won't pass the operation over to the fetchExchange no matter what. Right now, I have nothing in the cache, so when I reload, nothing will reload and there will be no requests fired.

    The next request policy is network-only. As it sounds, this passes each operation through all of the exchanges. When I go Next, you'll see the graphql request down there. I'll clear it. Then we go back, and because of this request policy, urql fires a request.

    The last request policy is cache-and-network. For this request policy, urql will overturn anything it finds in the cache immediately, and then pass that operation off to the fetchExchange as well. When the fetchExchange finishes its request, it updates the cache. In here, when we go to Next Page and then Previous Page, you'll see this request fired.

    You would use this request policy if you wanted to present the user with data as fast as possible and then refresh the data when the request finishes.