2

I have a mutation which removes item from the database. I know that one of the possible ways to update the Apollo Cache is to specify an update function which will be called after success response. I also know that there're couple of ways to do it by using cache.modify and cache.readQuery/cache.writeQuery functions.

Examples provided by the Apollo documentations shows how to remove an item from a list. Unfortunately, provided example in an article shows how to update a nested property instead of removing entire object.

Could you help me please to understand how to correctly remove an entire item from the cache?

I use Apollo-Client v3+

This is my part of the code

export const REMOVE_ITEM_MUTATION = gql`
  mutation RemoveItem($itemId: String!) {
    removeItem(input: {
      itemId: $itemId
    }) {
      itemId # Returns an id of successfully deleted object
    }
  }
`
  

const [removeItem, { loading }] = useMutation(
  REMOVE_ITEM_MUTATION,
  {
    update: (cache, result) => {
      // How to use cache.modify? 
    },
    onError: (error) => { /*...*/ },
    onCompleted: (value) => { /*...*/ }
  }
)

P.S. I also confused if cahce.modify function can replace cache.readQuery and cache.writeQuery? I treat it like a new way of modifying the cache starting from Apollo Client 3. Am I right?

Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68

0 Answers0