import { type ArgumentRef, type FieldKind, type FieldMap, type FieldNullability, type FieldOptionsFromKind, type InferredFieldOptionKeys, type InferredFieldOptionsByKind, type InputFieldMap, type InputFieldsFromShape, type InputShapeFromFields, type InterfaceParam, type ListResolveValue, type MaybePromise, type Merge, type Normalize, type ObjectRef, type OutputShape, type OutputType, type SchemaTypes, type ShapeFromTypeParam, type ShapeWithNullability, type TypeParam, typeBrandKey, } from '@pothos/core'; import type { FieldNode, GraphQLResolveInfo } from 'graphql'; import type { PrismaInterfaceRef, PrismaRef } from './interface-ref'; import type { PrismaObjectFieldBuilder } from './prisma-field-builder'; export interface PrismaDelegate { // biome-ignore lint/suspicious/noExplicitAny: this is fine findUniqueOrThrow?: (...args: any[]) => Promise; // biome-ignore lint/suspicious/noExplicitAny: this is fine findUnique: (...args: any[]) => Promise; } export const prismaModelName = Symbol.for('Pothos.prismaModelName'); export interface PrismaClient { $connect: () => Promise; } export interface PrismaModelTypes { Name: string; Shape: {}; Include: unknown; Select: unknown; OrderBy: unknown; Where: {}; WhereUnique: {}; Create: {}; Update: {}; ListRelations: string; RelationName: string; Relations: Record< string, { Shape: unknown; Name: string; Nullable: boolean; // Types: PrismaModelTypes; } >; } type ExtractModel = ParentShape extends { [prismaModelName]?: infer Name; } ? Types['PrismaTypes'][Name & keyof Types['PrismaTypes']] extends infer Model ? Model extends PrismaModelTypes ? Model : never : never : never; export type PrismaObjectFieldOptions< Types extends SchemaTypes, ParentShape, Type extends TypeParam, Nullable extends FieldNullability, Args extends InputFieldMap, Select, ResolveReturnShape, Shape = unknown extends Select ? ParentShape : ParentShape & ShapeFromSelection< Types, ExtractModel, // biome-ignore lint/suspicious/noExplicitAny: this is fine { select: Select extends (...args: any[]) => infer S ? S : Select } >, > = PothosSchemaTypes.ObjectFieldOptions & InferredFieldOptionsByKind< Types, Types['InferredFieldOptionsKind'], Shape, Type, Nullable, Args, ResolveReturnShape > & { select?: Select & ( | ExtractModel['Select'] | (( args: InputShapeFromFields, ctx: Types['Context'], nestedSelection: ( selection?: Selection, path?: string[], type?: string, ) => Selection, ) => ExtractModel['Select']) ); }; type PrismaObjectFieldsShape< Types extends SchemaTypes, Model extends PrismaModelTypes, Shape extends object, Select, > = Model['Select'] extends Select ? (t: PrismaObjectFieldBuilder) => FieldMap : (t: PrismaSelectionFieldBuilder) => FieldMap; type PrismaSelectionFieldBuilder< Types extends SchemaTypes, Model extends PrismaModelTypes, Shape extends object, > = PrismaObjectFieldBuilder; interface BaseSelection { include?: unknown; select?: unknown; } export type SelectedKeys = { [K in keyof T]: T[K] extends false ? never : K }[keyof T]; export type ShapeFromSelection< Types extends SchemaTypes, Model extends PrismaModelTypes, Selection, > = Normalize< Selection extends BaseSelection ? unknown extends Selection['select'] ? Model['Shape'] & RelationShapeFromInclude : Pick> & RelationShapeFromInclude & ('_count' extends keyof Selection['select'] ? ShapeFromCount : {}) : Model['Shape'] >; export type ShapeFromCount = Selection extends true ? { _count: number } : Selection extends { select: infer Counts } ? { _count: { [K in keyof Counts]: number } } : never; export type TypesForRelation< Types extends SchemaTypes, Model extends PrismaModelTypes, Relation extends keyof Model['Relations'], > = Model['Relations'][Relation]['Name'] extends infer Name ? Name extends keyof Types['PrismaTypes'] ? PrismaModelTypes & Types['PrismaTypes'][Name] : never : never; type RelationShapeFromInclude< Types extends SchemaTypes, Model extends PrismaModelTypes, Include, > = Normalize<{ [K in SelectedKeys as K extends Model['RelationName'] ? K : never]: K extends keyof Model['Relations'] ? Model['Relations'][K]['Shape'] extends unknown[] ? ShapeFromSelection, Include[K]>[] : Model['Relations'][K]['Nullable'] extends true ? ShapeFromSelection, Include[K]> | null : ShapeFromSelection, Include[K]> : unknown; }>; export type PrismaObjectRefOptions< Types extends SchemaTypes, Model extends PrismaModelTypes, Include, Select, Shape extends object, > = NameOrVariant & ( | { include?: Include & Model['Include']; select?: never; findUnique?: ((parent: Shape, context: Types['Context']) => Model['WhereUnique']) | null; } | { select: Model['Select'] & Select; include?: never; findUnique?: (parent: Shape, context: Types['Context']) => Model['WhereUnique']; } ); export type PrismaObjectImplementationOptions< Types extends SchemaTypes, Model extends PrismaModelTypes, Interfaces extends InterfaceParam[], Select, Shape extends object, > = Omit< | PothosSchemaTypes.ObjectTypeOptions | PothosSchemaTypes.ObjectTypeWithInterfaceOptions, 'description' | 'fields' > & { description?: string | false; fields?: PrismaObjectFieldsShape< Types, Model, Shape & { [prismaModelName]?: Model['Name'] }, Select >; }; export type PrismaObjectTypeOptions< Types extends SchemaTypes, Model extends PrismaModelTypes, Interfaces extends InterfaceParam[], Include, Select, Shape extends object, > = PrismaObjectImplementationOptions & PrismaObjectRefOptions; export type PrismaInterfaceRefOptions< Types extends SchemaTypes, Model extends PrismaModelTypes, Include, Select, Shape extends object, > = NameOrVariant & ( | { include?: Include & Model['Include']; select?: never; findUnique?: ((parent: Shape, context: Types['Context']) => Model['WhereUnique']) | null; } | { select: Model['Select'] & Select; include?: never; findUnique?: (parent: Shape, context: Types['Context']) => Model['WhereUnique']; } ); export type PrismaInterfaceImplementationOptions< Types extends SchemaTypes, Model extends PrismaModelTypes, Interfaces extends InterfaceParam[], Select, Shape extends object, > = Omit< PothosSchemaTypes.InterfaceTypeOptions, 'description' | 'fields' > & { description?: string | false; fields?: PrismaObjectFieldsShape< Types, Model, Shape & { [prismaModelName]?: Model['Name'] }, Select >; }; export type PrismaInterfaceTypeOptions< Types extends SchemaTypes, Model extends PrismaModelTypes, Interfaces extends InterfaceParam[], Include, Select, Shape extends object, > = PrismaInterfaceImplementationOptions & PrismaInterfaceRefOptions; type NameOrVariant = | { name?: never; variant?: string; } | { name?: string; variant?: never; }; export type PrismaNodeOptions< Types extends SchemaTypes, Model extends PrismaModelTypes, Interfaces extends InterfaceParam[], Include, Select, Shape extends object, UniqueField, > = NameOrVariant & Omit< | PothosSchemaTypes.ObjectTypeOptions | PothosSchemaTypes.ObjectTypeWithInterfaceOptions, 'fields' | 'isTypeOf' > & (UniqueField extends string ? { findUnique?: (id: string, context: Types['Context']) => Model['WhereUnique']; } : { findUnique: (id: string, context: Types['Context']) => Model['WhereUnique']; }) & { id: Omit< FieldOptionsFromKind< Types, Shape, 'ID', false, {}, 'Object', OutputShape, MaybePromise> >, 'args' | 'nullable' | 'type' | InferredFieldOptionKeys > & ( | { field?: never; resolve: ( parent: Shape, context: Types['Context'], ) => MaybePromise>; } | { resolve?: never; field: UniqueField extends UniqueFieldsFromWhereUnique ? UniqueField : UniqueFieldsFromWhereUnique; } ); fields?: PrismaObjectFieldsShape< Types, Model, Shape & { [prismaModelName]?: Model['Name'] }, Select >; } & { nullable?: boolean; } & ( | { include?: Include & Model['Include']; select?: never; } | { select: Model['Select'] & Select; include?: never; } ); type QueryForField< Types extends SchemaTypes, Args extends InputFieldMap, Include, > = Include extends { where?: unknown } ? | Omit | (( args: InputShapeFromFields, ctx: Types['Context'], ) => Omit) : never; type QueryFromRelation< Model extends PrismaModelTypes, Field extends keyof Model['Include'], > = Model['Include'][Field] extends infer Include ? Include extends { include?: infer I; select?: infer S; } ? { include?: NonNullable; select?: NonNullable; } : never : never; type CursorFromRelation< Model extends PrismaModelTypes, Field extends Model['ListRelations'], > = Field extends keyof Model['Include'] ? Model['Include'][Field] extends infer Include ? Include extends { cursor?: infer T } ? UniqueFieldsFromWhereUnique : never : never : never; type RefForRelation< Types extends SchemaTypes, Model extends PrismaModelTypes, Field extends keyof Model['Relations'], > = Model['Relations'][Field] extends unknown[] ? [ObjectRef] : ObjectRef; export type RelatedFieldOptions< Types extends SchemaTypes, Model extends PrismaModelTypes, Field extends keyof Model['Relations'], Nullable extends boolean, Args extends InputFieldMap, ResolveReturnShape, Shape, > = Omit< PothosSchemaTypes.ObjectFieldOptions< Types, Shape, RefForRelation, Nullable, Args, ResolveReturnShape >, 'description' | 'type' | InferredFieldOptionKeys > & { resolve?: ( query: QueryFromRelation, parent: Shape, args: InputShapeFromFields, context: Types['Context'], info: GraphQLResolveInfo, ) => MaybePromise>; description?: string | false; // biome-ignore lint/suspicious/noExplicitAny: this is fine type?: PrismaRef>; query?: QueryForField; onNull?: | 'error' | (( parent: Shape, args: InputShapeFromFields, context: Types['Context'], info: GraphQLResolveInfo, ) => MaybePromise< Error | ShapeWithNullability >); } & ({ field: boolean extends Nullable ? Types['DefaultFieldNullability'] : Nullable; relation: Model['Relations'][Field]['Nullable']; } extends { field: false; relation: true } ? { onNull: {} } : {}); export type VariantFieldOptions< Types extends SchemaTypes, Model extends PrismaModelTypes, // biome-ignore lint/suspicious/noExplicitAny: this is fine Variant extends PrismaRef, Args extends InputFieldMap, isNull, Shape, > = Omit< PothosSchemaTypes.ObjectFieldOptions< Types, Shape, Variant, unknown extends isNull ? false : true, Args, Model['Shape'] >, InferredFieldOptionKeys | 'type' > & { isNull?: isNull & (( parent: Shape, args: InputShapeFromFields, context: Types['Context'], info: GraphQLResolveInfo, ) => MaybePromise); }; export type RelationCountOptions< Types extends SchemaTypes, Shape, Where, Args extends InputFieldMap, > = Omit< PothosSchemaTypes.ObjectFieldOptions, 'type' | InferredFieldOptionKeys > & { resolve?: ( parent: Shape, args: InputShapeFromFields, context: Types['Context'], info: GraphQLResolveInfo, ) => MaybePromise; where?: Where | ((args: InputShapeFromFields, context: Types['Context']) => Where); }; export type PrismaFieldOptions< Types extends SchemaTypes, ParentShape, Type extends // biome-ignore lint/suspicious/noExplicitAny: this is fine | PrismaRef | keyof Types['PrismaTypes'] | [keyof Types['PrismaTypes']] // biome-ignore lint/suspicious/noExplicitAny: this is fine | [PrismaRef], Model extends PrismaModelTypes, Param extends TypeParam, Args extends InputFieldMap, Nullable extends FieldNullability, ResolveShape, ResolveReturnShape, Kind extends FieldKind = FieldKind, > = FieldOptionsFromKind< Types, ParentShape, Param, Nullable, Args, Kind, ResolveShape, ResolveReturnShape > extends infer FieldOptions ? Omit & { type: Type; resolve: FieldOptions extends { // biome-ignore lint/suspicious/noExplicitAny: this is fine resolve?: (parent: infer Parent, ...args: any[]) => unknown; } ? PrismaFieldResolver : PrismaFieldResolver; } : never; export type PrismaFieldWithInputOptions< Types extends SchemaTypes, ParentShape, Kind extends FieldKind, Args extends InputFieldMap, Fields extends InputFieldMap, Type extends // biome-ignore lint/suspicious/noExplicitAny: this is fine | PrismaRef | keyof Types['PrismaTypes'] | [keyof Types['PrismaTypes']] // biome-ignore lint/suspicious/noExplicitAny: this is fine | [PrismaRef], Model extends PrismaModelTypes, Param extends TypeParam, Nullable extends FieldNullability, InputName extends string, ResolveShape, ResolveReturnShape, ArgRequired extends boolean, > = Omit< PrismaFieldOptions< Types, ParentShape, Type, Model, Param, Args & { [K in InputName]: ArgumentRef< Types, InputShapeFromFields | (true extends ArgRequired ? never : null | undefined) >; }, Nullable, ResolveShape, ResolveReturnShape, Kind >, 'args' > & PothosSchemaTypes.FieldWithInputBaseOptions< Types, Args & { [K in InputName]: ArgumentRef< Types, InputShapeFromFields | (true extends ArgRequired ? never : null | undefined) >; }, Fields, InputName, ArgRequired >; export type PrismaFieldResolver< Types extends SchemaTypes, Model extends PrismaModelTypes, Parent, Param extends TypeParam, Args extends InputFieldMap, Nullable extends FieldNullability, ResolveReturnShape, > = ( query: { include?: Model['Include']; select?: Model['Select']; }, parent: Parent, args: InputShapeFromFields, context: Types['Context'], info: GraphQLResolveInfo, ) => ShapeFromTypeParam extends infer Shape ? [Shape] extends [[readonly (infer Item)[] | null | undefined]] ? ListResolveValue : MaybePromise : never; export type PrismaConnectionFieldOptions< Types extends SchemaTypes, ParentShape, Type extends // biome-ignore lint/suspicious/noExplicitAny: this is fine | PrismaInterfaceRef // biome-ignore lint/suspicious/noExplicitAny: this is fine | PrismaRef | keyof Types['PrismaTypes'], Model extends PrismaModelTypes, Param extends OutputType, Nullable extends boolean, Args extends InputFieldMap, ResolveReturnShape, Kind extends FieldKind, > = Omit< FieldOptionsFromKind< Types, ParentShape, Param, Nullable, InputFieldsFromShape & (InputFieldMap extends Args ? {} : Args), Kind, ParentShape, ResolveReturnShape >, 'args' | InferredFieldOptionKeys | 'type' > & Omit< PothosSchemaTypes.ConnectionFieldOptions< Types, ParentShape, Param, Nullable, false, false, Args, ResolveReturnShape >, InferredFieldOptionKeys | 'type' > & (InputShapeFromFields & PothosSchemaTypes.DefaultConnectionArguments extends infer ConnectionArgs ? { type: Type; cursor: UniqueFieldsFromWhereUnique; defaultSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number); maxSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number); resolve: ( query: { include?: Model['Include']; cursor?: Model['WhereUnique']; take: number; skip: number; }, parent: ParentShape, args: ConnectionArgs, context: Types['Context'], info: GraphQLResolveInfo, ) => ShapeFromTypeParam extends infer Shape ? [Shape] extends [[readonly (infer Item)[] | null | undefined]] ? ListResolveValue : MaybePromise : never; totalCount?: ( parent: ParentShape, args: ConnectionArgs, context: Types['Context'], info: GraphQLResolveInfo, ) => MaybePromise; } : never); export type RelatedConnectionOptions< Types extends SchemaTypes, Model extends PrismaModelTypes, Field extends Model['ListRelations'], Nullable extends boolean, Args extends InputFieldMap, Type = unknown, > = Omit< PothosSchemaTypes.ObjectFieldOptions< Types, Model['Shape'], ObjectRef, Nullable, InputFieldsFromShape & (InputFieldMap extends Args ? {} : Args), unknown >, 'args' | 'description' | InferredFieldOptionKeys | 'type' > & Omit< PothosSchemaTypes.ConnectionFieldOptions< Types, Model['Shape'], ObjectRef, false, false, Nullable, Args, unknown >, InferredFieldOptionKeys | 'type' > & (InputShapeFromFields & PothosSchemaTypes.DefaultConnectionArguments extends infer ConnectionArgs ? { resolve?: ( query: { include?: Model['Include']; cursor?: Model['WhereUnique']; take: number; skip: number; }, parent: Model['Shape'], args: ConnectionArgs, context: Types['Context'], info: GraphQLResolveInfo, ) => MaybePromise< ShapeFromTypeParam< Types, [ObjectRef], Nullable > >; } & { description?: string | false; query?: QueryForField; // biome-ignore lint/suspicious/noExplicitAny: this is fine type?: Type & PrismaRef>; cursor: CursorFromRelation; defaultSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number); maxSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number); totalCount?: boolean; } : never); export type WithBrand = T & { [typeBrandKey]: string }; export type IncludeMap = Record; export interface SelectionMap { select?: Record; include?: Record; where?: object; } export type FieldSelection = | Record | (( args: object, context: object, mergeNestedSelection: ( selection: SelectionMap | boolean | ((args: object, context: object) => SelectionMap), path?: IndirectInclude | string[], type?: string, ) => SelectionMap | boolean, resolveSelection: (path: string[]) => FieldNode | null, ) => SelectionMap); export type LoaderMappings = Record< string, { field: string; type: string; mappings: LoaderMappings; indirectPath: string[]; } >; export interface IndirectInclude { getType: () => string; path?: { type?: string; name: string }[]; paths?: { type?: string; name: string }[][]; } export type ShapeFromConnection = T extends { shape: unknown } ? T['shape'] : never; export type PrismaConnectionShape< Types extends SchemaTypes, T, Parent, Args extends InputFieldMap, > = Merge< ShapeFromConnection> extends infer Shape ? Shape & { parent: Parent; args: InputShapeFromFields & PothosSchemaTypes.DefaultConnectionArguments; } : never > extends infer C ? [C] extends [ { edges: infer Edges; }, ] ? Merge< Omit & { edges: Edges extends Iterable | null | undefined> ? Merge[] : never; } > : C : never; export type UniqueFieldsExtendedWhereUnique = NonNullable< T extends infer O ? { [K in keyof O]: undefined extends O[K] ? never : K; }[keyof O] : never >; export type UniqueFieldsFromWhereUnique = string & (UniqueFieldsExtendedWhereUnique extends infer K ? [K] extends [never] ? keyof T : K : never); type Simplify = { [K in keyof T]: T[K]; } & {}; // Infer the type of a specific field from a given type type InferField = T extends { [K in N]?: infer U | null } ? U : never; // Check if a type is nullable type IsNullable = null extends T ? true : undefined extends T ? true : false; // Capitalize the first letter of a string type CapitalizeFirst = S extends `${infer F}${infer R}` ? `${Uppercase}${R}` : S; type InferModelShape = Simplify< InferField & InferComposites> >; // Infer relations for a given model type InferRelations = { [K in keyof Model]: { // biome-ignore lint/suspicious/noExplicitAny: matching against any is okay Shape: Model[K] extends any[] ? InferModelShape[] : InferModelShape; Name: InferField, 'name'>; Nullable: IsNullable; }; }; // Create a list of keys that represent array relations type InferRelationList = { // biome-ignore lint/suspicious/noExplicitAny: matching against any is okay [K in keyof Model as Model[K] extends any[] ? K : never]: K; }; // Make a type nullable if IsNullable is true type MakeNullable = IsNullable extends true ? T | null : T; // Infer the item of an array type InferItemOfArray = T extends Array ? U : T; // Infers the composites of a given model type InferComposites = { // biome-ignore lint/suspicious/noExplicitAny: matching against any is okay [K in keyof Model]: Model[K] extends any[] ? Simplify< InferField & InferComposites> >[] : 'composites' extends keyof Exclude ? MakeNullable< Simplify< InferField & InferComposites> >, IsNullable > : InferField; }; type ExtractModelKeys = { [K in keyof T]: K extends `$${string}` ? never : K; }[keyof T]; type PrismaArgs = T extends { [K: symbol]: { types: { operations: { [K in F]: { args: unknown; }; }; }; }; } ? T[symbol]['types']['operations'][F]['args'] : unknown; type PrismaPayload = T extends { [K: symbol]: { types: { payload: unknown; }; }; } ? T[symbol]['types']['payload'] : unknown; /** * Infer PrismaTypes from a PrismaClient * @example * `type PrismaTypes = PrismaTypesFromClient` **/ export type PrismaTypesFromClient = { [K in ExtractModelKeys as CapitalizeFirst]: PrismaModelTypes & { Name: CapitalizeFirst; Shape: InferModelShape>; Include: InferField, 'include'>; Select: InferField, 'select'>; OrderBy: InferItemOfArray, 'orderBy'>>; WhereUnique: InferField, 'where'>; Where: InferField, 'where'>; ListRelations: keyof InferRelationList, 'objects'>>; RelationName: keyof InferField, 'objects'>; Relations: InferRelations, 'objects'>>; Create: PrismaUtils extends true ? InferField, 'data'> : {}; Update: PrismaUtils extends true ? InferField, 'data'> : {}; }; }; interface DMMFField { type: string; kind: string; name: string; isRequired: boolean; isList: boolean; hasDefaultValue: boolean; isUnique: boolean; isId: boolean; documentation?: string; relationName?: string; relationFromFields?: readonly string[]; isUpdatedAt?: boolean; } export interface PothosPrismaDatamodel { datamodel: { models: Record< string, { fields: readonly DMMFField[]; primaryKey: { name: string | null; fields: readonly string[] } | null; uniqueIndexes: { name: string | null; fields: readonly string[] }[]; documentation?: string; } >; }; }