@freshcells/apollo-link-field-keep

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

Similar to @include and @skip, but allows bypassing certain parts of the query completely before sending it to the server

原始使用文档
文档目录4 节
  1. apollo-link-field-keep
  2. Use cases
  3. Setup
  4. Persisted queries

semantic-release GitHub license badge npm codecov

EXPERIMENTAL

Similar to @include and @skip, but allows bypassing certain parts of the query completely before sending it to the server

API Documentation

Use cases

  • Feature toggling
  • Federated schemas

Setup

Available from the npm package registry with

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

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

Usage

    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
    ]),
})

You can then use the @keep directive as:

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

If $shouldKeep in this example is false, it will remove the whole field query including arguments. It will also clean up all variables that might be used (if not used by other fields).

If you have a query that is used in multiple components and needs to include a certain field everywhere, you can use ifFeature instead of if as:

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

It will work exactly the same way as $shouldKeep in the previous example. With this approach you just save time as you do not need to pass the same variables to all query use cases.

It is also possible to combine if and ifFeature:

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

In this case the fields will only be included if both $shouldKeep is true and feature1 is enabled.

To prevent Apollo Cache issues, it will fill the field with null when returned from the server.

Persisted queries

Using apollo-link-field-keep with persisted queries, is possible, but requires some pre-processing, before you can send it to the backend.

[!WARNING] Only the static version @keep(ifFeature: "name") works, because it can be statically inferred on both client and server. Please do not use @keep(if: $someVariable) with persistent queries, as variables won't be transferred to the server side and cannot be analyzed.

The library exposes the function removeIgnoreSetsFromDocument to remove any @keep directives. Make sure you still have KeepLink in your pipeline (so it can handle the null values properly)

Example

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 是什么?

Similar to @include and @skip, but allows bypassing certain parts of the query completely before sending it to the server

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