mirror of
https://github.com/sergeyyarkov/todo-app.git
synced 2026-10-11 10:29:14 +03:00
32 lines
903 B
JavaScript
32 lines
903 B
JavaScript
const { Category, Todo } = require('../models')
|
|
|
|
const categoryResolvers = {
|
|
Query: {
|
|
categories: () => Category.find({}),
|
|
category: (_, { id }) => Category.findById(id),
|
|
},
|
|
Mutation: {
|
|
createCategory: (_, { title, description }) => {
|
|
const category = new Category({
|
|
title,
|
|
description,
|
|
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, description }) => Category.findByIdAndUpdate(id, { $set: { title, description, updatedAt: new Date().toISOString() } }, { new: true })
|
|
},
|
|
Category: {
|
|
todos: ({ id }) => Todo.find({ categoryId: id }),
|
|
},
|
|
}
|
|
|
|
module.exports = {
|
|
categoryResolvers,
|
|
}
|