阿波罗客户端,订阅片段

发布于 2025-02-05 20:29:43 字数 735 浏览 2 评论 0原文

我正在尝试使用cache.ReadFragment读取从缓存中读取,并订阅更改,就像useQuery die一样。我尝试使用usequeryfetchpolicy:'仅缓存',但是尽管我在缓存中看到我的实体,但数据却返回为null。 cache.ReadFragment工作正常,但不订阅,这意味着将其应用于缓存的任何更改都不会重新触发它。

我如何实现这种行为,并在某些东西变为缓存后立即起作用?

  // one time read
  const data = client.cache.readFragment({
    id: client.cache.identify({
      __typename: 'Creator',
      id: creatorId,
    }),
    fragment: IS_FOLLOWING_FRAGMENT,
  }) as MyCreatorIsFollowing;
  // comes back as null although I see it in the cache
  const { data } = useQuery(GET_IS_FOLLOW_CREATOR, {
    fetchPolicy: 'cache-only',
    variables: { id: creatorId },
  });

I am trying to read from the cache, using cache.readFragment and subscribe to the changes just as useQuery does. I tried using useQuery with a fetchPolicy: 'cache-only' but although I see my entity in the cache, the data comes back as null. The cache.readFragment works fine, but it doesn't subscribe, meaning any change applied to the cache won't re-trigger it.

How can I implement that kind of behavior, and have it work right after something is mutated to the cache?

  // one time read
  const data = client.cache.readFragment({
    id: client.cache.identify({
      __typename: 'Creator',
      id: creatorId,
    }),
    fragment: IS_FOLLOWING_FRAGMENT,
  }) as MyCreatorIsFollowing;
  // comes back as null although I see it in the cache
  const { data } = useQuery(GET_IS_FOLLOW_CREATOR, {
    fetchPolicy: 'cache-only',
    variables: { id: creatorId },
  });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文