Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache persist problem. #67

Open
Yasir5247 opened this issue May 23, 2023 · 1 comment
Open

cache persist problem. #67

Yasir5247 opened this issue May 23, 2023 · 1 comment

Comments

@Yasir5247
Copy link

cache.ts

import {
  InvalidationPolicyCache,
  RenewalPolicy,
} from '@nerdwallet/apollo-cache-policies';
import {offsetLimitPagination} from '@apollo/client/utilities';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {persistCache} from 'apollo3-cache-persist';

//types
import {Gender, Message, MessagesQuery} from '../../_generated/graphql';
import {stores} from '../../stores';

//services
import {captureException} from '../../utils/sentry';

//custom funcs
import {
  convertToThaanaDate,
  formatDate,
  formatDhivehName,
  formatLicenceDate,
  formatTimeDate,
  stringCutter,
} from '../../utils/utilFuncs';

export const cache = new InvalidationPolicyCache({
  typePolicies: {
    //used for single person type.
    Person: {
      fields: {
        formatedDate: {
          read(_, {readField}) {
            const date = readField('birthDate') as string;
            return stores.ui.language === 'en'
              ? formatDate(date)
              : convertToThaanaDate(date);
          },
        },
      },
    },
    Expenditure: {
      fields: {
        formatedAmount: {
          read(_, {readField}) {
            const amount = readField('amount') as string;
            return `${Number(amount).toFixed(2)} MVR`;
          },
        },
      },
    },
    Transactions: {
      fields: {
        formattedSPName: {
          read(_, {readField}) {
            const subject = readField('serviceProvider') as string;
            return stringCutter(subject, 50);
          },
        },
        formatedAmount: {
          read(_, {readField}) {
            const amount = readField('amount') as string;
            return `${Number(amount).toFixed(2)} MVR`;
          },
        },
        formattedDate: {
          read(_, {readField}) {
            const date = readField('at') as Date;
            return formatTimeDate(date);
          },
        },
      },
    },
    ViugaEmployment: {
      // keyFields: ['recordCardNo'],
      fields: {
        formatedStartDate: {
          read(_, {readField}) {
            const date = readField('startDate') as string;
            return stores.ui.language === 'en'
              ? formatDate(date)
              : convertToThaanaDate(date);
          },
        },
      },
    },
    Query: {
      fields: {
        readMessages: {
          read(_, {readField}) {
            return readField<MessagesQuery[]>('messages') ?? [];
          },
        },
      },
    },
  },
  invalidationPolicies: {
    timeToLive: 3600 * 1000 * 24, // 24hr TTL on all types in the cache
    renewalPolicy: RenewalPolicy.AccessAndWrite,
    types: {
      Expenditure: {
        timeToLive: 3600 * 1000, // 1hr TTL on all types in the cache
        renewalPolicy: RenewalPolicy.AccessAndWrite,
      },
      Transactions: {
        timeToLive: 3600 * 1000, // 1hr TTL on all types in the cache
        renewalPolicy: RenewalPolicy.AccessAndWrite,
      },
    },
  },
});

persistCache({
  cache,
  storage: AsyncStorage,
  debug: false,
  trigger: 'background',
  maxSize: false,
}).catch(error => {
  captureException(error);
  console.log('persist cache error', error);
}); 

apollo.ts

import {cache} from './cache';

export const client = new ApolloClient({
  cache,
  link: from([sentryLink, errorLink, retryLink, authLink, httpLink]),
  assumeImmutableResults: false,
  connectToDevTools: false,
  defaultOptions: {
    watchQuery: {
      fetchPolicy: 'cache-and-network',
      errorPolicy: 'none',
    },
    query: {
      fetchPolicy: 'cache-first',
      errorPolicy: 'all',
    },
  },
});

@danReynolds here, i want to keep all types forever except Expenditure and Transactions. which lives for 1 hour.
here is my setup. am i doing everything good? when the network is down i sure don't see any information displayed on my app.

@danReynolds
Copy link
Collaborator

Sorry checking in here, is there still an issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants