Skip to content
This repository was archived by the owner on Apr 6, 2026. It is now read-only.
This repository was archived by the owner on Apr 6, 2026. It is now read-only.

cache persist problem.  #67

@Yasir5247

Description

@Yasir5247

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions