Skip to content

Instantly share code, notes, and snippets.

@aligos
Created November 4, 2016 10:11
Show Gist options
  • Save aligos/902ba3e90e820614e93833ade33956ad to your computer and use it in GitHub Desktop.
Save aligos/902ba3e90e820614e93833ade33956ad to your computer and use it in GitHub Desktop.
Server
import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { printSchema } from 'graphql/utilities/schemaPrinter';
import { subscriptionManager } from './data/subscriptions';
import schema from './data/schema';
const GRAPHQL_PORT = 8080;
const WS_PORT = 8090;
const graphQLServer = express().use('*', cors());
graphQLServer.use('/graphql', bodyParser.json(), graphqlExpress({
schema,
context: {},
}));
graphQLServer.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
graphQLServer.use('/schema', (req, res) => {
res.set('Content-Type', 'text/plain');
res.send(printSchema(schema));
});
graphQLServer.listen(GRAPHQL_PORT, () => console.log(
`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`
));
// WebSocket server for subscriptions
const websocketServer = createServer((request, response) => {
response.writeHead(404);
response.end();
});
websocketServer.listen(WS_PORT, () => console.log( // eslint-disable-line no-console
`Websocket Server is now running on http://localhost:${WS_PORT}`
));
// eslint-disable-next-line
new SubscriptionServer(
{ subscriptionManager },
websocketServer
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment