apollo-link-field-keep (英语)
警告语
类似 @include 财务报告和审定财务报表 @skip,但允许完全绕过查询的某些部分
在发送到服务器之前
使用大小写
- 特征
- 联邦计划
设置
备查 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)
}