import type { FieldKind, FieldMap, FieldNullability, GenericFieldRef, InputFieldMap, InterfaceParam, MaybeAsyncSelection, NormalizeArgs, OutputType, PluginName, SchemaTypes, ShapeFromTypeParam, TypeParam, } from '@pothos/core'; import type { PathSegment } from '@pothos/selection-mapper'; import type { BuildQueryResult, DBQueryConfig, TableRelationalConfig, TablesRelationalConfig, } from 'drizzle-orm'; import type { GraphQLResolveInfo } from 'graphql'; import type { DrizzleObjectFieldBuilder } from './drizzle-field-builder.js'; import type { PothosDrizzlePlugin } from './index.js'; import type { DrizzleInterfaceRef, DrizzleRef } from './interface-ref.js'; import type { DrizzleNodeRef } from './node-ref.js'; import type { DrizzleObjectRef, drizzleTableKey } from './object-ref.js'; import type { DrizzleConnectionFieldOptions, DrizzleConnectionShape, DrizzleFieldOptions, DrizzleFieldWithInputOptions, DrizzleInterfaceOptions, DrizzleNodeOptions, DrizzleObjectFieldOptions, DrizzleObjectOptions, DrizzlePluginOptions, DrizzleTableNameBrand, ShapeFromConnection, ShapeFromIdColumns, } from './types.js'; import type { DrizzleQueryFromInfoResult } from './utils/map-query.js'; import type { DrizzleRelationsHint } from './utils/refs.js'; // True when Type is a union of names and TableConfig is exactly its default. A single name gets // the same shape either way, so it skips the check. type IsDefaultConfigOfNames = IsUnion extends true ? (() => T extends TableConfig ? 1 : 2) extends () => T extends Default ? 1 : 2 ? true : false : false; type IsUnion = T extends unknown ? ([All] extends [T] ? false : true) : never; declare global { export namespace PothosSchemaTypes { export interface Plugins { drizzle: PothosDrizzlePlugin; } export interface SchemaBuilderOptions { drizzle: DrizzlePluginOptions; } export interface UserSchemaTypes { DrizzleRelations: TablesRelationalConfig; } export interface ExtendDefaultTypes> { DrizzleRelations: PartialTypes['DrizzleRelations'] & {}; } export interface SchemaBuilder { drizzleObject: < const Interfaces extends InterfaceParam[], Table extends keyof Types['DrizzleRelations'], Selection extends | DBQueryConfig<'one', Types['DrizzleRelations'], Types['DrizzleRelations'][Table]> | true, Shape = BuildQueryResult< Types['DrizzleRelations'], Types['DrizzleRelations'][Table], true extends Selection ? true : Selection & { columns: {} } >, >( table: Table | DrizzleRelationsHint, options: DrizzleObjectOptions, ) => DrizzleObjectRef; drizzleInterface: < const Interfaces extends InterfaceParam[], Table extends keyof Types['DrizzleRelations'], Selection extends | DBQueryConfig<'one', Types['DrizzleRelations'], Types['DrizzleRelations'][Table]> | true, Shape = BuildQueryResult< Types['DrizzleRelations'], Types['DrizzleRelations'][Table], true extends Selection ? true : Selection & { columns: {} } >, >( table: Table | DrizzleRelationsHint, options: DrizzleInterfaceOptions, ) => DrizzleInterfaceRef; drizzleNode: 'relay' extends PluginName ? < const Interfaces extends InterfaceParam[], Table extends keyof Types['DrizzleRelations'], Selection extends | DBQueryConfig<'one', Types['DrizzleRelations'], Types['DrizzleRelations'][Table]> | true, IDColumns, Shape = BuildQueryResult< Types['DrizzleRelations'], Types['DrizzleRelations'][Table], true extends Selection ? true : Selection & { columns: {} } >, >( table: Table | DrizzleRelationsHint, options: DrizzleNodeOptions, ) => DrizzleNodeRef> : '@pothos/plugin-relay is required to use this method'; drizzleObjectField: < Type extends DrizzleObjectRef | keyof Types['DrizzleRelations'], TableConfig extends TableRelationalConfig = Type extends DrizzleObjectRef ? Type[typeof drizzleTableKey] : Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']], // When TableConfig is the default, use each name's own table. Pairing every name with // every table made table name completion very slow in large schemas. Shape extends {} = IsDefaultConfigOfNames< Type, TableConfig, Type extends DrizzleObjectRef ? Type[typeof drizzleTableKey] : Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']] > extends true ? Type extends DrizzleObjectRef ? S & DrizzleTableNameBrand : BuildQueryResult< Types['DrizzleRelations'], Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']], true > & DrizzleTableNameBrand : Type extends DrizzleObjectRef ? S & DrizzleTableNameBrand : BuildQueryResult & DrizzleTableNameBrand, >( type: Type | DrizzleRelationsHint, fieldName: string, field: ( t: DrizzleObjectFieldBuilder, ) => GenericFieldRef, ) => void; drizzleInterfaceField: < Type extends DrizzleInterfaceRef | keyof Types['DrizzleRelations'], TableConfig extends TableRelationalConfig = Type extends DrizzleInterfaceRef ? Type[typeof drizzleTableKey] : Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']], // When TableConfig is the default, use each name's own table. Pairing every name with // every table made table name completion very slow in large schemas. Shape extends {} = IsDefaultConfigOfNames< Type, TableConfig, Type extends DrizzleInterfaceRef ? Type[typeof drizzleTableKey] : Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']] > extends true ? Type extends DrizzleInterfaceRef ? S & DrizzleTableNameBrand : BuildQueryResult< Types['DrizzleRelations'], Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']], true > & DrizzleTableNameBrand : Type extends DrizzleInterfaceRef ? S & DrizzleTableNameBrand : BuildQueryResult & DrizzleTableNameBrand, >( type: Type | DrizzleRelationsHint, fieldName: string, field: ( t: DrizzleObjectFieldBuilder, ) => GenericFieldRef, ) => void; drizzleObjectFields: < Type extends DrizzleObjectRef | keyof Types['DrizzleRelations'], TableConfig extends TableRelationalConfig = Type extends DrizzleObjectRef ? Type[typeof drizzleTableKey] : Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']], // When TableConfig is the default, use each name's own table. Pairing every name with // every table made table name completion very slow in large schemas. Shape extends {} = IsDefaultConfigOfNames< Type, TableConfig, Type extends DrizzleObjectRef ? Type[typeof drizzleTableKey] : Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']] > extends true ? Type extends DrizzleObjectRef ? S & DrizzleTableNameBrand : BuildQueryResult< Types['DrizzleRelations'], Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']], true > & DrizzleTableNameBrand : Type extends DrizzleObjectRef ? S & DrizzleTableNameBrand : BuildQueryResult & DrizzleTableNameBrand, >( type: Type | DrizzleRelationsHint, fields: (t: DrizzleObjectFieldBuilder) => FieldMap, ) => void; drizzleInterfaceFields: < Type extends DrizzleInterfaceRef | keyof Types['DrizzleRelations'], TableConfig extends TableRelationalConfig = Type extends DrizzleInterfaceRef ? Type[typeof drizzleTableKey] : Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']], // When TableConfig is the default, use each name's own table. Pairing every name with // every table made table name completion very slow in large schemas. Shape extends {} = IsDefaultConfigOfNames< Type, TableConfig, Type extends DrizzleInterfaceRef ? Type[typeof drizzleTableKey] : Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']] > extends true ? Type extends DrizzleInterfaceRef ? S & DrizzleTableNameBrand : BuildQueryResult< Types['DrizzleRelations'], Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']], true > & DrizzleTableNameBrand : Type extends DrizzleInterfaceRef ? S & DrizzleTableNameBrand : BuildQueryResult & DrizzleTableNameBrand, >( type: Type | DrizzleRelationsHint, fields: (t: DrizzleObjectFieldBuilder) => FieldMap, ) => void; } export interface PothosKindToGraphQLType { DrizzleObject: 'Object'; } export interface FieldOptionsByKind< Types extends SchemaTypes, ParentShape, Type extends TypeParam, Nullable extends FieldNullability, Args extends InputFieldMap, ResolveShape, ResolveReturnShape, > { DrizzleObject: DrizzleObjectFieldOptions< Types, ParentShape, Type, Nullable, Args, ResolveShape, ResolveReturnShape >; } export interface RootFieldBuilder< Types extends SchemaTypes, ParentShape, Kind extends FieldKind = FieldKind, > { drizzleQueryFromInfo: < Table extends keyof Types['DrizzleRelations'], const Selection extends DBQueryConfig< 'many', Types['DrizzleRelations'], Types['DrizzleRelations'][Table] > = {}, >( // biome-ignore lint/suspicious/noExplicitAny: a ref from before `withAuth` replaced the context type: Table | DrizzleRef | DrizzleRelationsHint, // The concrete query intersection contextually types callbacks while Selection retains literals. options: Selection & DBQueryConfig<'many', Types['DrizzleRelations'], Types['DrizzleRelations'][Table]> & Record< Exclude< keyof Selection, keyof DBQueryConfig<'many'> | 'context' | 'info' | 'path' | 'paths' >, never > & { context: Types['Context']; info: GraphQLResolveInfo; path?: PathSegment[]; paths?: PathSegment[][]; }, ) => MaybeAsyncSelection< Types, DrizzleQueryFromInfoResult< // With no query keys in the options, Selection falls back to the whole config. (() => T extends Selection ? 1 : 2) extends () => T extends DBQueryConfig< 'many', Types['DrizzleRelations'], Types['DrizzleRelations'][Table] > ? 1 : 2 ? {} : Selection, Table > >; drizzleField: < Args extends InputFieldMap, Param extends | keyof Types['DrizzleRelations'] | [keyof Types['DrizzleRelations']] // biome-ignore lint/suspicious/noExplicitAny: this is fine | DrizzleRef // biome-ignore lint/suspicious/noExplicitAny: this is fine | [DrizzleRef], Nullable extends FieldNullability, ResolveShape, ResolveReturnShape, Table extends keyof Types['DrizzleRelations'] = Param extends keyof Types['DrizzleRelations'] ? Param : Param extends [keyof Types['DrizzleRelations']] ? Param[0] : // biome-ignore lint/suspicious/noExplicitAny: this is fine Param extends DrizzleRef ? T & keyof Types['DrizzleRelations'] : // biome-ignore lint/suspicious/noExplicitAny: this is fine Param extends [DrizzleRef] ? T & keyof Types['DrizzleRelations'] : never, // biome-ignore lint/suspicious/noExplicitAny: this is fine Type extends TypeParam = Param extends DrizzleRef | [DrizzleRef] ? Param : Param extends [unknown] ? [ ObjectRef< Types, | BuildQueryResult< Types['DrizzleRelations'], Types['DrizzleRelations'][Table], true > | { $pothosQueryFor: Table | undefined; } >, ] : ObjectRef< Types, | BuildQueryResult< Types['DrizzleRelations'], Types['DrizzleRelations'][Table], true > | { $pothosQueryFor: Table | undefined; } >, >( options: DrizzleFieldOptions< Types, ParentShape, Table, Type, Nullable, Args, Kind, ResolveShape, ResolveReturnShape, Param >, ) => FieldRef>; drizzleConnection: 'relay' extends PluginName ? < Type extends // biome-ignore lint/suspicious/noExplicitAny: this is fine DrizzleRef | keyof Types['DrizzleRelations'], Nullable extends boolean, ResolveReturnShape, Args extends InputFieldMap = {}, // biome-ignore lint/suspicious/noExplicitAny: this is fine Shape = Type extends DrizzleRef ? Type['$inferType'] : | BuildQueryResult< Types['DrizzleRelations'], Types['DrizzleRelations'][Type & keyof Types['DrizzleRelations']], true > | { $pothosQueryFor: Type | undefined; }, const ConnectionInterfaces extends InterfaceParam[] = [], const EdgeInterfaces extends InterfaceParam[] = [], >( options: DrizzleConnectionFieldOptions< Types, ParentShape, Type, // biome-ignore lint/suspicious/noExplicitAny: this is fine Types['DrizzleRelations'][Type extends DrizzleRef ? K : Type & keyof Types['DrizzleRelations']], ObjectRef, Nullable, Args, ResolveReturnShape, Kind >, ...args: NormalizeArgs< [ connectionOptions: | ConnectionObjectOptions< Types, ObjectRef, false, false, DrizzleConnectionShape, ConnectionInterfaces > | ObjectRef< Types, ShapeFromConnection> >, edgeOptions: | ConnectionEdgeObjectOptions< Types, ObjectRef, false, DrizzleConnectionShape, EdgeInterfaces > | ObjectRef< Types, { cursor: string; node?: Shape | null | undefined; } >, ], 0 > ) => FieldRef>> : '@pothos/plugin-relay is required to use this method'; drizzleFieldWithInput: 'withInput' extends PluginName ? < Param extends | keyof Types['DrizzleRelations'] | [keyof Types['DrizzleRelations']] // biome-ignore lint/suspicious/noExplicitAny: this is fine | DrizzleRef // biome-ignore lint/suspicious/noExplicitAny: this is fine | [DrizzleRef], Nullable extends FieldNullability, ResolveShape, ResolveReturnShape, ArgRequired extends boolean, Fields extends InputFieldMap = {}, Args extends InputFieldMap = {}, InputName extends string = 'input', Table extends keyof Types['DrizzleRelations'] = Param extends keyof Types['DrizzleRelations'] ? Param : Param extends [keyof Types['DrizzleRelations']] ? Param[0] : // biome-ignore lint/suspicious/noExplicitAny: this is fine Param extends DrizzleRef ? T & keyof Types['DrizzleRelations'] : // biome-ignore lint/suspicious/noExplicitAny: this is fine Param extends [DrizzleRef] ? T & keyof Types['DrizzleRelations'] : never, Type extends TypeParam = ObjectRef< Types, | BuildQueryResult | { $pothosQueryFor: Table | undefined; } >, >( options: DrizzleFieldWithInputOptions< Types, ParentShape, Table, Type, Nullable, Args, Kind, ResolveShape, ResolveReturnShape, Param, InputName, Fields, boolean extends ArgRequired ? (Types & { WithInputArgRequired: boolean })['WithInputArgRequired'] : ArgRequired >, ) => FieldRef> : '@pothos/plugin-with-input is required to use this method'; } export interface ConnectionFieldOptions< Types extends SchemaTypes, ParentShape, Type extends OutputType, Nullable extends boolean, EdgeNullability extends FieldNullability<[unknown]>, NodeNullability extends boolean, Args extends InputFieldMap, ResolveReturnShape, > {} export interface ConnectionObjectOptions< Types extends SchemaTypes, Type extends OutputType, EdgeNullability extends FieldNullability<[unknown]>, NodeNullability extends boolean, Resolved, Interfaces extends InterfaceParam[] = [], > {} export interface ConnectionEdgeObjectOptions< Types extends SchemaTypes, Type extends OutputType, NodeNullability extends boolean, Resolved, Interfaces extends InterfaceParam[] = [], > {} export interface DefaultConnectionArguments { first?: number | null | undefined; last?: number | null | undefined; before?: string | null | undefined; after?: string | null | undefined; } export interface ConnectionShapeHelper {} export interface ScopeAuthFieldAuthScopes< Types extends SchemaTypes, Parent, Args extends {} = {}, > {} export interface ScopeAuthContextForAuth {} export interface FieldWithInputBaseOptions< Types extends SchemaTypes, Args extends InputFieldMap, Fields extends InputFieldMap, InputName extends string, ArgRequired extends boolean, > {} } }