@stewartmcgown/apollo-response-cache
Version:
Caching and invalidation mechanisms (plugins, directives) of Apollo GraphQL
55 lines (54 loc) • 1.94 kB
TypeScript
import { ApolloServerPlugin } from 'apollo-server-plugin-base';
import { ResponsePath } from 'graphql';
export interface CacheControlFormat {
version: 1;
hints: ({
path: (string | number)[];
} & CacheHint)[];
}
export declare enum CacheScope {
Public = "PUBLIC",
Private = "PRIVATE"
}
export interface CacheHint {
maxAge?: number;
scope?: CacheScope;
staleWhileRevalidate?: number;
}
export declare function makeCacheHint(hint: CacheHint): string;
export declare function makeCacheControlHeader(overallCachePolicy: Required<CacheHint>): string;
export interface CacheControlExtensionOptions {
/**
* Apply this maxAge to every object in the schema automatically. You can override
* this maxage with the @cacheControl(maxAge: N) directive.
*/
defaultMaxAge?: number;
/**
* Apply this staleWhileRevalidate to every object in the schema automatically. You can override
* this maxage with the @cacheControl(maxAge: N, staleWhileRevalidate: X) directive.
*/
defaultStaleWhileRevalidate?: number;
/**
* Calculate the cache-control HTTP header based on the maxage and swr
* of the response
*
* @default true
*/
calculateHttpHeaders?: boolean;
stripFormattedExtensions?: boolean;
/**
* Cache mutation responses using cacheControl hints
*
* @default false
*/
cacheMutations?: boolean;
}
declare type MapResponsePathHints = Map<ResponsePath, CacheHint>;
export declare const plugin: (options?: CacheControlExtensionOptions) => ApolloServerPlugin;
declare function computeOverallCachePolicy(hints: MapResponsePathHints): Required<CacheHint> | undefined;
declare function addHint(hints: MapResponsePathHints, path: ResponsePath, hint: CacheHint): void;
export declare const __testing__: {
addHint: typeof addHint;
computeOverallCachePolicy: typeof computeOverallCachePolicy;
};
export {};