Skip to content

Instantly share code, notes, and snippets.

@peggyrayzis
Last active August 15, 2019 15:08
Show Gist options
  • Save peggyrayzis/1d07e21df9bdf7cfe512464fcaf7a03f to your computer and use it in GitHub Desktop.
Save peggyrayzis/1d07e21df9bdf7cfe512464fcaf7a03f to your computer and use it in GitHub Desktop.
Resolvers for apollo-link-state
export const defaults = { // same as before }
export const resolvers = {
Mutation: {
visibilityFilter: (_, { filter }, { cache }) => {
cache.writeData({ data: { visibilityFilter: filter } });
return null;
},
addTodo: (_, { text }, { cache }) => {
const query = gql`
query GetTodos {
todos @client {
id
text
completed
}
}
`;
const previous = cache.readQuery({ query });
const newTodo = {
id: nextTodoId++,
text,
completed: false,
__typename: 'TodoItem',
};
const data = {
todos: previous.todos.concat([newTodo]),
};
cache.writeData({ data });
return newTodo;
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment