-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (149 loc) · 4.08 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@apollo/server/express4";
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
import express from "express";
import http from "http";
import cors from "cors";
import fs from "fs";
import bodyParser from "body-parser";
import { GraphQLScalarType } from "graphql";
import { makeExecutableSchema } from "@graphql-tools/schema";
import { WebSocketServer } from "ws";
import { useServer } from "graphql-ws/lib/use/ws";
import { PubSub } from "graphql-subscriptions";
import lifts from "./data/lifts.json" assert { type: "json" };
import trails from "./data/trails.json" assert { type: "json" };
import hotels from "./data/hotels.json" assert { type: "json" };
import events from "./data/events.json" assert { type: "json" };
import "dotenv/config";
const typeDefs = fs.readFileSync(
"./typeDefs.graphql",
"UTF-8"
);
const pubsub = new PubSub();
const resolvers = {
Query: {
allEvents: () => events,
allHotels: () => hotels,
allLifts: (parent, { status }) =>
!status
? lifts
: lifts.filter((lift) => lift.status === status),
findLiftById: (parent, { id }) =>
lifts.find((lift) => id === lift.id),
liftCount: (parent, { status }) =>
!status
? lifts.length
: lifts.filter((lift) => lift.status === status)
.length,
allTrails: (parent, { status }) =>
!status
? trails
: trails.filter((trail) => trail.status === status),
findTrailByName: (parent, { name }) =>
trails.find((trail) => name === trail.name),
trailCount: (parent, { status }) =>
!status
? trails.length
: trails.filter((trail) => trail.status === status)
.length
},
Mutation: {
setLiftStatus: (parent, { id, status }) => {
let updatedLift = lifts.find(
(lift) => id === lift.id
);
updatedLift.status = status;
pubsub.publish("lift-status-change", {
liftStatusChange: updatedLift
});
return updatedLift;
},
setTrailStatus: (parent, { id, status }) => {
let updatedTrail = trails.find(
(trail) => id === trail.id
);
updatedTrail.status = status;
pubsub.publish("trail-status-change", {
trailStatusChange: updatedTrail
});
return updatedTrail;
}
},
Lift: {
trailAccess: (parent) =>
parent.trails.map((id) =>
trails.find((t) => id === t.id)
)
},
Trail: {
accessedByLifts: (parent) =>
parent.lift.map((id) =>
lifts.find((l) => id === l.id)
)
},
Date: new GraphQLScalarType({
name: "DateTime",
description: "A valid date time value.",
parseValue: (value) => new Date(value),
serialize: (value) =>
new Date(value).toLocaleDateString("en-us", {
weekday: "long",
year: "numeric",
month: "short",
day: "numeric"
}),
parseLiteral: (ast) => new Date(ast.value)
}),
Subscription: {
liftStatusChange: {
subscribe: (parent, data) =>
pubsub.asyncIterator("lift-status-change")
},
trailStatusChange: {
subscribe: (parent, data) =>
pubsub.asyncIterator("trail-status-change")
}
}
};
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
const app = express();
const httpServer = http.createServer(app);
const wsServer = new WebSocketServer({
server: httpServer,
path: "/"
});
const serverCleanup = useServer({ schema }, wsServer);
const server = new ApolloServer({
schema,
context: { pubsub },
plugins: [
ApolloServerPluginDrainHttpServer({ httpServer }),
{
async serverWillStart() {
return {
async drainServer() {
await serverCleanup.dispose();
}
};
}
}
]
});
await server.start();
app.use(
"/",
cors(),
bodyParser.json(),
expressMiddleware(server)
);
const PORT = 3000;
// Now that our HTTP server is fully set up, we can listen to it.
httpServer.listen(PORT, () => {
console.log(
`Server is now running on http://localhost:${PORT}`
);
});