How to edit the property of a cached item - Normalized Cache in Kotlin

Hey, i need to edit a cached item with ApolloStore. (Kotlin on Android)

this is the item in the cache:

        "Post:169" : {
        "__typename" : Post
        "id" : 169
        "name" : test
        "favorites" : 1
    }

How can I increase the “favorites” property by 1 with ApolloStore?
I don’t really understand the docs for ApolloStore

Hi @Aarontells

The easiest way is to define a fragment and operation than contains your new data and call writeOperation with it as explained in Writing Operation data

If you feel more comfortable with Maps and do not need typesafe APIs, you can also use lower level accessCache APIs:

    apolloClient.apolloStore.accessCache {
      val record: Record? = it.loadRecord("Post:169", CacheHeaders.NONE)
      if (record != null) {
        val newFields = record.fields.toMutableMap()
        
        newFields["id"] = ((newFields["id"] as? Int) ?: 0) + 1
        it.merge(Record(record.key, newFields), CacheHeaders.NONE)
      }
    }

Hope this helps!

Hey Martin, thank you so much for your help.
It is working!

Why is this not covered in the official docs?
I couldn’t find anything about apolloStore.accessCache or did I miss something?

It’s low level APIs.
While they are working they come with a lot of potential footguns and it’s not something I would promote more than it actually is.