Last active
February 19, 2019 14:08
-
-
Save hwillson/c8964ec97ac61293127119a6547ac4d6 to your computer and use it in GitHub Desktop.
Apollo Client - 2.5.0 Announcement Post - Mutation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment