@soundxyz/response-cache
Version:
Heavily inspired by @envelop/response-cache
30 lines (29 loc) • 1.11 kB
TypeScript
import type { Maybe, PromiseOrValue } from "@envelop/core";
import type { ExecutionResult } from "graphql";
export type CacheEntityRecord = {
typename: string;
id?: number | string;
};
/**
* Interface for implementing a cache that will be used for `useResponseCache`.
*/
export type Cache = {
/** set a cache response */
set(
/** id/hash of the operation */
id: string,
/** the result that should be cached */
data: ExecutionResult,
/** array of entity records that were collected during execution */
entities: Iterable<CacheEntityRecord>,
/** how long the operation should be cached */
ttl: number): PromiseOrValue<void>;
/** get a cached response */
get(id: string): PromiseOrValue<readonly [Maybe<ExecutionResult>, ({
ttl?: number;
} | undefined)?]>;
/** invalidate operations via typename or id */
invalidate(entities: Iterable<CacheEntityRecord>): PromiseOrValue<void>;
/** Function to be called when the operation couldn't be cached, by default, if the execution had errors */
onSkipCache(id: string): void;
};