forked from graphql-hive/graphql-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
72 lines (61 loc) · 1.87 KB
/
index.ts
File metadata and controls
72 lines (61 loc) · 1.87 KB
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
import 'reflect-metadata';
import express from 'express';
import { createServer } from 'http';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import { graphqlApplication } from './app';
import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import bodyParser from 'body-parser';
import cors from 'cors';
const { schema, createExecution, createSubscription, createApolloGateway } =
graphqlApplication;
const gateway = createApolloGateway();
const app = express();
const httpServer = createServer(app);
// Creating the WebSocket subscription server
const server = new ApolloServer({
gateway,
plugins: [
// Proper shutdown for the HTTP server.
ApolloServerPluginDrainHttpServer({ httpServer }),
// Proper shutdown for the WebSocket server.
{
async serverWillStart() {
return {
async drainServer() {
await serverCleanup.dispose();
},
};
},
},
],
});
const wsServer = new WebSocketServer({
server: httpServer,
path: '/graphql',
});
const execute = createExecution();
const subscribe = createSubscription();
// Passing in an instance of a GraphQLSchema and
// telling the WebSocketServer to start listening
const serverCleanup = useServer({ schema, execute, subscribe }, wsServer);
async function main() {
await server.start();
app.use(
'/graphql',
cors<cors.CorsRequest>(),
bodyParser.json(),
expressMiddleware(server)
);
httpServer.listen({ port: 4000 }, () => {
// tslint:disable-next-line: no-console
console.info(`🚀 Server ready at http://localhost:4000/graphql`);
});
}
main().catch((error) => {
// tslint:disable-next-line: no-console
console.error(error);
process.exit(1);
});