Use Apollo client cache for query arguments already requested

I have a schema like this:

type PersonExists {
  id: Int!
  exists: Boolean!
}

type Query {
  people(ids: [Int!]!): [PersonExists!]!
}

Essentially, if I request people(ids: [1, 2, 3]) I am guaranteed to get back Person(id: 1), Person(id: 2) and Person(id: 3).

Therefore, if I have people(ids: [1, 2, 3]) cached and I then query people(ids: [1, 2, 3, 4]) what I would like to do is only fetch people(ids: [4]) from the server and merge the results, as that is the only ID not cached. Likewise, people(ids: [3, 2, 1]) would be cached as all of the Persons are cached.

Happy to clarify if I haven’t illustrated this well. Basically caching on a per argument array value basis, rather than all together.

I was hoping to do this through InMemoryCache or similiar, rather than fiddling with the reading from the cache and changing the query manually. Is such a thing possible?

Hi @oeed, welcome to the community.

AFAIK, the root key for the cache is dependent on the query and the variables. So, you are correct, each query with different/new variables will produce a new cache-key. I do not think there is a way to cache per argument.

Did you look into server-side caching? This could be a good use case to use something like Redis where you can cache by per id (with a TTL that makes sense to you) and that would make your queries faster.