apollo-server README

The project's README from github.com/apollographql/apollo-server. As one markdown file: README.md.

@apollo/server

This @apollo/server package is new since Apollo Server 4. Previous major versions of Apollo Server used a set of package names starting with apollo-server, such as apollo-server, apollo-server-express, apollo-server-core, etc.

npm version Build Status Join the community


Announcement: Join 1000+ engineers at GraphQL Summit 2025 by Apollo for talks, workshops, and office hours. Oct 6-8, 2025 in San Francisco. Get your pass here ->


A TypeScript/JavaScript GraphQL server

Apollo Server is an open-source, spec-compliant GraphQL server that's compatible with any GraphQL client, including Apollo Client. It's the best way to build a production-ready, self-documenting GraphQL API that can use data from any source.

You can use Apollo Server as:

Apollo Server provides a simple API for integrating with any Node.js web framework or serverless environment. The @apollo/server package itself ships with a minimally-configurable, standalone web server which handles CORS and body parsing out of the box. Integrations with other environments are community-maintained.

Apollo Server provides:

Documentation

Full documentation for Apollo Server is available on our documentation site. This README shows the basics of getting a server running (both standalone and with Express), but most features are only documented on our docs site.

Getting started: standalone server

You can also check out the getting started guide in the Apollo Server docs for more details, including examples in both TypeScript and JavaScript.

Apollo Server's standalone server lets you get a GraphQL server up and running quickly without needing to set up an HTTP server yourself. It allows all the same configuration of GraphQL logic as the Express integration, but does not provide the ability to make fine-grained tweaks to the HTTP-specific behavior of your server.

First, install Apollo Server and the JavaScript implementation of the core GraphQL algorithms:

npm install @apollo/server graphql

Then, write the following to server.mjs. (By using the .mjs extension, Node lets you use the await keyword at the top level.)

import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';

// The GraphQL schema
const typeDefs = `#graphql
  type Query {
    hello: String
  }
`;

// A map of functions which return data for the schema.
const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

const { url } = await startStandaloneServer(server);
console.log(`🚀 Server ready at ${url}`);

Now run your server with:

node server.mjs

Open the URL it prints in a web browser. It will show Apollo Sandbox, a web-based tool for running GraphQL operations. Try running the operation query { hello }!

Getting started: Express middleware

Apollo Server's Express middleware lets you run your GraphQL server as part of an app built with Express, the most popular web framework for Node.

First, install Apollo Server, its Express middleware, the JavaScript implementation of the core GraphQL algorithms, Express, and the standard Express middleware package for CORS headers:

npm install @apollo/server @as-integrations/express5 graphql express cors

If using Typescript you may also need to install additional type declaration packages as development dependencies to avoid common errors when importing the above packages (i.e. Could not find a declaration file for module 'cors'):

npm install --save-dev @types/cors @types/express

Then, write the following to server.mjs. (By using the .mjs extension, Node lets you use the await keyword at the top level.)

import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@as-integrations/express5';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer'
import express from 'express';
import http from 'http';
import cors from 'cors';

// The GraphQL schema
const typeDefs = `#graphql
  type Query {
    hello: String
  }
`;

// A map of functions which return data for the schema.
const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const app = express();
const httpServer = http.createServer(app);

// Set up Apollo Server
const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
await server.start();

app.use(
  cors(),
  express.json(),
  expressMiddleware(server),
);

await new Promise((resolve) => httpServer.listen({ port: 4000 }, resolve));
console.log(`🚀 Server ready at http://localhost:4000`);

Now run your server with:

node server.mjs

Open the URL it prints in a web browser. It will show Apollo Sandbox, a web-based tool for running GraphQL operations. Try running the operation query { hello }!

apollo-server (apollographql/apollo-server)

Searching Database…

这是什么?Apollo Server 是一个开源的、符合规范的 GraphQL 服务器,运行在 Node.js 上。你可以用它基于任意数据源构建可用于生产环境、且能自动生成文档的 GraphQL API。它既可以作为独立服务器运行,也可以接入 Express、Connect、Hapi、Koa 等框架。
这是独立服务器,还是要添加到我应用中的库?它是一个 npm 库(@apollo/server),需要安装到你自己的 Node 项目中。它开箱即用地附带一个可进行少量配置的独立 HTTP 服务器,也可以作为中间件运行在现有的 Express(或其他)应用中。
如何快速跑起一个基础的 GraphQL 服务器?用 npm 安装 @apollo/server 和 graphql 包,在 server.mjs 文件中编写你的 schema 和 resolvers,然后用 Node 运行它(.mjs 扩展名让你可以使用顶层 await)。打开终端输出的网址,就能看到用于测试查询的 Apollo Sandbox,比如输入 'query { hello }'。
开始之前我需要准备什么?你需要 Node.js 20 或更高版本,以及 npm 8.19.2+,另外还需要对等依赖 graphql 包来执行核心 GraphQL 功能。如果要使用 Express 集成,还需要 express 和 cors 中间件包。
Connect、Hapi 和 Koa 的集成是由 Apollo 官方自己维护的吗?不是。只有 Express 中间件直接随本项目提供;Connect、Hapi、Koa 以及其他环境的集成由社区维护,而不是 Apollo 核心团队。
什么是 Apollo Sandbox?Apollo Sandbox 是一个基于网页的 GraphQL IDE,Apollo Server 会自动在你服务器的网址上提供它,让你无需额外设置,就能直接在浏览器中运行 'query { hello }' 等操作。
Apollo Server 可以用于 GraphQL Federation 吗?可以。除了作为独立服务器运行,它还可以作为联邦超图(supergraph)中某个子图(subgraph)的 GraphQL 服务器,或者作为位于联邦超图前面的网关(gateway)。
为什么叫 @apollo/server,而不是 apollo-server?@apollo/server 是 Apollo Server 4 引入的统一包,取代了早期主版本中使用的拆分包(apollo-server、apollo-server-express、apollo-server-core)。
它能和任何 GraphQL 客户端一起使用吗?可以。它符合规范,设计上兼容任何 GraphQL 客户端,包括但不限于 Apollo Client。
Size
136.7 MB
Stars
14K
Age
10y
Commits · 90 Days