Using Cached Result between two Queries #3393
-
I have a query "me" which returns the logged in "User" type (based on their authentication token), and I have a "user(id: ID!)" query that returns the "User" type for the user specified by id. When my app starts, it always calls "me" right away. Then in the normal flow of the app, the "user" query is sometimes called with the id of the logged in user. The CacheExchange successfully finds the "User" in the cache and sends it back. However, the requestPolicyExchange says (I think), "i've never seen this operation before, so I am upgrading you". Which results in a stale response and an unnecessary fetch being sent off. I am sure I could find a way to work around this issue in my application layer by only using the "me" Query when I want the current user's info. But, it would be cool to not have to check for that. Was also wondering if I am just fundamentally misunderstanding something. Regards, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You could check whether you are querying resolvers: {
Query: { user: (parent, args, cache) => {
return cache.resolve({ __typename: 'User', id: args.id }) ? cache.keyOfEntity({ __typename: 'User', id: args.id }) : undefined
} }
} This ensures that if the |
Beta Was this translation helpful? Give feedback.
You could check whether you are querying
me
in theshouldUpgrade
method of therequestPolicyExchange
. Apart from that you would have to tell your normalized cache thatuser(id: $id)
gives youuser:id
.This ensures that if the
User:id
is in cache that it will be retrieved from cache, as the normalized cache does not by default assume thatQuery.user(id: $id)
links toUser:id