| 19 | const httpServer = createServer(app) |
| 20 | |
| 21 | async function start() { |
| 22 | /** Create WS Server */ |
| 23 | const wsServer = new WebSocketServer({ |
| 24 | server: httpServer, |
| 25 | path: '/graphql', |
| 26 | }) |
| 27 | |
| 28 | /** hand-in created schema and have the WS Server start listening */ |
| 29 | const serverCleanup = useServer({ schema, context }, wsServer) |
| 30 | |
| 31 | const server = new ApolloServer<Context>({ |
| 32 | schema, |
| 33 | plugins: [ |
| 34 | ApolloServerPluginDrainHttpServer({ httpServer }), |
| 35 | { |
| 36 | async serverWillStart() { |
| 37 | return { |
| 38 | async drainServer() { |
| 39 | await serverCleanup.dispose() |
| 40 | }, |
| 41 | } |
| 42 | }, |
| 43 | }, |
| 44 | ], |
| 45 | }) |
| 46 | |
| 47 | await server.start() |
| 48 | app.use('/graphql', cors<cors.CorsRequest>(), express.json(), expressMiddleware(server, { context: async () => context })); |
| 49 | |
| 50 | httpServer.listen(PORT, () => { |
| 51 | console.log(`🚀 Server ready at http://localhost:${PORT}/graphql`) |
| 52 | console.log(`⏰ Subscriptions ready at http://localhost:${PORT}/graphql`) |
| 53 | console.log( |
| 54 | `⭐️ See sample queries: http://pris.ly/e/ts/graphql-subscriptions#using-the-graphql-api`, |
| 55 | ) |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | start() |