Apollo Server for JavaScript (Node.js): Complete Guide with Examples
\n\nApollo Server is a powerful and flexible library for creating GraphQL servers on Node.js. It is part of the Apollo GraphQL ecosystem and provides developers with a convenient toolkit for building performant, scalable, and easily maintainable APIs. If you are familiar with REST, GraphQL can be thought of as an alternative where the client decides what data it needs, and the server returns exactly what was requested. Apollo Server handles all the routine work: request parsing, schema validation, executing resolvers, and formatting the response.
\n\nWhy do you need Apollo Server? First, it significantly simplifies the development of GraphQL servers. You don't need to write parsers, validators, or worry about security (protection against SQL injections, query depth limiting, etc.) — all of this is already built-in. Second, Apollo Server supports many integrations: with Express, Fastify, Koa, AWS Lambda, Azure Functions, and other platforms. This means you can deploy the server anywhere — from a simple local application to a serverless function. Third, the library is actively developed, has a large community and excellent documentation, making it the de facto standard for GraphQL in Node.js.
\n\nIn this article, we will thoroughly cover installation, main methods, write several working examples, and provide performance tips. The material will be useful both for beginners just starting to learn GraphQL and for experienced developers looking to deepen their knowledge of Apollo Server.
\n\n1. Installation
\n\nTo work with Apollo Server, you will need Node.js version 14 or higher. Installation is done via npm (or yarn). The main package is @apollo/server. You will also need graphql — this is the core of the GraphQL language, without which Apollo Server cannot function.
npm install @apollo/server graphql\n\nIf you plan to use Apollo Server with Express (the most popular option), install additionally:
\n\nnpm install express cors body-parser\nnpm install @as-integrations/express\n\nTo work with TypeScript (recommended), install the types:
\n\nnpm install --save-dev typescript @types/node @types/express\n\n2. Quick Start — Minimal Working Example
\n\nLet's create a simple GraphQL server that will greet the user. Create a file index.js and add the following code:
import { ApolloServer } from '@apollo/server';\nimport { startStandaloneServer } from '@apollo/server/standalone';\n\n// 1. Define the schema (typeDefs)\nconst typeDefs = `#graphql\n type Query {\n hello: String\n }\n`;\n\n// 2. Define resolvers\nconst resolvers = {\n Query: {\n hello: () => 'Hello, world! This is Apollo Server!',\n },\n};\n\n// 3. Create a server instance\nconst server = new ApolloServer({\n typeDefs,\n resolvers,\n});\n\n// 4. Start the server\nconst { url } = await startStandaloneServer(server, {\n listen: { port: 4000 },\n});\n\nconsole.log(`🚀 Server running at: ${url}`);\n\n\nStart the server with the command node index.js (if using ES modules, add "type": "module" to package.json). Open your browser and go to http://localhost:4000. You will see the Apollo Sandbox interface, where you can execute a query:
query {\n hello\n}\n\nThe response should be: { "data": { "hello": "Hello, world! This is Apollo Server!" } }. Congratulations, your first GraphQL server is working!
3. Main Methods/Functions/Classes
\n\nLet's look at the key elements of Apollo Server. For each, we will provide the signature, a brief description, and an example.
\n\n