-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (47 loc) · 1.35 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
const { makeExecutableSchema } = require('graphql-tools');
const { execute, subscribe } = require('graphql');
const { createServer } = require('http');
const { SubscriptionServer } = require('subscriptions-transport-ws');
const { ApolloServer } = require('apollo-server-express');
const cors = require('cors');
const express = require('express');
const { typeDefs, resolvers } = require('./schema');
const PORT = 4000;
const WS_PORT = 4001;
// Setup websocket server
const websocketServer = createServer((request, response) => {
response.writeHead(404);
response.end();
});
const subscriptionServer = SubscriptionServer.create(
{
execute,
subscribe,
schema: makeExecutableSchema({ typeDefs, resolvers }),
},
{
server: websocketServer,
path: '/graphql',
}
);
websocketServer.listen(WS_PORT, () =>
console.log(`Websocket Server is now running on http://localhost:${WS_PORT}/graphql`)
);
// Setup graphql server
const app = express();
app.use(cors());
const server = new ApolloServer({
typeDefs,
resolvers,
subscriptionsPath: `http://localhost:${WS_PORT}/graphql`,
playground: {
subscriptionEndpoint: `ws://localhost:${WS_PORT}/graphql`,
//version: `1.8.10`
},
});
server.applyMiddleware({ app });
app.listen({ port: PORT }, () =>
console.log(
`Server listening at http://localhost:${PORT}${server.graphqlPath}`
)
);