import { ImplementableInterfaceRef, type InterfaceParam, InterfaceRef, type InterfaceTypeOptions, type SchemaTypes, } from '@pothos/core'; import type DataLoader from 'dataloader'; import type { DataLoaderOptions } from '../types.js'; import { dataloaderGetter } from '../util.js'; export class LoadableInterfaceRef< Types extends SchemaTypes, RefShape, Shape, Key, CacheKey, > extends InterfaceRef { getDataloader; constructor( name: string, getDataloader: (context: Types['Context']) => DataLoader, ) { super(name); this.getDataloader = getDataloader; } } export class ImplementableLoadableInterfaceRef< Types extends SchemaTypes, RefShape, Shape, Key extends bigint | number | string, CacheKey, > extends ImplementableInterfaceRef { cacheResolved; getDataloader; constructor( builder: PothosSchemaTypes.SchemaBuilder, name: string, { loaderOptions, load, toKey, sort, cacheResolved, }: DataLoaderOptions, ) { super(builder, name); this.getDataloader = dataloaderGetter( loaderOptions, load as never, toKey, sort, ); this.cacheResolved = typeof cacheResolved === 'function' ? cacheResolved : cacheResolved && toKey; } override implement[]>( options: InterfaceTypeOptions< Types, ImplementableInterfaceRef, Shape, Interfaces >, ): InterfaceRef { return super.implement({ ...options, extensions: { ...options.extensions, getDataloader: this.getDataloader, cacheResolved: this.cacheResolved, }, }); } }