Skip to content

Instantly share code, notes, and snippets.

@HERRKIN
Created January 17, 2017 22:40
Show Gist options
  • Save HERRKIN/ede9a5b1381ecd491a4cf42351a4d83d to your computer and use it in GitHub Desktop.
Save HERRKIN/ede9a5b1381ecd491a4cf42351a4d83d to your computer and use it in GitHub Desktop.
toggleCompleted Mutation
import {Todos} from '../../../../lib/Collections'
export default function (root, options, context) {
console.log(options)
if (!context.userId) {
throw new Error('Unknown User (not logged in)')
}
// validate the todo belongs to the user
const todo = Todos.findOne({$and: [{_id: options.todoId}, {owner: context.userId}]})
// if not found throw Error
if (!todo) {
throw new Error('Todo not found on the user')
}
const todoId = Todos.update(todo._id, {$set: {completed: !todo.completed}})
if (todoId) {
todo.completed = !todo.completed
console.log(todo)
return todo // return the todo
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment