import { initContextCache } from '@pothos/core'; import type { LoadableRef } from '../../src'; import { User } from './schema/objects'; import type { ContextType } from './types'; let nextID = 0; export const createContext = (): ContextType => { const callCounts = new Map(); const context: ContextType = { id: nextID++, ...initContextCache(), get userLoader() { return User.getDataloader(context); }, get getLoader() { return (ref: LoadableRef) => ref.getDataloader(context); }, get load() { return (ref: LoadableRef, id: K) => ref.getDataloader(context).load(id); }, get loadMany() { return (ref: LoadableRef, ids: K[]) => ref.getDataloader(context).loadMany(ids); }, callCounts, countCall: (name: string) => { const count = callCounts.get(name) ?? 0; callCounts.set(name, count + 1); }, }; return context; };