0

我调用 subscribeToMore 函数,然后在 updateQuery 中得到新订单,我还需要更改状态缓存,即具有特定状态的订单数量,为此我使用 client.cache.modify。但是,如果我在没有 setTimeout 的情况下使用 client.cache.modify(或延迟 <500ms)。然后新订单不会与订单一起进入表格。如果我删除 client.cache.modify 一切正常

subscribeToMore({
        document: SUBSCRIPTION_ORDERS,
        updateQuery(prev: any, {subscriptionData: {data: {orders}}}) {
        if (!orders.orders.length) return prev;
        setTimeout(() => {
            orders.orders.forEach(order => {
              const tempData = orderStatusesData?.orderStatuses.find(orderStatus => orderStatus.slug === order.status);
              if (tempData) {
                 client.cache.modify({
                    id: client.cache.identify(tempData),
                       fields: {
                          count(cachedCount) {
                              return cachedCount + 1
                          }
                      }
                 })
               }
            })
         }, 600)

        const newOrders = orders.orders.filter(order => (order?.status === localStorage.getItem('orderStatus') || localStorage.getItem('orderStatus') == null) &&
        (order.deliveries[0].orderProducts[0].storeProduct.store.id === localStorage.getItem('storeId') || localStorage.getItem('storeId') == null))

         if (newOrders.length) {
                return Object.assign({}, prev, {
                        orders: {
                            orders: [...newOrders, ...prev.orders.orders],
                            count: prev.orders.count + newOrders.length,
                            __typename: prev.orders.__typename
                        }
                    });
                }
            }
 });

此代码适用于 setTimeout。但是如果没有 setTimeout 或 delay === 0 则不起作用。当我不使用 setTimeout 时,client.cache.modify 工作正常,但订单不会更新。我需要 client.cache.modify 工作并更新订单。如何才能做到这一点?如何删除 setTimeout 使其工作?

4

0 回答 0