File tree Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -41,4 +41,21 @@ const addBookToDB = async (bookToAdd) => {
41
41
}
42
42
} ;
43
43
44
- export { findBooksFromDB , addBookToDB } ;
44
+ const deleteBookFromDB = async ( bookToDelete ) => {
45
+ // create the connection
46
+ const client = createConnectionToDB ( ) ;
47
+ try {
48
+ // select the DB and collection
49
+ const db = selectDB ( client , DB_NAME ) ;
50
+ const bookDeleted = await db
51
+ . collection ( COLLECTION_BOOKS )
52
+ . deleteOne ( { _id : convertToObjectId ( bookToDelete . _id ) } ) ;
53
+ return bookDeleted . deletedCount ;
54
+ } catch ( error ) {
55
+ return error . toString ( ) ;
56
+ } finally {
57
+ await closeConnectionToDB ( client ) ;
58
+ }
59
+ } ;
60
+
61
+ export { findBooksFromDB , addBookToDB , deleteBookFromDB } ;
Original file line number Diff line number Diff line change 1
1
// local dependencies
2
- import { findBooksFromDB , addBookToDB } from "../dbRelated/booksDbOps.js" ;
2
+ import {
3
+ findBooksFromDB ,
4
+ addBookToDB ,
5
+ deleteBookFromDB ,
6
+ } from "../dbRelated/booksDbOps.js" ;
3
7
4
8
const resolvers = {
5
9
Query : {
@@ -12,7 +16,9 @@ const resolvers = {
12
16
addBook : ( _ , args ) => {
13
17
return addBookToDB ( args ) ;
14
18
} ,
19
+ deleteBook : ( _ , args ) => {
20
+ return deleteBookFromDB ( args ) ;
21
+ } ,
15
22
} ,
16
23
} ;
17
- // console.log("fromm console", resolvers.Query.getAllBooks());
18
24
export { resolvers } ;
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ const booksTypeDefs = gql`
15
15
# All the Mutations on Book listed here
16
16
type Mutation {
17
17
addBook(name: String!): Book!
18
+ deleteBook(_id: ID!): Int!
18
19
}
19
20
` ;
20
21
You can’t perform that action at this time.
0 commit comments