File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11'use strict'
22
3- const { MongoClient } = require ( 'mongo ' )
3+ const { MongoClient } = require ( 'mongodb ' )
44
55const {
66 DB_USER ,
Original file line number Diff line number Diff line change 11'use strict'
22
3- const courses = [
4- {
5- _id : 'oqeded' ,
6- title : 'My title' ,
7- teacher : 'My teacher' ,
8- description : 'A description' ,
9- topic : 'Programming'
10- } ,
11- {
12- _id : 'oqeded2' ,
13- title : 'My title 2' ,
14- teacher : 'My teacher' ,
15- description : 'A description' ,
16- topic : 'Programming'
17- } ,
18- {
19- _id : 'oqeded3' ,
20- title : 'My title 3' ,
21- teacher : 'My teacher' ,
22- description : 'A description' ,
23- topic : 'Programming'
24- }
25- ]
3+ const connectDb = require ( './db' )
4+ const { ObjectID } = require ( 'mongodb' )
265
276module . exports = {
287 Query : {
29- getCourses : ( ) => {
8+ getCourses : async ( ) => {
9+ let db
10+ let courses = [ ]
11+
12+ try {
13+ db = await connectDb ( )
14+ courses = await db . collection ( 'courses' ) . find ( ) . toArray ( )
15+ } catch ( error ) {
16+ console . error ( error )
17+ }
18+
3019 return courses
3120 } ,
32- getCourse : ( root , args ) => {
33- const course = courses . filter ( course => course . _id === args . id )
34- return course . pop ( )
21+ getCourse : async ( root , { id } ) => {
22+ let db
23+ let course
24+
25+ try {
26+ db = await connectDb ( )
27+ course = await db . collection ( 'courses' ) . findOne ( { _id : ObjectID ( id ) } )
28+ } catch ( error ) {
29+ console . error ( error )
30+ }
31+
32+ return course
3533 }
3634 }
3735}
You can’t perform that action at this time.
0 commit comments