[#128] fix MarketData cache issue, add default cachePolicy to data provider

This commit is contained in:
Bartłomiej Głownia 2022-03-29 13:48:39 +02:00
parent 7ba74eeafd
commit 7e74615bf6
2 changed files with 12 additions and 7 deletions

View File

@ -33,9 +33,6 @@ export function createClient(base?: string) {
Instrument: {
keyFields: false,
},
MarketData: {
keyFields: ['market', ['id']],
},
Node: {
keyFields: false,
},

View File

@ -3,6 +3,7 @@ import type { Draft } from 'immer';
import type {
ApolloClient,
DocumentNode,
FetchPolicy,
TypedDocumentNode,
OperationVariables,
} from '@apollo/client';
@ -45,7 +46,8 @@ function makeDataProviderInternal<QueryData, Data, SubscriptionData, Delta>(
subscriptionQuery: Query<SubscriptionData>,
update: Update<Data, Delta>,
getData: GetData<QueryData, Data>,
getDelta: GetDelta<SubscriptionData, Delta>
getDelta: GetDelta<SubscriptionData, Delta>,
fetchPolicy: FetchPolicy = 'no-cache'
): Subscribe<Data, Delta> {
const callbacks: UpdateCallback<Data, Delta>[] = [];
const updateQueue: Delta[] = [];
@ -104,7 +106,11 @@ function makeDataProviderInternal<QueryData, Data, SubscriptionData, Delta>(
}
});
try {
const res = await client.query<QueryData>({ query, variables });
const res = await client.query<QueryData>({
query,
variables,
fetchPolicy,
});
data = getData(res.data);
if (data && updateQueue && updateQueue.length > 0) {
data = produce(data, (draft) => {
@ -175,7 +181,8 @@ export function makeDataProvider<QueryData, Data, SubscriptionData, Delta>(
subscriptionQuery: Query<SubscriptionData>,
update: Update<Data, Delta>,
getData: GetData<QueryData, Data>,
getDelta: GetDelta<SubscriptionData, Delta>
getDelta: GetDelta<SubscriptionData, Delta>,
fetchPolicy: FetchPolicy = 'no-cache'
): Subscribe<Data, Delta> {
const getInstance = memoize<Data, Delta>((variables) =>
makeDataProviderInternal(
@ -183,7 +190,8 @@ export function makeDataProvider<QueryData, Data, SubscriptionData, Delta>(
subscriptionQuery,
update,
getData,
getDelta
getDelta,
fetchPolicy
)
);
return (callback, client, variables) =>