:wave: Trying to wrap my head around normalized ca...
# apollo-kotlin
d
đź‘‹ Trying to wrap my head around normalized cache and looking for some help: I have a query like
Copy code
query Accounts($filter: Filter) {
    accounts(filter: $filter) {
     id
     type 
     name
   } 
}
where the parameter is a filter and it’s optional. If no filter is given, you get an unfiltered list. The main screen in my app requests this data with
filter: null
so it can get all the accounts. A detail screen requests the data with
filter: {type: OPEN}
. I’m seeing a cache miss when I go to the detail screen. Is there a way for the detail screen to get cached data from the main screen even though the params are different?
I have a CacheKey setup to use the id:
CacheKey("Account:${obj["type"]}:${obj["id"]}")
. I assume there’s something there I need to change but I’m not sure what.
b
Hi! If you give it a different filter, the returned value would usually be different, so you can't really use the cached value, right?
d
hmm, that’s a good point. I guess there’s no way for Apollo to “know” how to filter the data.
b
exactly 🙂
m
You could do something using the low-level
CacheResolver
API
It's going to involve a fair amount of non-trivial code but if you know that
filter: null
is always the same as
filter: {type: OPEN}
, you could handle that programmatically
The default resolveField is doing something like this:
Copy code
val name = field.nameWithArguments(variables)
    if (!parent.containsKey(name)) {
      throw CacheMissException(parentId, name)
    }

    return parent[name]
  }
So if you can make it so that
name
above becomes
accounts(filter: null)
whenever
{type: OPEN}
is passed, data will be looked up from the parent record