@@ -9,35 +9,36 @@ npm: "apollo-server-express"
9
9
To run a hello world server with apollo-server-express:
10
10
11
11
``` bash
12
- npm install apollo-server-express express
12
+ npm install apollo-server-express apollo-server-core express graphql
13
13
```
14
14
15
15
Then run ` node server.js ` with this code in ` server.js ` :
16
16
17
17
``` js
18
- const express = require (' express' );
19
- const { ApolloServer , gql } = require (' apollo-server-express' );
18
+ import { ApolloServer } from ' apollo-server-express' ;
19
+ import { ApolloServerPluginDrainHttpServer } from ' apollo-server-core' ;
20
+ import express from ' express' ;
21
+ import http from ' http' ;
20
22
21
- const typeDefs = gql `
22
- type Query {
23
- hello : String
24
- }
25
- ` ;
23
+ async function startApolloServer (typeDefs , resolvers ) {
24
+ const app = express ();
26
25
27
- const resolvers = {
28
- Query: {
29
- hello : () => ' Hello world!' ,
30
- },
31
- };
26
+ const httpServer = http .createServer (app);
32
27
33
- const server = new ApolloServer ({ typeDefs, resolvers });
28
+ const server = new ApolloServer ({
29
+ typeDefs,
30
+ resolvers,
31
+ plugins: [ApolloServerPluginDrainHttpServer ({ httpServer })],
32
+ });
34
33
35
- const app = express ();
36
- server .applyMiddleware ({ app });
34
+ await server .start ();
37
35
38
- app .listen ({ port: 4000 }, () =>
39
- console .log (' Now browse to http://localhost:4000' + server .graphqlPath )
40
- );
36
+ server .applyMiddleware ({ app });
37
+
38
+ await new Promise (resolve => httpServer .listen ({ port: 4000 }, resolve));
39
+
40
+ console .log (` 🚀 Server ready at http://localhost:4000${ server .graphqlPath } ` );
41
+ }
41
42
```
42
43
43
44
Apollo Server also supports all Node.js HTTP server frameworks: Express, Connect, HAPI, Koa and NestJs.
0 commit comments