|
1 | | -import { createServer } from 'node:http' |
2 | | -import express from 'express' |
3 | | -import cors from 'cors' |
4 | | -import bodyParser from 'body-parser' |
5 | | -import { ApolloServer } from '@apollo/server' |
6 | | -import { expressMiddleware } from '@apollo/server/express4' |
7 | 1 | import { makeExecutableSchema } from '@graphql-tools/schema' |
8 | | -import { WebSocketServer } from 'ws' |
9 | | -import { useServer } from 'graphql-ws/lib/use/ws' |
10 | 2 | import { PubSub, withFilter } from 'graphql-subscriptions' |
11 | 3 | import shortid from 'shortid' |
12 | 4 | import gql from 'graphql-tag' |
13 | | -import { GraphQLError } from 'graphql' |
| 5 | +import { channels } from './data.mjs' |
| 6 | +import { simulateLatency, GraphQLErrorWithCode } from './util.mjs' |
14 | 7 |
|
15 | | -const shouldSimulateLatency = process.argv.includes('--simulate-latency') |
16 | | - |
17 | | -let latency = 500 |
18 | | -if (shouldSimulateLatency) { |
19 | | - const index = process.argv.indexOf('--simulate-latency') |
20 | | - if (index !== -1 && process.argv.length > index + 1) { |
21 | | - latency = parseInt(process.argv[index + 1]) |
22 | | - } |
23 | | -} |
24 | | - |
25 | | -export class GraphQLErrorWithCode extends GraphQLError { |
26 | | - constructor (message, code, extensions) { |
27 | | - super(message, { |
28 | | - extensions: { |
29 | | - code, |
30 | | - ...extensions, |
31 | | - }, |
32 | | - }) |
33 | | - } |
34 | | -} |
| 8 | +const pubsub = new PubSub() |
35 | 9 |
|
36 | 10 | const typeDefs = gql` |
37 | 11 | type Channel { |
@@ -77,37 +51,6 @@ type Subscription { |
77 | 51 | } |
78 | 52 | ` |
79 | 53 |
|
80 | | -const pubsub = new PubSub() |
81 | | - |
82 | | -let channels = [] |
83 | | - |
84 | | -function resetDatabase () { |
85 | | - channels = [ |
86 | | - { |
87 | | - id: 'general', |
88 | | - label: 'General', |
89 | | - messages: [], |
90 | | - }, |
91 | | - { |
92 | | - id: 'random', |
93 | | - label: 'Random', |
94 | | - messages: [], |
95 | | - }, |
96 | | - ] |
97 | | -} |
98 | | - |
99 | | -resetDatabase() |
100 | | - |
101 | | -function simulateLatency () { |
102 | | - return new Promise(resolve => { |
103 | | - if (shouldSimulateLatency) { |
104 | | - setTimeout(resolve, latency) |
105 | | - } else { |
106 | | - resolve() |
107 | | - } |
108 | | - }) |
109 | | -} |
110 | | - |
111 | 54 | const resolvers = { |
112 | 55 | Query: { |
113 | 56 | hello: () => simulateLatency().then(() => 'Hello world!'), |
@@ -168,61 +111,7 @@ const resolvers = { |
168 | 111 | }, |
169 | 112 | } |
170 | 113 |
|
171 | | -const schema = makeExecutableSchema({ |
| 114 | +export const schema = makeExecutableSchema({ |
172 | 115 | typeDefs, |
173 | 116 | resolvers, |
174 | 117 | }) |
175 | | - |
176 | | -const app = express() |
177 | | - |
178 | | -app.use(cors('*')) |
179 | | - |
180 | | -app.use(bodyParser.json()) |
181 | | - |
182 | | -app.get('/_reset', (req, res) => { |
183 | | - resetDatabase() |
184 | | - res.status(200).end() |
185 | | -}) |
186 | | - |
187 | | -const server = new ApolloServer({ |
188 | | - schema, |
189 | | - context: () => new Promise(resolve => { |
190 | | - setTimeout(() => resolve({}), 50) |
191 | | - }), |
192 | | - plugins: [ |
193 | | - // Proper shutdown for the WebSocket server. |
194 | | - { |
195 | | - async serverWillStart () { |
196 | | - return { |
197 | | - async drainServer () { |
198 | | - await serverCleanup.dispose() |
199 | | - }, |
200 | | - } |
201 | | - }, |
202 | | - }, |
203 | | - ], |
204 | | - csrfPrevention: false, |
205 | | -}) |
206 | | - |
207 | | -await server.start() |
208 | | - |
209 | | -app.use('/graphql', expressMiddleware(server)) |
210 | | - |
211 | | -const httpServer = createServer(app) |
212 | | - |
213 | | -// Websocket |
214 | | - |
215 | | -const wsServer = new WebSocketServer({ |
216 | | - server: httpServer, |
217 | | - path: '/graphql', |
218 | | -}) |
219 | | - |
220 | | -const serverCleanup = useServer({ |
221 | | - schema, |
222 | | -}, wsServer) |
223 | | - |
224 | | -httpServer.listen({ |
225 | | - port: 4042, |
226 | | -}, () => { |
227 | | - console.log('🚀 Server ready at http://localhost:4042') |
228 | | -}) |
0 commit comments