-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
85 lines (73 loc) · 1.89 KB
/
app.js
File metadata and controls
85 lines (73 loc) · 1.89 KB
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
77
78
79
80
81
82
83
84
85
const express = require("express");
const bodyParser = require("body-parser");
const { graphqlHTTP } = require("express-graphql");
const { buildSchema } = require("graphql");
const mongoose = require("mongoose");
const Event = require("./models/event");
const app = express();
const events = [];
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 events;
},
createEvent: (args) => {
const event = new Event({
title: args.eventInput.title,
description: args.eventInput.description,
price: +args.eventInput.price,
date: new Date(args.eventInput.date),
});
return event
.save()
.then((result) => {
console.log(result);
return { ...result._doc };
})
.catch((err) => {
console.log(err);
throw err;
});
return event;
},
},
graphiql: true,
})
);
mongoose
.connect(
`mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@cluster0.wkvl3.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority`
)
.then(console.log("mongo connected"))
.catch((err) => console.log(err));
app.listen(3001, () => {
console.log("server is running in port 3001");
});