forked from TobyChen0106/110-1-web-deploy-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (48 loc) · 1.51 KB
/
server.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
import express from "express";
import { ApolloServer, PubSub } from "apollo-server-express";
import { importSchema } from "graphql-import";
import bodyParser from "body-parser";
import cors from "cors";
import http from "http";
import "dotenv-defaults/config.js";
import path from "path";
import { dirname } from "path";
import { fileURLToPath } from "url";
import db from "./backend/db.js";
import Query from "./backend/resolvers/Query.js";
import Mutation from "./backend/resolvers/Mutation.js";
import Subscription from "./backend/resolvers/Subscription.js";
import mongo from "./backend/mongo.js";
import apiRoute from "./backend/route/api.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
const port = process.env.PORT || 80;
const typeDefs = importSchema("./backend/schema.graphql");
const pubsub = new PubSub();
const app = express();
app.use(cors());
app.use("/api", apiRoute);
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "build")));
app.get("/*", function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
const server = new ApolloServer({
typeDefs,
resolvers: {
Query,
Mutation,
Subscription,
},
context: {
db,
pubsub,
},
});
server.applyMiddleware({ app });
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
mongo.connect();
httpServer.listen(port, () => {
console.log(`🚀 Server Ready at ${port}! 🚀`);
console.log(`Graphql Port at ${port}${server.subscriptionsPath}`);
});