import { type InputFieldMap, type InputFieldsFromShape, type InputRef, inputShapeKey, type PothosInputObjectTypeConfig, type SchemaTypes, } from '../types'; import { BaseTypeRef } from './base'; import type { InputFieldRef } from './input-field'; export class InputObjectRef extends BaseTypeRef implements InputRef, PothosSchemaTypes.InputObjectRef { override kind = 'InputObject' as const; $inferInput!: T; [inputShapeKey]!: T; private fields = new Set<() => InputFieldMap>(); private fieldCbs = new Set<(name: string, ref: InputFieldRef) => void>(); constructor(name: string) { super('InputObject', name); } addFields(fields: () => InputFieldMap) { this.fields.add(fields); for (const cb of this.fieldCbs) { for (const [name, ref] of Object.entries(fields())) { if (ref) { cb(name, ref as InputFieldRef); } } } } onField(cb: (name: string, ref: InputFieldRef) => void) { this.fieldCbs.add(cb); for (const fieldMap of this.fields) { for (const [name, ref] of Object.entries(fieldMap())) { if (ref) { cb(name, ref as InputFieldRef); } } } } } export class ImplementableInputObjectRef< Types extends SchemaTypes, T extends object, Resolved = T, > extends InputObjectRef { builder: PothosSchemaTypes.SchemaBuilder; constructor(builder: PothosSchemaTypes.SchemaBuilder, name: string) { super(name); this.builder = builder; } implement( options: PothosSchemaTypes.InputObjectTypeOptions< Types, InputFieldsFromShape >, ): InputObjectRef { this.builder.inputType< ImplementableInputObjectRef, InputFieldsFromShape >(this as never, options); return this; } }