Adding an entity to apollo-client cache after mutation to match query variables

Adding an entity to apollo-client cache after mutation to match query variables


0

I’m currently building a SPA with apollo-graphql and everything is working fine so far. However, I have reached a point where the apollo cache causes some issues.
I have a UI which provides several filter criteria. The fields are mapped to variables in my graphql query, specifically a project ID and a timespan (start/end date). The result of the query is a list of corresponding timesheet entries for the project ID in the provided time frame. Every time the parameters are changed, the graphql query is refetched. Results are stored in the cache, grouped by query name and the given set of variables, so it working as expected.

On the same page, I have a form to add new timesheet entries. New entries are added via a graphql mutation. A timesheet entry is given a project id and a start/enddate. I’m using cache updates (writeQuery) to update the apollo cache to add a new timesheet entry after the mutation was successful, but I only have access to the current set of filter criteria. However, I need to add the newly created entry to all caches (i.e. combination of variables) which might contain the new timesheet entry. Otherwise, the new entry is not shown if the user changes the variables to a combination he/she entered before.

I’ve investigated the cache object already if there is a way to obtain "historic" queries, but it seems that queries executed before are just stringified, so it will be quite hard to identify queries in the cache which match a certian set of variables. If the variables would live as an actual object in the cache it would be way easier, but I could not find anything like this.

At the moment I tend to disable caching completely for this case or use refetchQueries, but I would prefer a more fine-grained approach.

Any suggestions how to deal with such scenarios? I also couldn’t find this particular problem in the docs. The examples mentioned in the docs only focus on queries with no variables.

New contributor

wrldtrvllr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 4 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *