import { PluginBuilderLens } from './builder' import { InputDefinitionBlock, OutputDefinitionBlock } from './definitions/definitionBlocks' import { NexusTypes, withNexusSymbol } from './definitions/_types' import { NexusWrapKind } from './definitions/wrapping' export type OutputFactoryConfig = { /** * The name of the type this field is being declared on */ typeName: string stage: 'walk' | 'build' args: any[] builder: PluginBuilderLens typeDef: OutputDefinitionBlock /** * The list of chained wrapping calls leading up to this dynamic method */ wrapping?: NexusWrapKind[] } export type InputFactoryConfig = { args: any[] builder: PluginBuilderLens typeDef: InputDefinitionBlock /** * The name of the type this field is being declared on */ typeName: string /** * The list of chained wrapping calls leading up to this dynamic method */ wrapping?: NexusWrapKind[] } export interface BaseExtensionConfig { /** * The name of the "extension", the field made * available on the builders */ name: T /** * The full type definition for the options, including generic * signature for the type */ typeDefinition?: string /** * Description inserted above the typeDefinition for the field, will be formatted * as JSDOC by Nexus */ typeDescription?: string } export interface DynamicOutputMethodConfig extends BaseExtensionConfig { /** * Invoked when the field is called */ factory(config: OutputFactoryConfig): any } export interface DynamicInputMethodConfig extends BaseExtensionConfig { /** * Invoked when the field is called */ factory(config: InputFactoryConfig): any } export class DynamicInputMethodDef { constructor(readonly name: Name, protected config: DynamicInputMethodConfig) {} get value() { return this.config } } withNexusSymbol(DynamicInputMethodDef, NexusTypes.DynamicInput) export class DynamicOutputMethodDef { constructor(readonly name: Name, protected config: DynamicOutputMethodConfig) {} get value() { return this.config } } withNexusSymbol(DynamicOutputMethodDef, NexusTypes.DynamicOutputMethod) /** * Defines a new property on the object definition block * for an output type, taking arbitrary input to define * additional types. See the connectionPlugin: * * t.connectionField('posts', { * nullable: true, * totalCount(root, args, ctx, info) { * return ctx.user.getTotalPostCount(root.id, args) * }, * nodes(root, args, ctx, info) { * return ctx.user.getPosts(root.id, args) * } * }) */ export function dynamicOutputMethod(config: DynamicOutputMethodConfig) { return new DynamicOutputMethodDef(config.name, config) } /** * Same as the outputFieldExtension, but for fields that * should be added on as input types. */ export function dynamicInputMethod(config: DynamicInputMethodConfig) { return new DynamicInputMethodDef(config.name, config) }