删除特定查询的 __typename

问题描述

我正在使用 @apollo/client 并在我的应用中查询图形协议。除了未使用 __typename 字段的 _meta 查询外,所有查询都可以很好地与 __typename 配合使用。 所以我从那个查询中得到这个错误 - Null value resolved for non-null field '__typename'

我尝试了很多解决方案,但没有一个解决我的问题。

首先,如果我将 addTypename 设置为 false,那么 _meta 查询可以正常工作,但是对于使用 __typename 字段的其他查询,缓存无法正常工作。 我尝试使用中间件来省略该特定查询的 __typename 字段,但奇怪的是我无法从操作/上下文对象中找到任何 __typename 键/值。 我还尝试在中间件的上下文缓存配置中将 addTypename 设置为 false,但没有奏效。

const cleanTypeName = new ApolloLink(
    (operation: Operation,forward: NextLink) => {
        if (operation.operationName === "_meta") {
            if (operation.variables) {
                const omitTypename = (key,value) =>
                    key === "__typename" ? undefined : value
                operation.variables = JSON.parse(
                    JSON.stringify(operation.variables),omitTypename
                )
            }

            const context = operation.getContext()
            operation.setContext({
                ...context,cache: {
                    ...context.cache,addTypename: false,config: {
                        ...context.cache.config,},})
        }

        return forward(operation)
    }
)

实际上,如果我通过 console.log 检查 operation.variables 是空的,因为 _meta 查询不需要传递任何参数。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...