Apollo client, subscribe to fragment

Viewed 38

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 },
  });
0 Answers
Related