替换 Query 对象的 my 字段时缓存数据可能会丢失 [英] Cache data may be lost when replacing the my field of a Query object

查看:18
本文介绍了替换 Query 对象的 my 字段时缓存数据可能会丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

const NewVerificationCode = () => {
  const { loading, error, data = {}, refetch } = useQuery(CONFIRMATION_CODE, {
    skip: true,
    onError: (err) => {},
  });
  console.log(loading, error, data);

  if (loading || error) {
    return <ErrorLoadingHandler {...{ loading, error }} />;
  }

  return (
    <form
      onSubmit={(e) => {
        refetch();
        e.preventDefault();
      }}
    >
      <div>
        <button type="submit" className="signUpbutton">
          {"Send the message again"}
        </button>
      </div>
    </form>
  );
};

const CONFIRMATION_CODE = gql`
  query {
    my {
      sendNewTokenForConfirmation
    }
  }
`;

</表单>);};const CONFIRMATION_CODE = gql`询问 {我的 {发送新令牌确认}}`;

when i make a request i get a warning

当我提出请求时,我收到警告

Cache data may be lost when replacing the my field of a Query object.

替换 Query 对象的 my 字段时缓存数据可能会丢失.

To address this problem (which is not a bug in Apollo Client), either ensure all >objects of type My have IDs, or define a custom merge function for the Query.my >field, so InMemoryCache can safely merge these objects existing:

为了解决这个问题(这不是 Apollo Client 中的错误),要么确保所有类型为 My 的对象都有 ID,要么为 Query.my > 字段定义一个自定义合并函数,这样 InMemoryCache 就可以安全地合并这些对象现有:


<块引用>

有关这些选项的更多信息,请参阅文档:

For more information about these options, please refer to the documentation:

我按照链接操作.

我阅读了文档并意识到问题出在 apollo 客户端缓存 (typePolicies) 中.

I read the documentation and realized that the problem is in the apollo client cache (typePolicies).

但是我不知道该怎么解决这个问题.

But how should I solve this problem I just can't figure out.

我应该在 typePolicies 中写什么来消除警告?.

What should i write in typePolicies to get rid of the warning ?.

推荐答案

您可能需要为 Apollo 返回一个 ID,以便在缓存中唯一标识该对象.我认为这个问题与您的类似:链接

You may need to return an id for Apollo to uniquely identify that object in the cache. I think this issue is similar to yours: link

const CONFIRMATION_CODE = gql`
  query {
    my {
      id
      sendNewTokenForConfirmation
    }
  }
`;

这篇关于替换 Query 对象的 my 字段时缓存数据可能会丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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