awslabs / aws-mobile-appsync-sdk-js

JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Apache License 2.0
921 stars 266 forks source link

calling clearStore() or resetStore() on the apollo client does not clear the `cache` key of the `appsync-metadata` key in localstorage #591

Open madmax141 opened 4 years ago

madmax141 commented 4 years ago

Note: If your issue/feature-request/question is regarding the AWS AppSync service, please log it in the official AWS AppSync forum

There seems to be a bug in the implementation of clearStore. through normal use of my application, the appsync-metadata key in localStorage eventually becomes populated with a snapshot (apparent copy) of the cache (this appears to happen after an arbitrary mutation). However, when calling clearStore on the apollo client, which I do when logging out, this snapshot remains while the appsync key is correctly cleared.

I would expect the appsync-metadata key to be also cleared when calling clearStore, I cannot find any way to set the appsync-metadata key to its initial state, but its very important for me to clear all cache data when users log out.

using the latest version of appsync (4.0.1), but also this failed with previous major versions. this happens in all major browsers.

NOTE: i'm not sure if this is intended behavior or not, I would be curious to know why, if thats the case

State when first logging in:

Screen Shot 2020-09-17 at 5 05 26 PM

State when a mutation is made, note the snapshot key in appsync-metadata having the cache key populated

Screen Shot 2020-09-17 at 5 05 55 PM

State after calling clearStore on the apollo client, note appsync storage key is correctly set to an empty object however the appsync-metadata key remains unchanged, with the cache object still populated:

Screen Shot 2020-09-17 at 5 06 35 PM
madmax141 commented 4 years ago

For anyone also experiencing this and wanting to be sure the cache is 100% cleared, the workaround i've discovered is directly mutation the redux store of appsync, then calling clearStore. this ensures that the metadata info doesn't get rehydrated and you have a truly clear cache. Directly mutating a redux store definitely isn't ideal so it would still be nice to have an official solution to this

apolloClient.store.cache.store.getState()['appsync-metadata'].snapshot.cache = {} apolloClient.clearStore()