Created
January 17, 2017 22:40
-
-
Save HERRKIN/ede9a5b1381ecd491a4cf42351a4d83d to your computer and use it in GitHub Desktop.
toggleCompleted 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
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