1// https://www.apollographql.com/docs/tutorial/local-state/
2
3const [mutate, { loading, error }] = useMutation(
4 isBooked ? CANCEL_TRIP : TOGGLE_CART,
5 {
6 variables: { launchId: id },
7 refetchQueries: [
8 {
9 query: GET_LAUNCH_DETAILS,
10 variables: { launchId: id },
11 },
12 ]
13 }
14 );
1const UPDATE_TODO = gql`
2 mutation UpdateTodo($id: String!, $type: String!) {
3 updateTodo(id: $id, type: $type) {
4 id
5 type
6 }
7 }
8`;
9
10function Todos() {
11 const { loading, error, data } = useQuery(GET_TODOS);
12 const [updateTodo] = useMutation(UPDATE_TODO);
13
14 if (loading) return <p>Loading...</p>;
15 if (error) return <p>Error :(</p>;
16
17 return data.todos.map(({ id, type }) => {
18 let input;
19
20 return (
21 <div key={id}>
22 <p>{type}</p>
23 <form
24 onSubmit={e => {
25 e.preventDefault();
26 updateTodo({ variables: { id, type: input.value } });
27 input.value = '';
28 }}
29 >
30 <input
31 ref={node => {
32 input = node;
33 }}
34 />
35 <button type="submit">Update Todo</button>
36 </form>
37 </div>
38 );
39 });
40}