deno.land / x / pothos@release-1713397530 / packages / core / fieldUtils / input.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116// @ts-nocheckimport InputFieldRef from '../refs/input-field.ts';import InputListRef from '../refs/input-list.ts';import type { ArgBuilder, FieldRequiredness, InputShapeFromTypeParam, InputTypeParam, NormalizeArgs, } from '../types/index.ts';import { InputType, SchemaTypes } from '../types/index.ts';import { inputTypeFromParam } from '../utils/index.ts';export default class InputFieldBuilder<Types extends SchemaTypes, Kind extends keyof PothosSchemaTypes.InputFieldOptionsByKind> { builder: PothosSchemaTypes.SchemaBuilder<Types>; kind: Kind; typename: string; /** * Create a Boolean input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ boolean = this.helper("Boolean"); /** * Create a Float input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ float = this.helper("Float"); /** * Create a ID input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ id = this.helper("ID"); /** * Create a Int input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ int = this.helper("Int"); /** * Create a String input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ string = this.helper("String"); /** * Create a Boolean list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ booleanList = this.helper(["Boolean"]); /** * Create a Float list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ floatList = this.helper(["Float"]); /** * Create a ID list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ idList = this.helper(["ID"]); /** * Create a Int list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ intList = this.helper(["Int"]); /** * Create a String list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ stringList = this.helper(["String"]); constructor(builder: PothosSchemaTypes.SchemaBuilder<Types>, kind: Kind, typename: string) { this.builder = builder; this.kind = kind; this.typename = typename; } listRef = <T extends InputTypeParam<Types>, Required extends boolean = true>(type: T, options?: { required?: Required; }): InputListRef<Types, InputShapeFromTypeParam<Types, T, Required>[]> => new InputListRef<Types, InputShapeFromTypeParam<Types, T, Required>[]>(type, options?.required ?? true); argBuilder(): ArgBuilder<Types> { const builder = this.field.bind(this as never) as InputFieldBuilder<Types, "Arg">["field"]; const protoKeys = Object.keys(Object.getPrototypeOf(this) as object).filter((key) => typeof (this as Record<string, unknown>)[key] === "function" && (Function.prototype as unknown as Record<string, unknown>)[key] === undefined); ([...Object.keys(this), ...protoKeys] as (keyof InputFieldBuilder<Types, "Arg">)[]).forEach((key) => { (builder as unknown as Record<string, unknown>)[key] = typeof this[key] === "function" ? (this[key] as Function).bind(this) : this[key]; }); return builder as ArgBuilder<Types>; } /** * Create in input field or argument for the current type * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ field<Type extends InputType<Types> | [ InputType<Types> ], Req extends FieldRequiredness<Type>>(options: PothosSchemaTypes.InputFieldOptionsByKind<Types, Type, Req>[Kind]): InputFieldRef<InputShapeFromTypeParam<Types, Type, Req>, Kind> { const ref: InputFieldRef<InputShapeFromTypeParam<Types, Type, Req>, Kind> = new InputFieldRef(this.kind, this.typename); this.builder.configStore.addFieldRef(ref, options.type, {}, (name, parentField, typeConfig) => ({ name, parentField, kind: this.kind, graphqlKind: this.kind, parentType: typeConfig.name, type: inputTypeFromParam<Types>(options.type, this.builder.configStore, options.required ?? this.builder.defaultInputFieldRequiredness), pothosOptions: options as unknown as PothosSchemaTypes.InputFieldOptionsByKind<Types>[Kind], description: options.description, deprecationReason: options.deprecationReason, defaultValue: options.defaultValue, extensions: options.extensions, })); return ref; } private helper<Type extends InputType<Types> | [ InputType<Types> ]>(type: Type) { return <Req extends FieldRequiredness<Type>>(...args: NormalizeArgs<[ options: Omit<PothosSchemaTypes.InputFieldOptionsByKind<Types, Type, Req>[Kind], "type"> ]>) => { const [options = {} as never] = args; return this.field({ ...options, type, } as PothosSchemaTypes.InputFieldOptionsByKind<Types, Type, Req>[Kind]); }; }}
Version Info