How to cache depends on variables

I have query looks like

query Bookmarks($itemId: ID){
  bookmarks(itemId: $itemId) {

which returns like

  data: {
    bookmarks: [
        id: 1, // bookmark id
        hasTargetItem: false // indicates this bookmark includes specified item id 

I’d like to cache query response that depends on $itemId. which means if requested this query with itemId 1 for the first time and then requested again but time with itemId 1, which results in it will only returns cached data. although request with itemId 2 to creates new request.
Is there any way to do this?


Are you using the Apollo Client library (React, Android, or iOS)? They all have caching support built-in. The cache key is calculated based on the query and the arguments.

Here is an example flow that describes exactly the behavior that you want - Caching in Apollo Client - Client (React) - Apollo GraphQL Docs

I’m using React Apollo Client.

Yes I know it have built-in caching support but seems like it caches based on TypeName and its id.
I would like to cache it with key looks like TypeName:ID:<Variable> ( replaced with variable value. lets say itemId for this example)

I thought I could do it by creating local only field with read function in type policies but seems like keyFields doesn’t work with local only field.

Is there any way to do this? Thanks.