import type { Request } from 'express'; import DataLoader from 'dataloader'; /** * Base class for Pastoria GraphQL contexts. * * Extend this class to create a custom context for your GraphQL resolvers, * then use it in your `pastoria/environment.ts` file: * * @example * ```typescript * // src/context.ts * export class Context extends PastoriaRootContext { * get userId() { * return this.req.session?.userId; * } * * static async createFromRequest(req: Request): Promise { * const ctx = new this(req); * // Perform async initialization (e.g., load user from database) * return ctx; * } * } * * // pastoria/environment.ts * export default new PastoriaEnvironment({ * schema: getSchema(), * createContext: (req) => Context.createFromRequest(req), * }); * ``` */ export declare class PastoriaRootContext { /** The Express request object */ readonly req: Request; private readonly DERIVED_CACHE; constructor(req: Request); /** * Factory method for creating context instances from Express requests. * Override this method in your subclass to add async initialization logic. * * @param req - The Express request object * @returns A promise that resolves to the context instance */ static createFromRequest(req: Request): Promise; /** * Memoizes a derived value for the lifetime of this context. * Useful for caching computed values that are expensive to create. * * @param key - Unique identifier for the cached value * @param make - Factory function to create the value if not cached * @returns The cached or newly created value * * @example * ```typescript * // In a GraphQL resolver * const expensiveData = context.derived('myData', () => { * return computeExpensiveValue(); * }); * ``` */ derived: (key: string, make: () => T) => T; /** * Creates a DataLoader for batch loading entities. * The DataLoader is cached for the lifetime of this context. * * @param key - Unique identifier for the DataLoader * @param batchLoadFn - Function to batch load entities * @returns A DataLoader instance * * @example * ```typescript * // In a GraphQL resolver * const userLoader = context.loader('users', async (ids) => { * const users = await db.users.findMany({ where: { id: { in: ids } } }); * return ids.map(id => users.find(u => u.id === id)); * }); * const user = await userLoader.load(userId); * ``` */ loader: (key: string, batchLoadFn: DataLoader.BatchLoadFn) => DataLoader; } //# sourceMappingURL=context.d.ts.map