import { ApolloCache } from '@apollo/client/core' ; import { ApolloPersistOptions , PersistedData } from './types' ; export default class Cache < T > { cache : ApolloCache < T > ; serialize : boolean ; constructor ( options ) { const { cache , serialize = true } = options ; this . cache = cache ; this . serialize = serialize ; } extract ( ) { let data = this . cache . extract ( ) as T ; if ( this . serialize ) { data = JSON . stringify ( data ) as string ; } return data ; } restore ( data ) { if ( this . serialize && typeof data === 'string' ) { data = JSON . parse ( data ) ; } if ( data != null ) { this . cache . restore ( data as T ) ; } } }