I have a graphql query response of the shape
{
table {
id
legs {
id
}
}
This normalizes to table and leg entries in my InMemoryCache.
But then if my application retrieves a leg from cache and needs to know the corresponding table, how would I find this?
The two ideas I had are
- adding a
tableprop to eachlegwhen the query response comes in - not sure if/how that would work (I have multiple queries and mutations containing the graphql fragment with above shape) - having a suitable
cache redirect, but I don't know how to do this without searching alltablesfor theleg.
Does apollo provide any features suitable to achieve this inverse lookup?
Update: to clarify, the leg has a table prop, but I since I already have the info in the client before resolving that prop, I'd like to resolve that prop client-side instead of server-side.
You should be adding a
tableprop to eachleg. According to the graphql.org documentation you should be thinking in graphs:In your model, tables and legs are nodes in your business model graph. When you add a table prop to each leg you are creating a new edge in this graph that your client-side code can traverse to get the relevant data.
Edit after clarification:
You can use
writeFragmentand to gain fine grained control of the Apollo cache. Once the cache filling query is done, compute the inverse relationship and write it to the cache like so:Demo
I put up a complete exemple here: https://codesandbox.io/s/6vx0m346z I also put it below just for completeness sake.
index.js
client.js
Films.js
Planet.js