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
44require("dotenv").config();
import { GraphQLServer, PubSub } from "graphql-yoga";
import mongoose from "mongoose";
import schema from "../graphql/";
import { models } from "./config/db/";
const { mongoURI: db } = process.env;
const pubsub = new PubSub();
const options = {
port: process.env.PORT || "4000",
endpoint: "/graphql",
subscriptions: "/subscriptions",
playground: "/playground"
};
const context = {
models,
pubsub
};
// Connect to MongoDB with Mongoose.
mongoose
.connect(
db,
{
useCreateIndex: true,
useNewUrlParser: true
}
)
.then(() => console.log("MongoDB connected"))
.catch(err => console.log(err));
const server = new GraphQLServer({
schema,
context
});
server.start(options, ({ port }) => {
console.log(`๐ Server is running on http://localhost:${port}`);
});