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

心不动则不痛 提交于 2021-01-04 07:59:12

问题


this my code

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
    }
  }
`;

when i make a request i get a warning

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:

I followed the links.

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.

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


回答1:


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
    }
  }
`;


来源:https://stackoverflow.com/questions/63423578/cache-data-may-be-lost-when-replacing-the-my-field-of-a-query-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!