import { AllInputTypes, GetGen2 } from '../typegenTypeHelpers' import { AllNexusArgsDefs, AllNexusInputTypeDefs } from './wrapping' import { NexusTypes, withNexusSymbol } from './_types' export type ArgsRecord = Record export type CommonArgConfig = { /** * The description to annotate the GraphQL SDL */ description?: string | null } & NexusGenPluginArgConfig export interface ScalarArgConfig extends CommonArgConfig { /** * Configure the default for the object */ default?: T } export type NexusArgConfigType = T | AllNexusInputTypeDefs export interface NexusAsArgConfig extends CommonArgConfig { /** * Configure the default for the object */ default?: GetGen2<'allTypes', T> // TODO: Make this type-safe somehow } export interface NexusArgConfig extends NexusAsArgConfig { /** * The type of the argument, either the string name of the type, * or the concrete Nexus type definition */ type: NexusArgConfigType } export interface NexusFinalArgConfig extends NexusArgConfig { configFor: 'arg' argName: string fieldName: string parentType: string } export class NexusArgDef { constructor(readonly name: string, protected config: NexusArgConfig) {} get value() { return this.config } } withNexusSymbol(NexusArgDef, NexusTypes.Arg) /** * Defines an argument that can be used in any object or interface type * * Takes the GraphQL type name and any options. * * The value returned from this argument can be used multiple times in any valid `args` object value * * @see https://graphql.github.io/learn/schema/#arguments */ export function arg(options: NexusArgConfig) { if (!options.type) { throw new Error('You must provide a "type" for the arg()') } return new NexusArgDef( typeof options.type === 'string' ? options.type : (options.type as any).name, options ) } export function stringArg(options?: ScalarArgConfig) { return arg({ type: 'String', ...options }) } export function intArg(options?: ScalarArgConfig) { return arg({ type: 'Int', ...options }) } export function floatArg(options?: ScalarArgConfig) { return arg({ type: 'Float', ...options }) } export function idArg(options?: ScalarArgConfig) { return arg({ type: 'ID', ...options }) } export function booleanArg(options?: ScalarArgConfig) { return arg({ type: 'Boolean', ...options }) }