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