Skip to content

Instantly share code, notes, and snippets.

@hwillson
Last active February 19, 2019 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwillson/c8964ec97ac61293127119a6547ac4d6 to your computer and use it in GitHub Desktop.
Save hwillson/c8964ec97ac61293127119a6547ac4d6 to your computer and use it in GitHub Desktop.
Apollo Client - 2.5.0 Announcement Post - Mutation
// 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