I want to implement custom Apollo Cache on top of existing InMemoryCache. The only thing which I want to change is underlying DataStore class, so instead of using ObjectCache or DepTrackingCache I want to use my custom implementation based on Vue reactivity system.
So, that all changes to cache becomes immediately visible in my Vue components.
If OptimisticCacheLayer class would be stored as a property of InMemoryCache I'd be able to overwrite it in constructor without copying methods (and later synchronizing them with new versions of InMemoryCache). Something like this:
Purpose
I want to implement custom Apollo Cache on top of existing
InMemoryCache
. The only thing which I want to change is underlyingDataStore
class, so instead of usingObjectCache
orDepTrackingCache
I want to use my custom implementation based on Vue reactivity system. So, that all changes to cache becomes immediately visible in my Vue components.Actual issue
All works good, except of optimisticResponse. This is due to the fact that
OptimisticCacheLayer
inheritsObjectCache
(https://github.com/apollographql/apollo-client/blob/master/packages/apollo-cache-inmemory/src/inMemoryCache.ts#L54) and later usage ofOptimisticCacheLayer
is hardcoded insideInMemoryCache
in 2 places (https://github.com/apollographql/apollo-client/blob/master/packages/apollo-cache-inmemory/src/inMemoryCache.ts#L247, https://github.com/apollographql/apollo-client/blob/master/packages/apollo-cache-inmemory/src/inMemoryCache.ts#L284). So, there is no easy way to substitute implementation ofOptimisticCacheLayer
and I need to copy/paste 2 methodsremoveOptimistic
andperformTransaction
fromInMemoryCache
into my implementation just because I want to add reactivity intoOptimisticCacheLayer
class.Solution
If
OptimisticCacheLayer
class would be stored as a property ofInMemoryCache
I'd be able to overwrite it in constructor without copying methods (and later synchronizing them with new versions ofInMemoryCache
). Something like this:And later use
this.OptimisticCacheLayer
instead ofOptimisticCacheLayer