Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example ApolloClient config showing a Mutation resolver.
- const client = new ApolloClient({
- cache: new InMemoryCache(),
- resolvers: {
- Mutation: {
- toggleTodo: (_root, variables, { cache, getCacheKey }) => {
- const id = getCacheKey({ __typename: 'TodoItem', id: variables.id })
- const fragment = gql`
- fragment completeTodo on TodoItem {
- completed
- }
- `;
- const todo = cache.readFragment({ fragment, id });
- const data = { ...todo, completed: !todo.completed };
- cache.writeData({ id, data });
- return null;
- },
- },
- },
- });
- // Example Apollo Mutation component using the local Mutation resolver.
- const TOGGLE_TODO = gql`
- mutation ToggleTodo($id: Int!) {
- toggleTodo(id: $id) @client
- }
- `;
- const Todo = ({ id, completed, text }) => (
- <Mutation mutation={TOGGLE_TODO} variables={{ id }}>
- {toggleTodo => (
- <li
- onClick={toggleTodo}
- style={{
- textDecoration: completed ? 'line-through' : 'none',
- }}
- >
- {text}
- </li>
- )}
- </Mutation>
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement