Skip to content

Instantly share code, notes, and snippets.

@JeffML
Created November 26, 2017 17:24
Show Gist options
  • Save JeffML/9009bc2140a21e4750459d814e45d9af to your computer and use it in GitHub Desktop.
Save JeffML/9009bc2140a21e4750459d814e45d9af to your computer and use it in GitHub Desktop.
Resolver code for AuthorOps
const addBook = (book, authorId) => {
console.log("addBook", book, authorId)
return new Promise((resolve, reject) => {
book.authorId = authorId
books.push(book)
resolve(books.length)
})
}
const removeBook = (book, authorId) => {
return new Promise((resolve, reject) => {
books = books.filter(b => b.ISBN !== book.ISBN && b.authorId === authorId);
resolve(books.length)
})
}
const updateBook = (book, authorId) => {
return new Promise((resolve, reject) => {
let old = books.find(b => b.ISBN === book.ISBN && b.authorId === authorId);
if (!old) {
reject(`Book with ISBN = ${book.ISBN} not found`)
return
}
resolve(Object.assign(old, book))
})
}
const AuthorOps = (authorId) => ({
addBook: ({
input
}) => addBook(input, authorId),
removeBook: ({
input
}) => removeBook(input, authorId),
updateBook: ({
input
}) => updateBook(input, authorId)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment