Skip to content

Commit d33aa9c

Browse files
committed
Mutations e Inputs
1 parent b3a0299 commit d33aa9c

7 files changed

Lines changed: 94 additions & 32 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,20 @@
2323
description
2424
}
2525
}
26+
```
27+
28+
* Mutation
29+
30+
```
31+
mutation {
32+
createCourse(input: {
33+
title: "Nuevo curso"
34+
description: "Description new"
35+
topic: "diseño"
36+
}){
37+
_id
38+
title
39+
description
40+
}
41+
}
2642
```

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const typeDefs = readFileSync(
1616
join(__dirname, 'lib', 'schema.gql'),
1717
'utf-8'
1818
)
19-
const schema = makeExecutableSchema({ typeDefs, resolvers})
19+
const schema = makeExecutableSchema({ typeDefs, resolvers })
2020

2121
app.use('/api', gqlMiddleware({
2222
schema: schema,

lib/db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
DB_PASSWORD,
88
DB_HOST,
99
DB_PORT,
10-
DB_NAME,
10+
DB_NAME
1111
} = process.env
1212

1313
const mongoUrl = `mongodb://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}`

lib/mutations.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const connectDb = require('./db')
4+
5+
module.exports = {
6+
createCourse: async (root, { input }) => {
7+
const defaults = {
8+
teacher: '',
9+
topic: ''
10+
}
11+
12+
const newCourse = Object.assign(defaults, input)
13+
let db
14+
let course
15+
16+
try {
17+
db = await connectDb()
18+
course = await db.collection('courses').insertOne(input)
19+
20+
newCourse._id = course.insertedId
21+
} catch (error) {
22+
console.error(error)
23+
}
24+
25+
return newCourse
26+
}
27+
}

lib/querys.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
const connectDb = require('./db')
4+
const { ObjectID } = require('mongodb')
5+
6+
module.exports = {
7+
getCourses: async () => {
8+
let db
9+
let courses = []
10+
11+
try {
12+
db = await connectDb()
13+
courses = await db.collection('courses').find().toArray()
14+
} catch (error) {
15+
console.error(error)
16+
}
17+
18+
return courses
19+
},
20+
getCourse: async (root, { id }) => {
21+
let db
22+
let course
23+
24+
try {
25+
db = await connectDb()
26+
course = await db.collection('courses').findOne({ _id: ObjectID(id) })
27+
} catch (error) {
28+
console.error(error)
29+
}
30+
31+
return course
32+
}
33+
}

lib/resolvers.js

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,9 @@
11
'use strict'
22

3-
const connectDb = require('./db')
4-
const { ObjectID } = require('mongodb')
3+
const querys = require('./querys')
4+
const mutations = require('./mutations')
55

66
module.exports = {
7-
Query: {
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-
19-
return courses
20-
},
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
33-
}
34-
}
7+
Query: querys,
8+
Mutation: mutations
359
}

lib/schema.gql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,16 @@ type Query {
1111
getCourses: [Course]
1212
"Devuelve un curso el simbolo ! define que es obligatorio"
1313
getCourse(id: ID!): Course
14+
}
15+
16+
input CourseInput {
17+
title: String!
18+
teacher: String
19+
description: String!
20+
topic: String
21+
}
22+
23+
type Mutation {
24+
"Crea un curso"
25+
createCourse(input: CourseInput!): Course
1426
}

0 commit comments

Comments
 (0)