|
1 | 1 | ---
|
2 | 2 | name: Apollo Server
|
3 |
| -description: A set of GraphQL server packages from Apollo that work with various Node.js HTTP frameworks (Express, Connect, Hapi, Koa etc). |
| 3 | +description: A GraphQL server from Apollo that works with any Node.js HTTP framework |
4 | 4 | url: https://www.apollographql.com/docs/apollo-server/
|
5 | 5 | github: apollographql/apollo-server
|
6 |
| -npm: "apollo-server-express" |
| 6 | +npm: "@apollo/server" |
7 | 7 | ---
|
8 | 8 |
|
9 |
| -To run a hello world server with apollo-server-express: |
| 9 | +To run a hello world server with Apollo Server: |
10 | 10 |
|
11 | 11 | ```bash
|
12 |
| -npm install apollo-server-express apollo-server-core express graphql |
| 12 | +npm install @apollo/server graphql |
13 | 13 | ```
|
14 | 14 |
|
15 | 15 | Then run `node server.js` with this code in `server.js`:
|
16 | 16 |
|
17 | 17 | ```js
|
18 |
| -import { ApolloServer } from 'apollo-server-express'; |
19 |
| -import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core'; |
20 |
| -import express from 'express'; |
21 |
| -import http from 'http'; |
| 18 | +import { ApolloServer } from '@apollo/server'; |
| 19 | +import { startStandaloneServer } from '@apollo/server/standalone'; |
22 | 20 |
|
23 |
| -async function startApolloServer(typeDefs, resolvers) { |
24 |
| - const app = express(); |
| 21 | +const server = new ApolloServer({ |
| 22 | + typeDefs, |
| 23 | + resolvers, |
| 24 | +}); |
25 | 25 |
|
26 |
| - const httpServer = http.createServer(app); |
27 |
| - |
28 |
| - const server = new ApolloServer({ |
29 |
| - typeDefs, |
30 |
| - resolvers, |
31 |
| - plugins: [ApolloServerPluginDrainHttpServer({ httpServer })], |
32 |
| - }); |
33 |
| - |
34 |
| - await server.start(); |
35 |
| - |
36 |
| - server.applyMiddleware({ app }); |
37 |
| - |
38 |
| - await new Promise(resolve => httpServer.listen({ port: 4000 }, resolve)); |
| 26 | +const { url } = await startStandaloneServer(server); |
39 | 27 |
|
40 |
| - console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`); |
41 |
| -} |
| 28 | +console.log(`🚀 Server ready at ${url}`); |
42 | 29 | ```
|
43 | 30 |
|
44 |
| -Apollo Server also supports all Node.js HTTP server frameworks: Express, Connect, HAPI, Koa and NestJs. |
| 31 | +Apollo Server has a built in standalone HTTP server and middleware for Express, and has an framework integration API that supports all [Node.js HTTP server frameworks and serverless environments](https://www.apollographql.com/docs/apollo-server/integrations/integration-index) via community integrations. |
| 32 | + |
| 33 | +Apollo Server has a [plugin API](https://www.apollographql.com/docs/apollo-server/integrations/plugins), integration with Apollo Studio, and performance and security features such as [caching](https://www.apollographql.com/docs/apollo-server/performance/caching/), [automatic persisted queries](https://www.apollographql.com/docs/apollo-server/performance/apq/), and [CSRF prevention](https://www.apollographql.com/docs/apollo-server/security/cors#preventing-cross-site-request-forgery-csrf). |
0 commit comments