UNPKG

@quell/server

Version:

Quell is an open-source NPM package providing a light-weight caching layer implementation and cache invalidation for GraphQL responses on both the client- and server-side. Use Quell to prevent redundant client-side API requests and to minimize costly serv

81 lines 5.17 kB
import { Response, Request, NextFunction } from "express"; import { RedisClientType } from "redis"; import { GraphQLSchema } from "graphql"; import type { ConstructorOptions, IdCacheType, CostParamsType, MutationMapType, QueryMapType, FieldsMapType, RequestType } from "./types/types"; import type { BuildFromCacheFunction, GenerateCacheIDFunction } from "./types/readCacheTypes"; import type { WriteToCacheFunction, UpdateIdCacheFunction } from "./types/writeCacheTypes"; import type { NormalizeForCacheFunction } from "./cacheOperations/normalizeCache"; import type { UpdateCacheByMutationFunction } from "./types/updateCacheTypes"; import type { ClearCacheFunction, DeleteCacheByIdFunction, ClearAllCachesFunction } from "./types/invalidateCacheTypes"; import type { BuildCacheFromResponseFunction, BuildCacheFromMergedResponseFunction, HandleQueryCachingFunction } from "./types/buildCacheTypes"; /** * Creates a QuellCache instance that provides middleware for caching between the graphQL endpoint and * front-end requests, connects to redis cloud store via user-specified parameters. * - If there is no cache expiration provided by the user, cacheExpiration defaults to 14 days in seconds. * - If there are no cost parameters provided by the user, costParameters is given the default values. * - If redisPort, redisHost, and redisPassword are omitted, will use local Redis instance. * See https://redis.io/docs/getting-started/installation/ for instructions on installing Redis and starting a Redis server. * @param {ConstructorOptions} options - The options to use for the cache. * @param {GraphQLSchema} options.schema - GraphQL defined schema that is used to facilitate caching by providing valid queries, * mutations, and fields. * @param {number} [options.cacheExpiration=1209600] - Time in seconds for redis values to be evicted from the cache. Defaults to 14 days. * @param {CostParamsType} [options.costParameters=defaultCostParams] - The cost parameters to use for caching. Defaults to: * - maxCost: 5000 (maximum cost allowed before a request is rejected) * - mutationCost: 5 (cost of a mutation) * - objectCost: 2 (cost of retrieving an object) * - scalarCost: 1 (cost of retrieving a scalar) * - depthCostFactor: 1.5 (multiplicative cost of each depth level) * - maxDepth: 10 (depth limit parameter) * - ipRate: 3 (requests allowed per second) * @param {number} options.redisPort - (optional) The Redis port to connect to. * @param {string} options.redisHost - (optional) The Redis host URI to connect to. * @param {string} options.redisPassword - (optional) The Redis password to the host URI. * @example // Omit redisPort, redisHost, and redisPassword to use a local Redis instance. * const quellCache = new QuellCache({ * schema: schema, * cacheExpiration: 3600, // 1 hour in seconds * }); */ export declare class QuellCache { idCache: IdCacheType; schema: GraphQLSchema; costParameters: CostParamsType; queryMap: QueryMapType; mutationMap: MutationMapType; fieldsMap: FieldsMapType; cacheExpiration: number; redisReadBatchSize: number; redisCache: RedisClientType; costLimit: (req: Request, res: Response, next: NextFunction) => void; depthLimit: (req: Request, res: Response, next: NextFunction) => void; rateLimiter: (req: Request, res: Response, next: NextFunction) => Promise<void>; buildFromCache: BuildFromCacheFunction; generateCacheID: GenerateCacheIDFunction; writeToCache: WriteToCacheFunction; normalizeForCache: NormalizeForCacheFunction; updateIdCache: UpdateIdCacheFunction; updateCacheByMutation: UpdateCacheByMutationFunction; clearCache: ClearCacheFunction; deleteCacheById: DeleteCacheByIdFunction; clearAllCaches: ClearAllCachesFunction; buildCacheFromResponse: BuildCacheFromResponseFunction; buildCacheFromMergedResponse: BuildCacheFromMergedResponseFunction; handleQueryCaching: HandleQueryCachingFunction; constructor({ schema, cacheExpiration, // Default expiry time is 14 days in seconds costParameters, redisPort, redisHost, redisPassword, }: ConstructorOptions); /** * The class's controller method. It: * - reads the query string from the request object, * - tries to construct a response from cache, * - reformulates a query for any data not in cache, * - passes the reformulated query to the graphql library to resolve, * - joins the cached and uncached responses, * - decomposes and caches the joined query, and * - attaches the joined response to the response object before passing control to the next middleware. * @param {Request} req - Express request object, including request body with GraphQL query string. * @param {Response} res - Express response object, will carry query response to next middleware. * @param {NextFunction} next - Express next middleware function, invoked when QuellCache completes its work. */ query(req: RequestType, res: Response, next: NextFunction): Promise<void>; } //# sourceMappingURL=QuellCache.d.ts.map