import { ImplementableObjectRef, type ObjectRef, type SchemaTypes } from '@pothos/core'; import type { DataLoaderOptions, LoadableNodeId } from '../types.js'; import { dataloaderGetter } from '../util.js'; import { LoadableObjectRef } from './object.js'; export class LoadableNodeRef< Types extends SchemaTypes, RefShape, Shape, IDShape extends bigint | number | string = string, Key extends bigint | number | string = IDShape, CacheKey = Key, > extends LoadableObjectRef { parseId: ((id: string, ctx: object) => IDShape) | undefined; builder: PothosSchemaTypes.SchemaBuilder; constructor( builder: PothosSchemaTypes.SchemaBuilder, name: string, { id, loaderOptions, load, toKey, sort, }: DataLoaderOptions & LoadableNodeId, ) { super(name, dataloaderGetter(loaderOptions, load, toKey, sort)); this.builder = builder; this.parseId = id.parse; } } export class ImplementableLoadableNodeRef< Types extends SchemaTypes, RefShape, Shape, IDShape extends bigint | number | string = string, Key extends bigint | number | string = IDShape, CacheKey = Key, > extends ImplementableObjectRef { parseId: ((id: string, ctx: object) => IDShape) | undefined; getDataloader; protected cacheResolved; constructor( builder: PothosSchemaTypes.SchemaBuilder, name: string, { id, loaderOptions, load, toKey, sort, cacheResolved, }: DataLoaderOptions & LoadableNodeId, ) { super(builder, name); this.parseId = id.parse; this.builder = builder; this.getDataloader = dataloaderGetter(loaderOptions, load, toKey, sort); this.cacheResolved = typeof cacheResolved === 'function' ? cacheResolved : cacheResolved && toKey; ( this.builder as typeof builder & { nodeRef: (ref: ObjectRef, options: Record) => void; } ).nodeRef(this, { id, loadManyWithoutCache: (ids: Key[], context: SchemaTypes['Context']) => this.getDataloader(context).loadMany(ids), }); this.updateConfig((config) => ({ ...config, extensions: { ...config.extensions, getDataloader: this.getDataloader, cacheResolved: this.cacheResolved, }, })); } }