From 3ad8911e60733ac8e6032e8e081083c51946f3f6 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Tue, 29 Nov 2022 11:25:06 -0800 Subject: [PATCH] Update apollo-server to Apollo Server v4 This moves to a new package name with a slightly different API. Also mention a few of the distinctive features. --- .../javascript/server/apollo-server.md | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/src/content/code/language-support/javascript/server/apollo-server.md b/src/content/code/language-support/javascript/server/apollo-server.md index 8318f811c6..3a52436d3b 100644 --- a/src/content/code/language-support/javascript/server/apollo-server.md +++ b/src/content/code/language-support/javascript/server/apollo-server.md @@ -1,44 +1,33 @@ --- name: Apollo Server -description: A set of GraphQL server packages from Apollo that work with various Node.js HTTP frameworks (Express, Connect, Hapi, Koa etc). +description: A GraphQL server from Apollo that works with any Node.js HTTP framework url: https://www.apollographql.com/docs/apollo-server/ github: apollographql/apollo-server -npm: "apollo-server-express" +npm: "@apollo/server" --- -To run a hello world server with apollo-server-express: +To run a hello world server with Apollo Server: ```bash -npm install apollo-server-express apollo-server-core express graphql +npm install @apollo/server graphql ``` Then run `node server.js` with this code in `server.js`: ```js -import { ApolloServer } from 'apollo-server-express'; -import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core'; -import express from 'express'; -import http from 'http'; +import { ApolloServer } from '@apollo/server'; +import { startStandaloneServer } from '@apollo/server/standalone'; -async function startApolloServer(typeDefs, resolvers) { - const app = express(); +const server = new ApolloServer({ + typeDefs, + resolvers, +}); - const httpServer = http.createServer(app); - - const server = new ApolloServer({ - typeDefs, - resolvers, - plugins: [ApolloServerPluginDrainHttpServer({ httpServer })], - }); - - await server.start(); - - server.applyMiddleware({ app }); - - await new Promise(resolve => httpServer.listen({ port: 4000 }, resolve)); +const { url } = await startStandaloneServer(server); - console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`); -} +console.log(`🚀 Server ready at ${url}`); ``` -Apollo Server also supports all Node.js HTTP server frameworks: Express, Connect, HAPI, Koa and NestJs. +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. + +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).