Skip to content

Instantly share code, notes, and snippets.

@qswitcher
Last active May 22, 2019 02:45
Show Gist options
  • Save qswitcher/571a68be214b45462d200a5fec1f40f2 to your computer and use it in GitHub Desktop.
Save qswitcher/571a68be214b45462d200a5fec1f40f2 to your computer and use it in GitHub Desktop.
interface ApolloCache<TSerialized> {
/**********************************************************
* Core API
*********************************************************/
/**
* Returns the cached results for a query if present
* @param query read options
*/
read<TResult, TVariables>(query: ReadOptions<TVariables>): TResult | null;
/**
* Writes the results of a query to the cache.
* @param write write options
*/
write<TResult, TVariables>(write: WriteOptions<TResult, TVariables>): void;
/**
* Returns all the fields of the query that are present in the cache, indicating if
* any fields were missing.
* @param query diff options
*/
diff<TResult, TVariables>(
query: DiffOptions<TVariables>
): DiffResult<TResult>;
/**
* Takes a callback to be executed when the cache is updated.
* @param watch callback
*/
watch<TVariables>(watch: WatchOptions<TVariables>): () => void;
/**
* Clears the cache
*/
reset(): Promise<void>;
/*
* Optional, evicts the results of a query from the cache.
*
* NOTE: InMemoryCache does not implement this method and throws an error
*/
evict<TVariables>(query: EvictOptions<TVariables>): EvictionResult;
/**********************************************************
* Serialization API, required for server side rendering or offline persistence
*********************************************************/
/**
* Populates the cache with the serialized data returned from `extract`
* @param serializedState serialized contents of the cache
*/
restore(serializedState: TSerialized): ApolloCache<TSerialized>;
/**
* Returns a serialized version of the cache contents.
* @param optimistic true if optimistic updates should be included
*/
extract(optimistic?: boolean): TSerialized;
/**********************************************************
* Transational API, only required if you wish to support optimistic updates for mutations
*********************************************************/
/**
* Performs an optimistic update on the cache.
* @param transaction
* @param id transaction ID
*/
recordOptimisticTransaction(
transaction: Transaction<TSerialized>,
id: string
): void;
/**
* Executes a transaction on the given cache.
* @param transation
*/
performTransation(transation: Transaction<TSerialized>): void;
/**
* Rolls back the given optimistic transaction
* @param id transaction ID
*/
removeOptimistic(id: string): void;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment