dataIdFromObject 错误地覆盖了来自其他查询的缓存 [英] dataIdFromObject overwriting cache from other queries incorrectly

查看:19
本文介绍了dataIdFromObject 错误地覆盖了来自其他查询的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此使用 dataIdFromObject 如下:

cache: new InMemoryCache({
      dataIdFromObject: object => {
        switch (object.__typename) {
          case 'AppKey':
            return object.appKeyId
          case 'App':
            return object.appId
          default:
            return defaultDataIdFromObject(object)
        }
      },
    })

是一些如何从 app 重写 appKey 中的第一个 object.name 的方法,有时反之亦然.例如

is some how rewriting the first object.name in appKey from app, or sometimes vice versa. For example

data.getAppKeys = [{ appKeyId: 1, name: 'My App' }, ...correctObjects] 当后端的密钥为 {appKeyId: 1, name:'myAppKey'}.dataIdFromObject 注释掉任一 case 时不会发生这种情况.

data.getAppKeys = [{ appKeyId: 1, name: 'My App' }, ...correctObjects] when the backend has the key as {appKeyId: 1, name: 'myAppKey'}. This doesn't occur when commenting out either of the cases from dataIdFromObject.

如何让缓存重写正确的查询?

How can I get the cache to rewrite the correct queries?

推荐答案

你需要有一个超过数字的唯一标识符,因为数字会匹配并且缓存会错误地覆盖,解决方案是添加 __typename 像这样输入数据标识符:

You need to have a unique identifier that goes beyond a number, because the numbers will match and the cache will overwrite incorrectly, the solution was is add the __typename into the data identifier like so:

dataIdFromObject: object => {
        const getID = (typename, id) => `${typename}_${id}`

        switch (object.__typename) {
          case 'AppKey':
            return getID(object.__typename, object.appKeyId)
          case 'App':
            return getID(object.__typename, object.appId)
          default:
            return getId(object.__typename, defaultDataIdFromObject(object))
        }
      },
    })

这篇关于dataIdFromObject 错误地覆盖了来自其他查询的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆