-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
48 lines (40 loc) · 1.26 KB
/
server.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
import Config from './config';
import express from 'express';
import {
graphqlExpress,
graphiqlExpress,
} from 'apollo-server-express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { createServer } from 'http';
import { execute, subscribe } from 'graphql';
import { SubscriptionServer } from 'subscriptions-transport-ws';
// schema file
import schema from './data/schema';
// Express server
const server = express();
// origin must be same as your client URI
server.use('*', cors({ origin: `http://localhost:3001` }));
// endpoint for clients to interact with server
server.use('/graphql', bodyParser.json(), graphqlExpress({
schema
}));
// endpoint for browser client and test tool
server.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
subscriptionsEndpoint: `ws://localhost:${Config.serverPort}/subscriptions`
}));
// IMPORTANT: wrap the Express server with new http client instance
const ws = createServer(server);
ws.listen(Config.serverPort, () => {
console.log(`Apollo Server is now running on http://localhost:${Config.serverPort}`);
// Set up the WebSocket for handling GraphQL subscriptions
new SubscriptionServer({
execute,
subscribe,
schema
}, {
server: ws,
path: '/subscriptions',
});
});