Add subscription GraphQL

Let's set up the subscription in ShoppingListProvider.

This particular subscription will fire for all types of mutations: CREATED, UPDATED and DELETED because I want to show how to sync these with the local cache. You can obviously choose yourself which mutations you want to handle per subscription.

We are specifying which data we want to receive, this will include the mutation type, the item data as node and any previous values that we're interested in. In this case, I only want to know the id so that in the case of a DELETED mutation, I'll know which item was deleted.

Now let's create a method on the provider to use this subscription and handle the mutations. We use the subscribeToMore method on the query and pass it the subscription GraphQL and a function that gets as input the local cache and the data received from the subscription.

We are only handling the CREATED and DELETED mutations because, as I mentioned in the previous part of this tutorial, we don't need to manually update our local cache for UPDATED mutations. Apollo Client takes care of that for us.

Change the getAllItems method on the provider and add the call to subscribeToChanges.