todo-app/server/resolvers/categoryResolvers.js
2020-06-14 21:05:20 +03:00

31 lines
843 B
JavaScript

const { Category, Todo } = require('../models')
const categoryResolvers = {
Query: {
categories: () => Category.find({}),
category: (_, { id }) => Category.findById(id),
},
Mutation: {
createCategory: (_, { title }) => {
const category = new Category({
title,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
return category.save()
},
deleteCategory: async (_, { id }) => {
const deletedCategory = await Category.findByIdAndRemove(id)
return deletedCategory
},
updateCategory: (_, { id, title }) => Category.findByIdAndUpdate(id, { $set: { title, updatedAt: new Date().toISOString() } }, { new: true })
},
Category: {
todos: ({ id }) => Todo.find({ categoryId: id }),
},
}
module.exports = {
categoryResolvers,
}