File tree Expand file tree Collapse file tree 2 files changed +7
-6
lines changed Expand file tree Collapse file tree 2 files changed +7
-6
lines changed Original file line number Diff line number Diff line change @@ -26,17 +26,18 @@ const findBooksFromDB = async () => {
26
26
27
27
const addBookToDB = async ( bookToAdd ) => {
28
28
// create the connection
29
- const client = await createConnectionToDB ( ) ;
29
+ const client = createConnectionToDB ( ) ;
30
30
try {
31
31
// select the DB and collection
32
32
const db = selectDB ( client , DB_NAME ) ;
33
- const booksFound = await db . collection ( COLLECTION_BOOKS ) . find ( { } ) . toArray ( ) ;
34
- const res = booksFound . length > 0 ? booksFound : [ ] ;
35
- return res ;
33
+ const bookInserted = await db
34
+ . collection ( COLLECTION_BOOKS )
35
+ . insertOne ( bookToAdd ) ;
36
+ return { _id : bookInserted . insertedId , ...bookToAdd } ;
36
37
} catch ( error ) {
37
38
return error . toString ( ) ;
38
39
} finally {
39
- closeConnectionToDB ( client ) ;
40
+ await closeConnectionToDB ( client ) ;
40
41
}
41
42
} ;
42
43
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ const booksTypeDefs = gql`
14
14
15
15
# All the Mutations on Book listed here
16
16
type Mutation {
17
- addBook(name: String!): ID
17
+ addBook(name: String!): Book!
18
18
}
19
19
` ;
20
20
You can’t perform that action at this time.
0 commit comments