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

标签 reactjs apollo-client graphql-js

这是我的代码

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
    }
  }
`;
当我提出请求时,我收到警告

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

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:

    {"__typename":"My","getUser{"__typename":"User","email":"shakizriker0022@gmail.com"}}

incoming: {"__typename":"My","sendNewTokenForConfirmation":"SUCCESS"}

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


我跟着链接。
我阅读了文档并意识到问题出在 apollo 客户端缓存(typePolicies)中。
但是我应该如何解决这个问题,我只是想不通。
我应该在 typePolicies 中写什么来摆脱警告?

最佳答案

您可能需要为 Apollo 返回一个 id 以在缓存中唯一标识该对象。
我认为这个问题与您的相似:
link

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

关于reactjs - 替换 Query 对象的 my 字段时缓存数据可能会丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63423578/

相关文章:

javascript - ReactJS - 将数据从子组件传递到其父组件

reactjs - Material UI 日期范围选择器

android - 限制 NativeBase 文本的长度

graphql - 使用 NEXT.js 和 Apollo 客户端 2 配置订阅

javascript - 为什么我无法使用 Apollo Link State 添加待办事项?

node.js - 如何从导致突变失败的 graphql 响应中删除 `__typename` 字段

node.js - 在 Google Cloud App Engine 上运行 GraphQL 服务器

javascript - "export ' createNetworkInterface ' was not found in ' Apollo 客户端'

javascript - 如何查询动态数量的别名?

javascript - 在 React 中使用本地 .json 文件时,import 和 fetch() 有什么区别