@freshcells/apollo-link-field-keep

中文文档
1.3.1发布于 2025/07/03TypeScriptESM + CJS零依赖近 30 天下载 3812

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

中文使用文档
文档目录4 节
  1. apollo-link-field-keep (英语)
  2. 使用大小写
  3. 设置
  4. 被质疑的问题

semantic-release GitHub license badge npm codecov

警告语

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

API 文档

使用大小写

  • 特征
  • 联邦计划

设置

备查 npm 软件包登记册

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

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

使用量

    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 指令如下:

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

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

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

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

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

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

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 正确值)

示例

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 共发布 18 个版本,首次发布于 2021/08/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.1latest2025/07/031.4 年前
1.3.02025/07/031.4 年前
1.2.42025/06/291.4 年前
1.2.32025/06/291.4 年前
1.2.22025/06/291.4 年前
1.2.12025/06/281.4 年前

关于 @freshcells/apollo-link-field-keep 的常见问题

@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 和提交记录判断。