Last active
August 15, 2019 15:08
-
-
Save peggyrayzis/1d07e21df9bdf7cfe512464fcaf7a03f to your computer and use it in GitHub Desktop.
Resolvers for apollo-link-state
This file contains hidden or 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
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