# @freshcells/apollo-link-field-keep 中文文档

规范地址：https://npmcn.com/package/%40freshcells/apollo-link-field-keep/

版本：1\.3\.1
npm 元数据更新时间：2025-07-03T10:53:41.101Z
中文译文更新时间：2026-11-15T13:54:25.529Z
软件包许可：未声明

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40freshcells/apollo-link-field-keep
原文对照：https://npmcn.com/package/%40freshcells/apollo-link-field-keep/?lang=original

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：73.1 kB，21 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：@apollo/client \>=3，apollo-utilities \>=1，graphql \>=15
- 首次发布：2021-08-13T13:25:48.330Z
- 最新版本发布：2025-07-03T10:53:40.886Z
- 近 30 天下载量：3812（2026-10-16 至 2026-11-14，npm 官方下载统计）
- 已发布版本数：18（近 12 个月 0 个）

## 中文简介

类似 `@include` 财务报告和审定财务报表 `@skip`,但允许在发送到服务器之前完全绕过查询的某些部分

## 安装

```sh
npm install @freshcells/apollo-link-field-keep
```

也可以使用：`pnpm add @freshcells/apollo-link-field-keep`、`yarn add @freshcells/apollo-link-field-keep`、`bun add @freshcells/apollo-link-field-keep`

## README

## apollo-link-field-keep (英语)

[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![GitHub license badge](https://badgen.net/github/license/freshcells/apollo-link-field-keep)](https://opensource.org/licenses/MIT) [![npm](https://badgen.net/github/release/freshcells/apollo-link-field-keep)](https://www.npmjs.com/package/@freshcells/apollo-link-field-keep) [![codecov](https://codecov.io/gh/freshcells/apollo-link-field-keep/branch/main/graph/badge.svg?token=GCIBUG9Z7J)](https://codecov.io/gh/freshcells/apollo-link-field-keep)

**警告语**

类似 `@include` 财务报告和审定财务报表 `@skip`,但允许完全绕过查询的某些部分 在发送到服务器之前

[API 文档](https://freshcells.github.io/apollo-link-field-keep/api/)

### 使用大小写

-   特征
-   联邦计划

### 设置

备查 `npm` 软件包登记册

```
yarn add @freshcells/apollo-link-field-keep

npm install @freshcells/apollo-link-field-keep
```

#### 使用量

```ts
    import KeepLink from '@freshcells/apollo-link-field-keep'

// ...

const keepLink = new KeepLink()

// if you want to use ifFeature conditions, add also the following line, otherwise skip it:
keepLink.enabledFeatures = ['feature1']

const apolloClient = new ApolloClient({
    link: from([
        keepLink,
        // .. other links
    ]),
})
```

然后,你可以使用 `@keep` 指令如下:

```graphql
query someQuery($argValue: String! $shouldKeep: Boolean!) {
    someOther {
        subFieldOther(someArg: $argValue) {
            subSubFieldOther
        }
    }
    field(someArg: $argValue) @keep(if: $shouldKeep) {
        subFieldA
        subFieldB
    }
}
```

若为 `$shouldKeep` 此示例中 `false`这将删除整个 `field` 查询,包括参数. 它还会清理所有可能使用的变量(如果其他字段没有使用的话).

如果您有一个用于多个组件的查询, 并且需要包含在任何地方的某个字段, 您可以使用 `ifFeature` 改为 `if` 作为:

```graphql
query someQuery($argValue: String!) {
    someOther {
        subFieldOther(someArg: $argValue) {
            subSubFieldOther
        }
    }
    field(someArg: $argValue) @keep(ifFeature: "feature1") {
        subFieldA
        subFieldB
    }
}
```

它的工作方式 完全一样 `$shouldKeep` 在前一个例子中。 有了这个方法 你就省下时间 由于您不需要将相同的变量传递给所有查询使用大小写.

也可以结合 `if` 财务报告和审定财务报表 `ifFeature`数字 :

```graphql
query someQuery($argValue: String! $shouldKeep: Boolean!) {
    someOther {
        subFieldOther(someArg: $argValue) {
            subSubFieldOther
        }
    }
    field(someArg: $argValue) @keep(if: $shouldKeep ifFeature: "feature1") {
        subFieldA
        subFieldB
    }
}
```

在这种情况下,只有两个字段都包括在内 `$shouldKeep` 是真的,还有 `feature1` 已启用.

为了防止阿波罗缓存问题,它会给场上填充 `null` 当从服务器返回时.

### 被质疑的问题

使用 `apollo-link-field-keep` 与持续的查询,是可能的,但需要一些预处理,然后才能 发去后端.

> \[尖叫声\] 只有静态版本 `@keep(ifFeature: "name")` 工作,因为它可以在两个客户端上静态推断 和服务器。 请不要使用 `@keep(if: $someVariable)` 由于变量不会转移到 服务器侧并无法分析 .

库显示功能 `removeIgnoreSetsFromDocument` 删除任何 `@keep` 指令。 。 . 确定你还有 `KeepLink` 在您的管道( 这样它就可以处理 `null` 正确值)

#### 示例

```ts
import {removeIgnoreSetsFromDocument} from '@freshcells/apollo-link-field-keep'
import {
    getDefaultValues,
    removeConnectionDirectiveFromDocument,
} from 'apollo-utilities'
import {parse, print} from 'graphql/index'

export const removeClientDirectives = <T extends Record<string, unknown>>(
    query: string,
    variables: T,
    featureList: string[]
) => {
    const parsedQuery = parse(query)
    // we have to restore the default variables, as they might have been removed by the keepLink before sending them to the server.
    const defaults = getDefaultValues(
        parsedQuery.definitions.find((d) => d.kind === 'OperationDefinition')
    )
    const nextQuery = removeIgnoreSetsFromDocument(
        removeConnectionDirectiveFromDocument(parsedQuery),
        {...defaults, ...variables},
        featureList
    )
    return print(nextQuery.modifiedDoc)
}
```

## 常见问题

### @freshcells/apollo-link-field-keep 是什么？

类似 @include 财务报告和审定财务报表 @skip,但允许在发送到服务器之前完全绕过查询的某些部分

### 如何安装 @freshcells/apollo-link-field-keep？

在项目目录运行 npm install @freshcells/apollo-link-field-keep，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@apollo/client（\>=3）、apollo-utilities（\>=1）、graphql（\>=15）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @freshcells/apollo-link-field-keep 支持 TypeScript 吗？

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

### @freshcells/apollo-link-field-keep 支持 ESM 还是 CommonJS？

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

### @freshcells/apollo-link-field-keep 可以商用吗？

@freshcells/apollo-link-field-keep 使用 未声明 许可。package\.json 没有声明许可证。没有许可证的代码默认保留全部权利，商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括，不构成法律意见，请以许可原文为准。

### @freshcells/apollo-link-field-keep 的下载量有多少？

根据 npm 官方下载统计，@freshcells/apollo-link-field-keep 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,812 次，日均约 127 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @freshcells/apollo-link-field-keep 还在维护吗？

最新版本 1\.3\.1 发布于 2025/07/03（1\.4 年前）；至今共发布 18 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/08/13。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

