Apollo client modify.cache after receiving data from the subscription sometimes the cache does not update with new data

Apollo client modify.cache after receiving data from the subscription sometimes the cache does not update with new data


0

I am updating the real-time status of the product data with a subscription. Here is my code

useProductSubscription({
    varibales: {
      ids: productIds
    },
    onData: ({ data }) => {
      const dataUpdated = data.productUpdated;
      if (!dataUpdated) return;
      const { productId, status } = dataUpdated;

      cache.modify({
        id: cache.indentify({
        __typename: 'Product',
        id: productId,
      }),
      fields: {
        status: () => status,
      }
    });
  },
});

The data returned from the subscription update is fine, and I’m using the modify.cache function to update the cached data.

However, sometimes it does not update with the correct latest status and appears to be out of sync. This issue consistently occurs when there are frequent data updates from the subscription within a short period of time. Do you have any ideas about this problem, or could it be that I’m doing something wrong here? Thanks.

Here is data from Apollo Dev Tool

Payload return from subscription

ProductUpdatedPayload:a9297616-8335-4a6d-92fb-d4fb1bb7ab6c {
  __typename: Product
  id: "a9297616-8335-4a6d-92fb-d4fb1bb7ab6c"
  status: "OPEN"
}

Product cache not update correctly

Product:a9297616-8335-4a6d-92fb-d4fb1bb7ab6c {
  id: "a9297616-8335-4a6d-92fb-d4fb1bb7ab6c"
  status: "CLOSED"
}

I would like update correct latest status as real-time and dont out of sync when receive data from subscription

New contributor

StreetSpot396 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *