import { InputDefinitionBlock, OutputDefinitionBlock, } from "./definitions/definitionBlocks"; import { withNexusSymbol, NexusTypes } from "./definitions/_types"; import { PluginBuilderLens } from "./builder"; export type OutputFactoryConfig = { stage: "walk" | "build"; args: any[]; builder: PluginBuilderLens; typeDef: OutputDefinitionBlock; /** * The name of the type this field is being declared on */ typeName: string; }; export type InputFactoryConfig = { args: any[]; builder: PluginBuilderLens; typeDef: InputDefinitionBlock; /** * The name of the type this field is being declared on */ typeName: string; }; 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; } 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); }