Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo GraphQL, is there a way to manipulate the data that is being written to the cache during querying?

According to Apollo GraphQL docs, Apollo Links can go in two directions - client -> server and server -> client:

Apollo Link Control Flow

However, I am not able to find docs or examples regarding links that go from server -> client. My goal is to catch and parse the incoming data that that is going to be stored in cache. This way, I can read a custom parsed data from cache. Is is possible to achieve this?

like image 898
Gasim Avatar asked Aug 24 '19 14:08

Gasim


People also ask

How does Apollo GraphQL cache work?

Apollo Client stores the results of your GraphQL queries in a local, normalized, in-memory cache. This enables Apollo Client to respond almost immediately to queries for already-cached data, without even sending a network request. The Apollo Client cache is highly configurable.

Are GraphQL queries cached?

HTTP caches will not caches POST requests, which means GraphQL is simply not cacheable at the HTTP level. However, GET is indeed a valid way to query a GraphQL server over HTTP. This means that caches could indeed cache GraphQL responses.

What does a GraphQL client usually do before caching the results of a query?

Generally, when caching data, the intuition is to put information that's fetched remotely into a local store from where it can be retrieved later on. With GraphQL, the naive approach would be to simply put the results of GraphQL queries into the store and simply return them whenever the same query is sent.

How do I invalidate cache in Apollo client?

When we have access to the cache object we can call cache. data. delete(key) where key is the key that Apollo is using to store the data for a specific item. And the record will be entirely deleted from the cache.


1 Answers

You can found after afterware example here: https://github.com/apollographql/apollo-link/tree/master/packages/apollo-link-http#afterware-data-manipulation

like image 155
ManUtopiK Avatar answered Oct 20 '22 03:10

ManUtopiK