@as-integrations/express5

中文文档
1.1.2发布于 2025/07/18MITTypeScriptESM + CJS零依赖近 30 天下载 509.5万

An Apollo Server integration for use with Express v5

原始使用文档
文档目录4 节
  1. Introduction
  2. Requirements
  3. Installation
  4. Usage

Apollo Server Integration for Express v5

Introduction

This package integrates Apollo Server v4 with version 5 of the Express web framework, by exporting a middleware that executes GraphQL operations.

It is identical to the middleware exported at @apollo/server/express5 from the core @apollo/server v4 package, updated to support Express v5 instead of Express v4.

Requirements

Installation

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

Usage

Set up Express & Apollo Server like you usually would, and then connect the two by using the expressMiddleware middleware:

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';
import { typeDefs, resolvers } from './schema';

interface MyContext {
  token?: string;
}

// Required logic for integrating with Express
const app = express();
// Our httpServer handles incoming requests to our Express app.
// Below, we tell Apollo Server to "drain" this httpServer,
// enabling our servers to shut down gracefully.
const httpServer = http.createServer(app);

const server = new ApolloServer<MyContext>({
  typeDefs,
  resolvers,
  plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
// Ensure we wait for our server to start
await server.start();

// Set up our Express middleware to handle CORS, body parsing,
// and our expressMiddleware function.
app.use(
  '/',
  cors<cors.CorsRequest>(),
  express.json(),
  // expressMiddleware accepts the same arguments:
  // an Apollo Server instance and optional configuration options
  expressMiddleware(server, {
    context: async ({ req }) => ({ token: req.headers.token }),
  }),
);

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

Note: You must call and await server.start() before using the integration.

版本与发布记录

@as-integrations/express5 共发布 7 个版本,首次发布于 2025/05/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.2latest2025/07/181.3 年前
1.1.12025/07/081.3 年前
1.1.02025/06/071.4 年前
1.1.0-alpha.0alpha2025/06/071.4 年前
2.0.0-alpha.12025/06/031.4 年前
2.0.0-alpha.02025/06/031.4 年前

关于 @as-integrations/express5 的常见问题

@as-integrations/express5 是什么?

An Apollo Server integration for use with Express v5

如何安装 @as-integrations/express5?

在项目目录运行 npm install @as-integrations/express5,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:express(^5.0.0)、@apollo/server(^4.0.0 || ^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@as-integrations/express5 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@as-integrations/express5 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@as-integrations/express5 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@as-integrations/express5 可以商用吗?

@as-integrations/express5 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@as-integrations/express5 的下载量有多少?

根据 npm 官方下载统计,@as-integrations/express5 在 2026/10/07 – 2026/11/05 这 30 天内被下载 5,094,907 次,日均约 169,830 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@as-integrations/express5 还在维护吗?

最新版本 1.1.2 发布于 2025/07/18(1.3 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/05/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。