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

Remove any dangling references when removing records #85

Open
Tracked by #2331
piotr-pawlowski opened this issue Aug 6, 2020 · 4 comments
Open
Tracked by #2331

Remove any dangling references when removing records #85

piotr-pawlowski opened this issue Aug 6, 2020 · 4 comments

Comments

@piotr-pawlowski
Copy link

ApolloStore: does remove(key: CacheKey) method remove ApolloCacheReference field in related records? I mean a mechanism like "ON DELETE CASCADE SET NULL" in RDBMS.

@martinbonnin
Copy link
Contributor

There is ApolloStore.remove(cacheKey: CacheKey, cascade: Boolean). Would that work?

@piotr-pawlowski
Copy link
Author

piotr-pawlowski commented Aug 13, 2020

This method with flag cascade = true looks for children and deletes them with root record (cacheKey).

From SqlNormalizedCache:

 override fun remove(cacheKey: CacheKey, cascade: Boolean): Boolean {
    val result: Boolean = nextCache?.remove(cacheKey, cascade) ?: false
    if (result) {
      return true
    }
    return if (cascade) {
      selectRecordForKey(cacheKey.key)
          ?.referencedFields()
          ?.all { remove(CacheKey(it.key), cascade = true) }
          ?: false
    } else {
      deleteRecord(cacheKey.key)
    }
  }

From cache.sq:

recordForKey:
SELECT key, record FROM records WHERE key=?;

So this method loads the record from database, retrieves referencedFields (children for that loaded record) and deletes them all with given record.

But what about referencing field? There should be a solution to remove references from parent records to provide consistency of database.

For example:

SELECT key, record FROM records WHERE record LIKE '%ApolloCacheReference{<cache key>}%'

And then clear that referencing field from the record by merge method or somehow. Without that solution there is no way to fetch parent data from cache, because there occurs MISS field error (there is still ApolloCacheReference field to a child record that doesn't exists in database).

@martinbonnin martinbonnin changed the title ApolloStore: does "remove(key)" method remove ApolloCacheReference field in related records? 🗄️ Normalized cache: handle dangling references Apr 6, 2022
@martinbonnin
Copy link
Contributor

Hoping to dig into this soon. I'm realizing just now that cascade wasn't a great naming choice as it's about deleting children, not parents like SQL is doing.

apollo-client refers to this as dangling references and handles them with readFunctions.

I think something similar would work. Compared to cascade delete, that'd keep the dangling reference around in case it becomes valid again. I think that could be handy?

I tweaked the name of this issue a bit so that it's more clear in apollographql/apollo-kotlin#2331

@BoD
Copy link
Collaborator

BoD commented Dec 18, 2024

In #83 an API has been added to remove all dangling references.

Keeping this issue open as it may still be useful to have a way to remove them at the same time as remove() rather than having to garbage collect manually at a later time.

@BoD BoD transferred this issue from apollographql/apollo-kotlin Dec 18, 2024
@BoD BoD changed the title Normalized cache: handle dangling references Remove any dangling references when removing records Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants