Mutating data with Apollo Client
We have replaced the way we get data in our client. The next step is to switch the way in which we create new posts, too. Before Apollo Client, we had to add new fake posts to the array of demo posts manually, within the memory of the browser. Now, everything in our text area is sent with the addPost
mutation to our GraphQL API, through Apollo Client.
As with the GraphQL queries, there is a useMutation
Hook that you can use to send a mutation against our GraphQL API. Before, there was also an HOC method and a separate Mutation
component, which have been deprecated as well. They still exist for backward compatibility, but we will not cover them in this book.
Apollo useMutation Hook
The newest version of Apollo Client comes with the useMutation
Hook. The method works equally to the useQuery
Hook—you just need to pass the parsed mutation string to it. In response to that, the useMutation
Hook will return a function equally named to the...