-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (65 loc) · 1.7 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require('express');
const bodyParser = require('body-parser');
const graphqlHttp = require('express-graphql');
const { buildSchema } = require('graphql');
const mongoose = require('mongoose');
const EventModel = require('./models/Event');
const app = express();
app.use(bodyParser.json());
app.use('/graphql', graphqlHttp({
schema: buildSchema(`
type Event {
_id: ID
title: String!
description: String!
price: Float!
date: String!
}
input EventInput {
title: String!
description: String!
price: Float!
date: String!
}
type rootQuery {
events: [Event!]!
}
type rootMutation {
createEvent(eventInput: EventInput ): Event
}
schema {
query: rootQuery
mutation: rootMutation
}
`),
rootValue: {
events: () => {
return EventModel
.find()
.then(result => {
console.log('result success', result);
return result
})
.catch(err => { throw err;});
},
createEvent: (args) => {
return new EventModel({
title: args.eventInput.title,
description: args.eventInput.description,
price: +args.eventInput.price,
date: args.eventInput.date,
})
.save()
.then(result => ({
...result._doc,
_id: result.id,
}))
.catch(err => {throw err;});
},
},
graphiql: true,
}));
mongoose
.connect(`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0-iivao.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`)
.then(() => app.listen(3000, () => console.log('start server')))
.catch(err => console.log('error trying to connect db', err));