hasura/learn-graphql

Example code for "Delete mutation and update cache" is broken

Philzen opened this issue · 1 comments

Concerning https://github.com/hasura/learn-graphql/blob/master/tutorials/frontend/typescript-react-apollo/tutorial-site/content/optimistic-update-mutations/3.1-mutation-update-cache.md

The update-method cannot work, because todo.id is simply not available in that scope. The default way to do that (as far as i could deduct from the apollo docs) would be to destructure delete_todo like this: update(cache, { data: { delete_todo } }) and then grab the id from there.

Otherwise, this tutorial really made my day, because i had two ground-breaking realisations:

  1. When applying the generated types, as this tutorial page demonstrates, one won't have to worry about changes or typos in the query name (delete_todos) for destructuring – typescript will enforce the right name
  2. actually, the error on this page led me to another discovery: because i was wondering in what scope todo.id could be available for this to work, i tried out what other options could be provided to todoRemove() … and booom: yes, you can supply any option you can give to useMutation there as well. So it is possible to use information in other callbacks that would otherwise never be available in the useMutation-scope, for instance show an alert containing the deleted todo-text onCompleted (The project i'm working on cannot return the complete deleted object after the mutation, therefore the deleted items' title was never available in the callback data, so this is a fantastic workaround)
    This actually solved a challenge i thought was hardly solvable for me! 🔥

Unless i have overlooked something, i can try to rectify the issue. Kindly advise.

I could also expand the tutorial with my two findings if i find the time.

@Philzen - Thanks for reporting the issue. We will get it checked. cc: @arjunyel

And definitely generated types will help solve a lot of user driven errors that come up due to typos, invalid schema, things that actually don't exist due to breaking changes etc.