@as-integrations/fastify

中文文档
3.1.0发布于 2025/10/11MITTypeScriptESM + CJS近 30 天下载 131.6万

An Apollo Server integration for use with Fastify

原始使用文档
文档目录13 节
  1. Introduction
  2. Requirements
  3. Installation
  4. Usage
  5. Context
  6. API
  7. fastifyApollo
  8. fastifyApolloHandler
  9. ApolloFastifyContextFunction
  10. ApolloFastifyHandlerOptions
  11. ApolloFastifyPluginOptions
  12. HTTPS/HTTP2
  13. Contributors

Apollo Server Fastify

NPM version NPM downloads

Apollo Server Integration for Fastify

Introduction

An Apollo Server integration for use with Fastify.

This is a simple package that easily allows you to connect your own Fastify server implementation to an Apollo Server instance.

Requirements

Installation

npm install @as-integrations/fastify @apollo/server graphql fastify

Usage

Setup Fastify & Apollo Server like you usually would and then connect the two by using the fastifyApollo plugin:

import Fastify from "fastify";
import { ApolloServer, BaseContext } from "@apollo/server";
import fastifyApollo, { fastifyApolloDrainPlugin } from "@as-integrations/fastify";
// ...

const fastify = Fastify();

const apollo = new ApolloServer<BaseContext>({
  typeDefs,
  resolvers,
  plugins: [fastifyApolloDrainPlugin(fastify)],
});

await apollo.start();

// ...

await fastify.register(fastifyApollo(apollo));

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

Alternatively you can use the exported function fastifyApolloHandler which can be passed into any Fastify route handler. This allows you to explicitly set all routing options like the URL path and accepted methods.

Examples shown below:

import { fastifyApolloHandler } from "@as-integrations/fastify";

// ... setup Fastify & Apollo

fastify.post("/graphql", fastifyApolloHandler(apollo));
// OR
fastify.get("/api", fastifyApolloHandler(apollo));
// OR
fastify.route({
  url: "/graphql",
  method: ["POST", "OPTIONS"],
  handler: fastifyApolloHandler(apollo),
});

Please see the example.

Context

Apollo Server v4 has moved context setup outside of the ApolloServer constructor.

Define your own context function and pass it in to the context option. The function accepts two arguments - request and reply. They are of type FastifyRequest and FastifyReply respectively. Whatever is returned from the function will be passed to the context argument in your resolvers.

For example:

import { ApolloServer } from "@apollo/server";

import fastifyApollo, {
  fastifyApolloHandler,
  ApolloFastifyContextFunction,
} from "@as-integrations/fastify";
// ...

interface MyContext {
  authorization: JWTPayload | false;
}

const apollo = new ApolloServer<MyContext>(...);

const myContextFunction: ApolloFastifyContextFunction<MyContext> = async (request, reply) => ({
  authorization: await isAuthorized(request.headers.authorization),
});

await fastify.register(fastifyApollo(apollo), {
  context: myContextFunction,
});

// OR

await fastify.post("/graphql", fastifyApolloHandler(apollo, {
  context: myContextFunction,
}));
// Access the context in your resolvers
export const resolvers = {
  Query: {
    helloWorld: (parent, args, context, info) => {
      if (!context.authorization) {
        throw new Error("Not authorized");
      }

      return "Hello world :)";
    },
  },
};

API

All options and generics are optional other than passing in the ApolloServer instance.

fastifyApollo

function fastifyApollo<Context extends BaseContext = BaseContext>(
  apollo: ApolloServer<Context>,
): FastifyPluginAsync<ApolloFastifyPluginOptions<Context>>;

fastifyApolloHandler

function fastifyApolloHandler<Context extends BaseContext = BaseContext>(
  apollo: ApolloServer<Context>,
  options?: ApolloFastifyHandlerOptions<Context>,
): RouteHandlerMethod;

ApolloFastifyContextFunction

type ApolloFastifyContextFunction<Context> = (
  request: FastifyRequest,
  reply: FastifyReply,
) => Promise<Context>;

ApolloFastifyHandlerOptions

interface ApolloFastifyHandlerOptions<Context extends BaseContext = BaseContext> {
  context?: ApolloFastifyContextFunction<Context>; // default: async () => ({})
}

ApolloFastifyPluginOptions

interface ApolloFastifyPluginOptions<Context extends BaseContext = BaseContext>
  extends ApolloFastifyHandlerOptions<Context> {
  path?: string; // default: "/graphql"
  method?: HTTPMethod | HTTPMethod[]; // default: ["GET", "POST", "OPTIONS"]
}

HTTPMethod is exported from Fastify.

HTTPS/HTTP2

All functions and types optionally allow you to pass in or infer a Server type from Fastify.

Contributors

版本与发布记录

@as-integrations/fastify 共发布 15 个版本,首次发布于 2022/09/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.0latest2025/10/111.1 年前
3.0.02025/08/081.2 年前
2.1.12023/10/053 年前
2.1.02023/07/213 年前
2.0.02023/05/123 年前
1.3.22023/02/253 年前

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

@as-integrations/fastify 是什么?

An Apollo Server integration for use with Fastify

如何安装 @as-integrations/fastify?

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

@as-integrations/fastify 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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

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

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