Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. // Example ApolloClient config showing a Mutation resolver.
  2. const client = new ApolloClient({
  3. cache: new InMemoryCache(),
  4. resolvers: {
  5. Mutation: {
  6. toggleTodo: (_root, variables, { cache, getCacheKey }) => {
  7. const id = getCacheKey({ __typename: 'TodoItem', id: variables.id })
  8. const fragment = gql`
  9. fragment completeTodo on TodoItem {
  10. completed
  11. }
  12. `;
  13. const todo = cache.readFragment({ fragment, id });
  14. const data = { ...todo, completed: !todo.completed };
  15. cache.writeData({ id, data });
  16. return null;
  17. },
  18. },
  19. },
  20. });
  21.  
  22. // Example Apollo Mutation component using the local Mutation resolver.
  23. const TOGGLE_TODO = gql`
  24. mutation ToggleTodo($id: Int!) {
  25. toggleTodo(id: $id) @client
  26. }
  27. `;
  28. const Todo = ({ id, completed, text }) => (
  29. <Mutation mutation={TOGGLE_TODO} variables={{ id }}>
  30. {toggleTodo => (
  31. <li
  32. onClick={toggleTodo}
  33. style={{
  34. textDecoration: completed ? 'line-through' : 'none',
  35. }}
  36. >
  37. {text}
  38. </li>
  39. )}
  40. </Mutation>
  41. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement