fix(trading): update cached Order objects (#3450)

This commit is contained in:
Bartłomiej Głownia 2023-04-18 14:56:13 +02:00 committed by GitHub
parent 045dace274
commit ec12c7ecfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

View File

@ -20,6 +20,7 @@ import type {
OrdersQueryVariables,
} from './__generated__/Orders';
import { OrdersDocument, OrdersUpdateDocument } from './__generated__/Orders';
import type { ApolloClient } from '@apollo/client';
export type Order = Omit<OrderFieldsFragment, 'market'> & {
market?: Market;
@ -81,8 +82,31 @@ const getData = (
): Edge<OrderFieldsFragment>[] =>
responseData?.party?.ordersConnection?.edges || [];
const getDelta = (subscriptionData: OrdersUpdateSubscription) =>
subscriptionData.orders || [];
const getDelta = (
subscriptionData: OrdersUpdateSubscription,
variables: OrdersQueryVariables,
client: ApolloClient<object>
) => {
if (!subscriptionData.orders) {
return [];
}
subscriptionData.orders.forEach((order) => {
client.cache.modify({
id: client.cache.identify({
__typename: 'Order',
id: order.id,
}),
fields: {
price: () => order.price,
size: () => order.size,
remaining: () => order.remaining,
updatedAt: () => order.updatedAt,
status: () => order.status,
},
});
});
return subscriptionData.orders;
};
const getPageInfo = (responseData: OrdersQuery): PageInfo | null =>
responseData.party?.ordersConnection?.pageInfo || null;

View File

@ -104,7 +104,11 @@ interface GetTotalCount<QueryData> {
}
interface GetDelta<SubscriptionData, Delta, Variables> {
(subscriptionData: SubscriptionData, variables?: Variables): Delta;
(
subscriptionData: SubscriptionData,
variables: Variables,
client: ApolloClient<object>
): Delta;
}
export type Node = { id: string };
@ -420,7 +424,7 @@ function makeDataProviderInternal<
if (!subscriptionData || !getDelta || !update) {
return;
}
const delta = getDelta(subscriptionData, variables);
const delta = getDelta(subscriptionData, variables, client);
if (loading) {
updateQueue.push(delta);
} else {