import { ImplementableObjectRef, ObjectRef, type SchemaTypes } from '@pothos/core'; import type DataLoader from 'dataloader'; import type { DataLoaderOptions } from '../types.js'; import { dataloaderGetter } from '../util.js'; export class LoadableObjectRef< Types extends SchemaTypes, RefShape, Shape, Key, CacheKey, > extends ObjectRef { getDataloader; constructor( name: string, getDataloader: (context: Types['Context']) => DataLoader, ) { super(name); this.getDataloader = getDataloader; } } export class ImplementableLoadableObjectRef< Types extends SchemaTypes, RefShape, Shape, Key extends bigint | number | string, CacheKey, > extends ImplementableObjectRef { getDataloader; protected cacheResolved; constructor( builder: PothosSchemaTypes.SchemaBuilder, name: string, { loaderOptions, load, toKey, sort, cacheResolved, }: DataLoaderOptions, ) { super(builder, name); this.getDataloader = dataloaderGetter(loaderOptions, load, toKey, sort); this.cacheResolved = typeof cacheResolved === 'function' ? cacheResolved : cacheResolved && toKey; this.builder.configStore.onTypeConfig(this, (config) => { config.extensions = { ...config.extensions, getDataloader: this.getDataloader, cacheResolved: this.cacheResolved, }; }); } }