1+ /* eslint-disable no-new */
12/* @flow */
23
34import express from 'express' ;
45import cors from 'cors' ;
5- import graphqlHTTP from 'express-graphql' ;
6- import expressPlayground from 'graphql-playground-middleware-express' ;
6+ import { ApolloServer } from 'apollo-server-express' ;
77import { altairExpress } from 'altair-express-middleware' ;
8+ import { express as voyagerMiddleware } from 'graphql-voyager/middleware' ;
9+ import http from 'http' ;
10+ import { SubscriptionServer } from 'subscriptions-transport-ws' ;
11+ import { execute , subscribe } from 'graphql' ;
812import { mainPage , addToMainPage } from './mainPage' ;
913import { expressPort , getExampleNames , resolveExamplePath } from './config' ;
1014import './mongooseConnection' ;
1115
12- const server = express ( ) ;
13- server . use ( cors ( { origin : true , credentials : true } ) ) ;
16+ const app = express ( ) ;
17+ const httpServer = http . createServer ( app ) ;
18+ app . use ( cors ( { origin : true , credentials : true } ) ) ;
1419
1520// scan `examples` directory and add
1621// - graphql endpoint by uri /exampleDirName
@@ -25,28 +30,48 @@ for (const name of exampleNames) {
2530}
2631
2732// $FlowFixMe
28- server . get ( '/' , ( req , res ) => {
33+ app . get ( '/' , ( req , res ) => {
2934 res . send ( mainPage ( ) ) ;
3035} ) ;
3136
32- server . listen ( expressPort , ( ) => {
37+ httpServer . listen ( expressPort , ( ) => {
3338 console . log ( `🚀🚀🚀 The server is running at http://localhost:${ expressPort } /` ) ;
39+
40+ // https://www.apollographql.com/docs/graphql-subscriptions/setup/
41+ SubscriptionServer . create (
42+ {
43+ schema : require ( './examples/northwind/schema' ) . default ,
44+ execute,
45+ subscribe,
46+ onConnect : ( connectionParams , ws , context ) => {
47+ console . log ( `WS[connect][${ context . request . connection . remoteAddress } ]` , connectionParams ) ;
48+ } ,
49+ onDisconnect : ( ws , context ) => {
50+ console . log ( `WS[disconn][${ context . request . connection . remoteAddress } ]` ) ;
51+ } ,
52+ } ,
53+ {
54+ server : httpServer ,
55+ path : '/northwind' ,
56+ }
57+ ) ;
3458} ) ;
3559
3660function addExample ( example , uri ) {
3761 example . uri = `/${ uri } ` ; // eslint-disable-line
38- server . use (
39- example . uri ,
40- ( graphqlHTTP ( ( ) => ( {
41- schema : example . schema ,
42- graphiql : true ,
43- customFormatErrorFn : ( error ) => ( {
44- message : error . message ,
45- stack : ! error . message . match ( / f o r s e c u r i t y r e a s o n / i) ? error . stack . split ( '\n' ) : null ,
46- } ) ,
47- } ) ) : any )
62+
63+ const server = new ApolloServer ( {
64+ schema : example . schema ,
65+ playground : {
66+ subscriptionEndpoint : example . uri ,
67+ } ,
68+ } ) ;
69+ server . applyMiddleware ( { app, path : example . uri } ) ;
70+
71+ app . use (
72+ `${ example . uri } -altair` ,
73+ altairExpress ( { endpointURL : example . uri , subscriptionsEndpoint : example . uri } )
4874 ) ;
49- server . get ( `${ example . uri } -playground` , expressPlayground ( { endpoint : example . uri } ) ) ;
50- server . use ( `${ example . uri } -altair` , altairExpress ( { endpointURL : example . uri } ) ) ;
75+ app . use ( `${ example . uri } -voyager` , voyagerMiddleware ( { endpointUrl : example . uri } ) ) ;
5176 addToMainPage ( example ) ;
5277}
0 commit comments