Skip to content

Commit 6f094a6

Browse files
committed
Add Delete Mutation
1 parent 1839a58 commit 6f094a6

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ mutation {
5353
teacher
5454
}
5555
}
56+
```
57+
58+
* Mutation Delete
59+
60+
```
61+
mutation {
62+
deleteCourse(_id: "5d49a689956e731fb00367f1")
63+
}
5664
```

lib/mutations.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module.exports = {
6868
{ _id: ObjectID(_id) },
6969
{ $set: input }
7070
)
71-
71+
7272
student = await db.collection('students').findOne(
7373
{ _id: ObjectID(_id) }
7474
)
@@ -77,5 +77,41 @@ module.exports = {
7777
}
7878

7979
return student
80+
},
81+
deleteCourse: async (root, { _id }) => {
82+
let db
83+
let info
84+
85+
try {
86+
db = await connectDb()
87+
88+
info = await db.collection('courses').deleteOne({
89+
_id: ObjectID(_id)
90+
})
91+
} catch (error) {
92+
console.error(error)
93+
}
94+
95+
return info.deletedCount
96+
? `El curso con id ${_id} fue eliminado exitosamente.`
97+
: 'No existe el curso con el id indicado'
98+
},
99+
deleteStudent: async (root, { _id }) => {
100+
let db
101+
let info
102+
103+
try {
104+
db = await connectDb()
105+
106+
info = await db.collection('students').deleteOne({
107+
_id: ObjectID(_id)
108+
})
109+
} catch (error) {
110+
console.error(error)
111+
}
112+
113+
return info.deletedCount
114+
? `El estudiante con id ${_id} fue eliminado exitosamente.`
115+
: 'No existe el estudiante con el id indicado'
80116
}
81117
}

lib/schema.gql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ type Mutation {
5353
createCourse(input: CourseInput!): Course
5454
"Edita un curso"
5555
editCourse(_id: ID!, input: CourseEditInput): Course
56+
"Elimina un curso"
57+
deleteCourse(_id: ID!): String
5658
"Crea un estudiante"
5759
createStudent(input: StudentInput!): Student
5860
"Edita un estudiante"
5961
editStudent(_id: ID!, input: StudentEditInput): Student
62+
"Elimina un estudiante"
63+
deleteStudent(_id: ID!): String
6064
}

0 commit comments

Comments
 (0)