Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache migrations #3422

Closed
remigijusbalc opened this issue Aug 2, 2024 · 2 comments
Closed

Cache migrations #3422

remigijusbalc opened this issue Aug 2, 2024 · 2 comments
Labels
question Issues that have a question which should be addressed

Comments

@remigijusbalc
Copy link

Question

Hello,
Is there any recommended/official way to migrate existing cache entries?

Example:
User A has persisted query in cache (SQLiteCache) with X fields.
New app version introduces changes to query where some of X fields are removed, some changed and new ones are introduced.

How could previous version be migrated to the latest one so the first access of the cache with changed query fields would not fail and return proper values including migrated ones?

@remigijusbalc remigijusbalc added the question Issues that have a question which should be addressed label Aug 2, 2024
@AnthonyMDev
Copy link
Contributor

Thanks for the question @remigijusbalc.

The cache is not really intended to be a complete data persistence option. It's not an alternative to something like CoreData/SwiftData. It's meant to just be a cache of fetched data, which is usually temporary and expires. When things change, you should probably just be refetching them from your GraphQL service and letting that re-write new data into the cache.

If you really need to mutate the data in the cache for a migration, you can use a local cache mutation as defined here in our docs. But it's going to be a pretty manual process that you're going to have to put a lot of thought and care into getting right. There isn't an easy or automatic way that's built in to apply migrations based on a change set in a GraphQL schema or anything like that.

Also, if new fields are added to a query, data for those fields is just not going to be in the cache yet, so your query is going to get a cache miss and need to be refetched the first time. If you somehow know what the values of those fields should be, you could maybe run a local cache mutation on app startup that could write that data to the cache I suppose.

Alternatively, if you want the existing data to appear from the cache and then have the new data loaded, you could try using a deferred fragment . This is a new feature and is still in its experimental phase, but you can try it out! A deferred fragment is able to be returned later after the non-deferred parts of a query. So if all of the non-deferred parts of the query are in the cache, you would get a cache hit, and the deferred fragments would just not be there yet. Then we would do a network fetch and then update the data as it comes in.

I hope that helps. As I don't believe there is much in the way of an actionable work item for us from this issue, I'm going to close the issue. If you have more specific details of your use case, you can still comment on this issue and we may be able to help point you in the right direction for a solution.

@AnthonyMDev AnthonyMDev closed this as not planned Won't fix, can't repro, duplicate, stale Aug 2, 2024
Copy link
Contributor

github-actions bot commented Aug 2, 2024

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that have a question which should be addressed
Projects
None yet
Development

No branches or pull requests

2 participants