@apollo/generate-persisted-query-manifest

中文文档
2.0.0发布于 2026/11/06MITTypeScriptCommonJS构建来源已验证近 30 天下载 39.2万

Creates a Persisted Query Manifest from an Apollo Client Web project

原始使用文档
文档目录6 节
  1. Setup
  2. CLI configuration
  3. Supported config file formats
  4. Configuration
  5. TypeScript
  6. Options

@apollo/generate-persisted-query-manifest

generate-persisted-query-manifest is a tool for generating persisted query manifests from the source code of Apollo Client Web projects. It scans a source code directory looking for GraphQL operations, applies the same transformations to them as Apollo Client Web would do at runtime, and compiles them into a persisted query manifest JSON file.

(It is not a general-purpose tool for creating PQM JSON files from arbitrary GraphQL operations: it is specifically tailored to the way Apollo Client Web transforms operations before sending them to servers.)

Setup

First, install the @apollo/generate-persisted-query-manifest package as a dev dependency:

npm install --save-dev @apollo/generate-persisted-query-manifest

Use its CLI to extract queries from your app:

npx generate-persisted-query-manifest

CLI configuration

To override the default options, you can provide a config file. Create a persisted-query-manifest.config.json file in the root of your project.

{
  "documents": [
    "src/**/*.{graphql,gql,js,jsx,ts,tsx}",
    "!**/*.d.ts",
    "!**/*.spec.{js,jsx,ts,tsx}",
    "!**/*.story.{js,jsx,ts,tsx}",
    "!**/*.test.{js,jsx,ts,tsx}"
  ],
  "output": "persisted-query-manifest.json"
}

NOTE: The config file is optional. Defaults for each option are displayed above.

If you would like to define the config file in a directory other than the root, you can tell the CLI the location of the config file using the --config option.

npx generate-persisted-query-manifest --config path/to/persisted-query-manifest.config.json

Supported config file formats

The config file can be provided in a variety of formats and optionally prefixed with a dot (.). The CLI will search for a config file in these locations:

  • .persisted-query-manifest.config.json
  • persisted-query-manifest.config.json
  • .persisted-query-manifest.config.yml
  • persisted-query-manifest.config.yml
  • .persisted-query-manifest.config.yaml
  • persisted-query-manifest.config.yaml
  • .persisted-query-manifest.config.js
  • persisted-query-manifest.config.js
  • .persisted-query-manifest.config.ts
  • persisted-query-manifest.config.ts
  • .persisted-query-manifest.config.cjs
  • persisted-query-manifest.config.cjs
  • A persisted-query-manifest key in package.json

Configuration

To customize the CLI, create a config file using one of the formats described above in the root of your directory. If using a .js , .cjs, or .ts config file, your config object must be the default export.

TypeScript

If you define your config file as a TypeScript file, you can get autocompletion and documentation on the options by importing the PersistedQueryManifestConfig type from the package.

// persisted-query-manifest.config.ts
import { PersistedQueryManifestConfig } from "@apollo/generate-persisted-query-manifest";

const config: PersistedQueryManifestConfig = {
  // options
};

export default config;

Options

  • documents - string | string[]

Tell the CLI where to look for your documents. You can use glob patterns to determine where to look. Prefix the pattern with ! to ignore the path which is useful to ignore queries that might be defined in your tests or storybook stories not used in your production application.

Paths are interpreted relative to the current working directory, not the config file's directory. We recommend always running generate-persisted-query-manifest via an npm run script, which runs commands with the directory containing package.json as the current directory.

Default:

[
  "src/**/*.{graphql,gql,js,jsx,ts,tsx}",
  "!**/*.d.ts",
  "!**/*.spec.{js,jsx,ts,tsx}",
  "!**/*.story.{js,jsx,ts,tsx}",
  "!**/*.test.{js,jsx,ts,tsx}"
]

Usage with GraphQL Codegen persisted documents

GraphQL Codegen is a popular code generation utility used with GraphQL. You can use GraphQL Codegen's persisted documents feature with generate-persisted-query-manifest by providing the documents option with the fromGraphQLCodegenPersistedDocuments utility exported by this package. This is useful to prevent traversing the file system to parse GraphQL documents since GraphQL Codegen has already done the hard work for you.

import type { PersistedQueryManifestConfig } from "@apollo/generate-persisted-query-manifest";
import { fromGraphQLCodegenPersistedDocuments } from "@apollo/generate-persisted-query-manifest";

const config: PersistedQueryManifestConfig = {
  documents: fromGraphQLCodegenPersistedDocuments(
    "./src/gql/persisted-documents.json",
  ),
};

export default config;

NOTE: Running these documents through this package is necessary to ensure the GraphQL document sent to the server by Apollo Client matches that in the manifest. While GraphQL Codegen's persisted documents transforms are intended to be similar to Apollo Client's, there are subtle differences that may prevent correct usage of Apollo's persisted queries. This also ensures forward compatibility with any future transforms introduced by Apollo Client that may alter the GraphQL document output.

  • output - string

Tell the CLI the location of where to write your manifest file. Paths are interpreted relative to the current working directory.

Default: persisted-query-manifest.json

  • createOperationId - Function (query: string, options: CreateOperationIdOptions) => string

A custom function that allows you to customize the id for a query operation. By default, a SHA 256 hash of the query string will be used to generate the id. This option can only be used if your config file is defined using a .js, .cjs or .ts extension.

When you use this option, you cannot use the generatePersistedQueryIdsAtRuntime function from @apollo/persisted-query-lists in your client, because that function assumes that you are using the default ID generation (SHA256 hash of the body). It is compatible with generatePersistedQueryIdsFromManifest.

interface CreateOperationIdOptions {
  /**
   * The name of the operation.
   */
  operationName: string;

  /**
   * The type of operation.
   */
  type: "query" | "mutation" | "subscription";

  /**
   * Helper function that returns the default generated ID for the operation.
   */
  createDefaultId: () => string;
}

Here is an example that uses a Base 64 encoded string for an operation named TestOperation, but uses the default generated ID for all others.

import { Buffer } from "node:buffer";

const config = {
  createOperationId(query, { operationName, type, createDefaultId }) {
    switch (operationName) {
      case "TestOperation":
        return Buffer.from(query).toString("base64");
      default:
        return createDefaultId();
    }
  },
};
  • documentTransform - DocumentTransform

An @apollo/client DocumentTransform instance used to transform GraphQL documents before they are saved to the manifest. Use this option if you pass a documentTransform option to your Apollo Client instance.

For more information, see the Apollo Client Document transforms documentation.

NOTE: This feature is only available if you use Apollo Client 3.8.0 or greater.

import { DocumentTransform } from "@apollo/client/core";

const config = {
  // Inlined for this example, but ideally this should use the same instance
  // that is passed to your Apollo Client instance
  documentTransform: new DocumentTransform((document) => {
    // ... transform the document

    return transformedDocument;
  }),
};
  • addTypename: boolean

Whether to add __typename fields to selection sets in operations. Defaults to true; you should set this to false if you also pass addTypename: false to your InMemoryCache constructor in your app.

[!NOTE] This option can be useful if you use this tool on a codebase that does not use Apollo Client. Be aware that this use case is not supported by this tool.

Note that the ability to pass addTypename: false will not be supported with Apollo Client v4.

版本与发布记录

@apollo/generate-persisted-query-manifest 共发布 16 个版本,首次发布于 2023/05/27;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/11/061 天前
1.4.02025/11/1411 个月前
1.3.02025/05/291.4 年前
1.2.22024/08/032 年前
1.2.12024/05/102 年前
1.2.02024/02/272 年前

其他发布通道:alpha → 1.0.0-alpha.0。

关于 @apollo/generate-persisted-query-manifest 的常见问题

@apollo/generate-persisted-query-manifest 是什么?

Creates a Persisted Query Manifest from an Apollo Client Web project

如何安装 @apollo/generate-persisted-query-manifest?

在项目目录运行 npm install @apollo/generate-persisted-query-manifest,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:graphql(14.x || 15.x || 16.x || 17.x)、@apollo/client(^3.7.0 || ^4.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 generate-persisted-query-manifest,可以用 npx generate-persisted-query-manifest 直接运行。

@apollo/generate-persisted-query-manifest 支持 TypeScript 吗?

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

@apollo/generate-persisted-query-manifest 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@apollo/generate-persisted-query-manifest 需要什么版本的 Node.js?

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

@apollo/generate-persisted-query-manifest 可以商用吗?

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

@apollo/generate-persisted-query-manifest 的下载量有多少?

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

@apollo/generate-persisted-query-manifest 还在维护吗?

最新版本 2.0.0 发布于 2026/11/06(1 天前);至今共发布 16 个版本,其中过去 12 个月发布了 2 个;首次发布于 2023/05/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。