[#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: { Instrument: {
keyFields: false, keyFields: false,
}, },
MarketData: {
keyFields: ['market', ['id']],
},
Node: { Node: {
keyFields: false, keyFields: false,
}, },

View File

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