Cache update methods are really hard to write and require tons of boilerplate from users {code} (async () => { const result = await client.mutate({ mutation: gql(createTodo), variables: { input: { name: 'Use', description: 'Realtime and Offline', } }, optimisticResponse: () => ({ createTodo: { __typename: 'Todo', // This type must match the return type of the query below (listTodos) id: uuid(), name: 'Use', description: 'Realtime and Offline', } }), update: (cache, { data: { createTodo } }) => { const query = gql(listTodos);
// Read query from cache const data = cache.readQuery({ query });
// Add newly created item to the cache copy data.listTodos.items = [ ...data.listTodos.items.filter(item => item.id !== createTodo.id), createTodo ];
//Overwrite the cache with the new results cache.writeQuery({ query, data }); } }); {code}
We can provide helper that will simplify writing cache to look something like
{code} (async () => { const result = await client.mutate(buildMutation(client, gql(createTodo), { inputType: gql(CreateTodoInput), variables: { input: { name: 'Use', description: 'Realtime and Offline', } } }, (_variables) => [ gql(listTodos) ], 'Todo'));
console.log(result); })(); {code} |
|