|
1 | 1 | ---
|
2 | 2 | title: "7. Update data with mutations"
|
3 |
| -description: Start here for the Apollo fullstack tutorial |
| 3 | +description: Learn how to update data with the Mutation component |
4 | 4 | ---
|
5 | 5 |
|
| 6 | +With Apollo Client, updating data from a graph API is as simple as calling a function. Additionally, the Apollo Client cache is smart enough to automatically update in most cases. In this section, we'll learn how to use the `Mutation` component from `react-apollo` to login a user. |
| 7 | + |
6 | 8 | <h2 id="query-component">What is a Mutation component?</h2>
|
7 | 9 |
|
| 10 | +The `Mutation` component is another important building block in an Apollo app. It's a React component that provides a function to execute a GraphQL mutation. Additionally, it tracks the loading, completion, and error state of that mutation. |
| 11 | + |
| 12 | +Updating data with a `Mutation` component from `react-apollo` is very similar to fetching data with a `Query` component. The main difference is that the first argument to the `Mutation` render prop function is a **mutate function** that actually triggers the mutation when it is called. The second argument to the `Mutation` render prop function is a result object that contains loading and error state, as well as the return value from the mutation. Let's see an example: |
| 13 | + |
8 | 14 | <h2 id="fetch-data">Update data with Mutation</h2>
|
9 | 15 |
|
10 |
| -<h2 id="pagination">Update the Apollo cache</h2> |
| 16 | +The first step is defining our GraphQL mutation. To start, navigate to `src/pages/login.js` and copy the code below so we can start building out the login screen: |
| 17 | + |
| 18 | +_src/pages/login.js_ |
| 19 | + |
| 20 | +```js |
| 21 | +import React from 'react'; |
| 22 | +import { Mutation, ApolloConsumer } from 'react-apollo'; |
| 23 | +import gql from 'graphql-tag'; |
| 24 | + |
| 25 | +import LoginForm from '../components/login-form'; |
| 26 | + |
| 27 | +const LOGIN_USER = gql` |
| 28 | + mutation login($email: String!) { |
| 29 | + login(email: $email) |
| 30 | + } |
| 31 | +`; |
| 32 | +``` |
| 33 | + |
| 34 | +Just like before, we're using the `gql` function to wrap our GraphQL mutation so it can be parsed into an AST. We're also importing some components that we'll use in the next steps. Now, let's bind this mutation to our component by passing it to the `mutation` prop: |
| 35 | + |
| 36 | +_src/pages/login.js_ |
| 37 | + |
| 38 | +```js |
| 39 | +export default function Login() { |
| 40 | + return ( |
| 41 | + <Mutation mutation={LOGIN_USER}> |
| 42 | + {(login, { data }) => <LoginForm login={login} />} |
| 43 | + </Mutation> |
| 44 | + ); |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Our `Mutation` component takes a render prop function as a child that exposes a mutate function (`login`) and the data object returned from the mutation. Finally, we pass our login function to the `LoginForm` component. |
| 49 | + |
| 50 | +To create a better experience for our users, we want to persist the login between sessions. In order to do that, we need to save our login token to `localStorage`. Let's learn how we can use the `onCompleted` handler on `Mutation` to persist our login: |
| 51 | + |
| 52 | +<h3 id="apolloconsumer">Expose Apollo Client with ApolloConsumer</h3> |
| 53 | + |
| 54 | +One of the main functions of `react-apollo` is that it puts your `ApolloClient` instance on React's context. Sometimes, we need to access the `ApolloClient` instance to directly call a method that isn't exposed by the `react-apollo` helper components. The `ApolloConsumer` component can help us access the client. |
| 55 | + |
| 56 | +`ApolloConsumer` takes a render prop function as a child that is called with the client instance. Let's wrap our `Mutation` component with `ApolloConsumer` to expose the client. Next, we want to pass an `onCompleted` callback to `Mutation` that will be called once the mutation is complete with its return value. This callback is where we will save the login token to `localStorage`. |
| 57 | + |
| 58 | +In our `onCompleted` handler, we also call `client.writeData` to write local data to the Apollo cache indicating that the user is logged in. This is an example of a **direct write** that we'll explore further in the next section on local state management. |
| 59 | + |
| 60 | +_src/pages/login.js_ |
| 61 | + |
| 62 | +```js lines=3,4,7-10,16 |
| 63 | +export default function Login() { |
| 64 | + return ( |
| 65 | + <ApolloConsumer> |
| 66 | + {client => ( |
| 67 | + <Mutation |
| 68 | + mutation={LOGIN_USER} |
| 69 | + onCompleted={({ login }) => { |
| 70 | + localStorage.setItem('token', login); |
| 71 | + client.writeData({ data: { isLoggedIn: true } }); |
| 72 | + }} |
| 73 | + > |
| 74 | + {(login, { data }) => <LoginForm login={login} />} |
| 75 | + </Mutation> |
| 76 | + )} |
| 77 | + </ApolloConsumer> |
| 78 | + ); |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +<h3 id="authenticate">Attach authorization headers to the request</h3> |
| 83 | + |
| 84 | +We're almost done completing our login feature! Before we do, we need to attach our token to the GraphQL request's headers so our server can authorize the user. To do this, navigate to `src/index.js` where we create our `ApolloClient` and add the code below to the constructor: |
| 85 | + |
| 86 | +_src/index.js_ |
| 87 | + |
| 88 | +```js lines=5,6 |
| 89 | +const client = new ApolloClient({ |
| 90 | + cache, |
| 91 | + link: new HttpLink({ |
| 92 | + uri: 'http://localhost:4000/graphql', |
| 93 | + headers: { |
| 94 | + authorization: localStorage.getItem('token'), |
| 95 | + }, |
| 96 | + }) |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +Specifying the `headers` option on `HttpLink` allows us to read the token from `localStorage` and attach it to the request's headers each time a GraphQL operation is made. |
11 | 101 |
|
12 |
| -<h2 id="testing">Test Mutation components</h2> |
| 102 | +Now that we know the basics, it's time to dive deeper into mutations. In the next section, we'll practice creating more complex `Mutation` components to manage local state in our app. We'll also learn how to query local data and write a local schema. |
0 commit comments