concept cache in category graphql

This is an excerpt from Manning's book GraphQL in Action.
This is not a simple process though. There will be records referencing other records and you will be managing a cyclic graph there. Populating and reading the cache will need query traversal. You will probably need to implement a separate layer to handle this cache logic. However, this method will be a lot more efficient than response-based caching.
In addition to the linkattribute, Apollo Client requires thecacheattribute. This attribute is used to specify the cache object that Apollo will use for its store. The default cache is an instance of theInMemoryCacheobject, which will make Apollo Client use the browser’s memory for caching. That’s what most web applications will need to do. This cache flexibility allows an Apollo Client instance to be used with other types of cache. For example, if you need your application’s cached data to be persisted between sessions, you can use a cache object that works on top ofwindow.localstorage.
We can update the cache in either the
NewApproachorTaskPagecomponents. TheuseMutationhook function accepts anupdatefunction (in its second argument) and it invokes that function after a mutation operation is successful. That function receives the cache object and the results object for the mutation operation. For example, here’s how thatupdatefunction can be used with theAPPROACH_CREATEmutation:Listing 10.24. Changes in
web/src/components/NewApproach.jsexport default function NewApproach({ taskId, onSuccess }) { // ·-·-· const [ createApproach, { error, loading } ] = useMutation( APPROACH_CREATE, { update(cache, { data: { approachCreate } }) { if (approachCreate.approach) { // Modify the cache for Task (ID: taskId) // and append the new approachCreate.approach record } }, }, ); // ·-·-· }Apollo’s
cacheobject manages data by the query operations that resolved them. It provides methods likereadQuery,writeQuery, andmodifyto interact with cached data. For this example, we need to use thecache.modifymethod. Here is an example of its basic usage: