import './global-types.js'; import SchemaBuilder, { brandWithType, type InterfaceRef, isThenable, type OutputType, type SchemaTypes, } from '@pothos/core'; import type { GraphQLResolveInfo } from 'graphql'; import { PrismaInterfaceRef } from './interface-ref.js'; import { ModelLoader, typeParentLoader } from './model-loader.js'; import { PrismaNodeRef } from './node-ref.js'; import { PrismaObjectRef } from './object-ref.js'; import { PrismaObjectFieldBuilder } from './prisma-field-builder.js'; import type { PrismaDelegate, PrismaModelTypes, PrismaNodeOptions } from './types.js'; import { getDefaultIDParser, getDefaultIDSerializer } from './util/cursors.js'; import { getDelegateFromModel, getModel, getRefFromModel } from './util/datamodel.js'; import { getModelDescription } from './util/description.js'; import { getClient, getDMMF } from './util/get-client.js'; import { queryFromInfo } from './util/map-query.js'; import { getRelationMap } from './util/relation-map.js'; const schemaBuilderProto = SchemaBuilder.prototype as PothosSchemaTypes.SchemaBuilder; /** * The body of `prismaObject` and `prismaInterface`. The two build the same ref, extensions and * field builder; only the ref class and which of `objectType`/`interfaceType` registers it differ. */ function definePrismaType( builder: PothosSchemaTypes.SchemaBuilder, type: string, { fields, findUnique, select, include, description, ...options }: { fields?: (t: PrismaObjectFieldBuilder) => {}; findUnique?: ((model: Record, ctx: {}) => unknown) | null; select?: {}; include?: {}; description?: string | false; variant?: string; name?: string; extensions?: {}; }, kind: 'Interface' | 'Object', ) { const isInterface = kind === 'Interface'; const ref = options.variant ? isInterface ? new PrismaInterfaceRef(options.variant, type) : new PrismaObjectRef(options.variant, type) : (getRefFromModel(type, builder, isInterface ? 'interface' : 'object') as PrismaObjectRef< SchemaTypes, PrismaModelTypes >); const name = options.variant ?? options.name ?? type; const fieldMap = getRelationMap(getDMMF(builder)).get(type)!; const idSelection = ModelLoader.getDefaultIDSelection(ref, type, builder); const fullSelect = select && { ...idSelection, ...select }; const loader = ModelLoader.forRef(ref, type, findUnique, builder); ref.name = name; const config = { ...options, description: getModelDescription(type, builder, description), extensions: { ...options.extensions, pothosPrismaInclude: include, pothosPrismaModel: type, pothosPrismaFieldMap: fieldMap, pothosPrismaSelect: fullSelect, pothosPrismaLoader: loader, // Read by scope-auth for the row its checks see. A type without a `findUnique` is never // reloaded. ...(!isInterface && findUnique !== null ? { pothosScopeAuthParent: typeParentLoader( loader, fieldMap, { select: fullSelect, include }, select ? [] : getModel(type, builder) .fields.filter((field) => field.kind === 'scalar' || field.kind === 'enum') .map((field) => field.name), ), } : {}), }, name, fields: fields ? () => fields(new PrismaObjectFieldBuilder(name, builder, type, fieldMap, kind)) : undefined, }; if (isInterface) { builder.interfaceType(ref as never, config as never); } else { builder.objectType(ref as never, config as never); } return ref; } schemaBuilderProto.prismaObject = function prismaObject(type, options) { return definePrismaType(this, type, options as never, 'Object') as never; }; schemaBuilderProto.prismaInterface = function prismaInterface(type, options) { return definePrismaType(this, type, options as never, 'Interface') as never; }; schemaBuilderProto.prismaNode = function prismaNode( this: PothosSchemaTypes.SchemaBuilder & { nodeInterfaceRef?: () => InterfaceRef; }, type: keyof SchemaTypes['PrismaTypes'], { id: { field, resolve: rawResolve, ...idOptions }, findUnique: rawFindUnique, name, variant, nullable, ...options }: PrismaNodeOptions, ) { const fieldName = field as unknown as string; const resolve = rawResolve ?? getDefaultIDSerializer(type, fieldName, this); const idParser = fieldName ? getDefaultIDParser(type, fieldName, this) : undefined; const typeName = variant ?? name ?? type; const nodeRef = new PrismaNodeRef(typeName, type); const findUnique = rawFindUnique ? (parent: unknown, context: {}) => { const id = resolve(parent as never, context); return isThenable(id) ? id.then((settled) => rawFindUnique(settled as string, context)) : rawFindUnique(id as string, context); } : ModelLoader.getFindUniqueForField(nodeRef, type, fieldName, this); const extendedOptions = { ...options, // The global ID and `findUnique` read `id.field`, which a type-level `select` only covers // when it is the primary key. select: options.select && fieldName ? { ...ModelLoader.getCursorSelection(nodeRef, type, fieldName, this), ...options.select, } : options.select, name, variant, findUnique, }; const ref = this.prismaObject(type, extendedOptions as never); const loadWhere = (delegate: PrismaDelegate, query: object, where: {}) => (delegate.findUniqueOrThrow && !nullable ? delegate.findUniqueOrThrow({ ...query, where } as never) : delegate.findUnique({ ...query, ...(nullable ? {} : { rejectOnNotFound: true }), where, } as never) ).then((record: unknown) => { brandWithType(record, typeName as OutputType); return record; }); const loadNode = (query: object, id: string, context: SchemaTypes['Context']) => { const delegate = getDelegateFromModel(getClient(this, context), type); const where = rawFindUnique ? rawFindUnique(id, context) : { [fieldName]: idParser!(id) }; return isThenable(where) ? where.then((settled) => loadWhere(delegate, query, settled as {})) : loadWhere(delegate, query, where as {}); }; (this as typeof this & { nodeRef: (ref: unknown, options: unknown) => unknown }).nodeRef(ref, { id: { ...idOptions, resolve: (parent: never, _args: object, context: object) => resolve(parent, context), }, loadWithoutCache: (id: string, context: SchemaTypes['Context'], info: GraphQLResolveInfo) => { // A promise while a select beneath the node is async; the query waits only then. const query = queryFromInfo({ context, info, typeName, skipDeferredFragments: this.options.prisma?.skipDeferredFragments, awaitSelections: true, }); return isThenable(query) ? query.then((settled) => loadNode(settled as object, id, context)) : loadNode(query, id, context); }, }); this.configStore.associateParamWithRef(nodeRef, ref); return nodeRef; } as never; /** The body of `prismaObjectField(s)` and `prismaInterfaceField(s)`: they differ only in kind. */ function addPrismaFields( builder: PothosSchemaTypes.SchemaBuilder, type: string | PrismaObjectRef, fields: (t: PrismaObjectFieldBuilder) => {}, graphqlKind?: 'Interface', ) { // A model name names the interface registered under it, as `prismaInterface` registers one. const ref = typeof type === 'string' ? getRefFromModel(type, builder, graphqlKind ? 'interface' : 'object') : type; builder.configStore.onTypeConfig(ref, ({ name }) => { builder.configStore.addFields(ref, () => fields( new PrismaObjectFieldBuilder( name, builder, ref.modelName, getRelationMap(getDMMF(builder)).get(ref.modelName)!, graphqlKind, ), ), ); }); } schemaBuilderProto.prismaObjectField = function prismaObjectField(type, fieldName, field) { addPrismaFields(this, type as never, (t) => ({ [fieldName]: field(t as never) })); }; schemaBuilderProto.prismaInterfaceField = function prismaInterfaceField(type, fieldName, field) { addPrismaFields(this, type as never, (t) => ({ [fieldName]: field(t as never) }), 'Interface'); }; schemaBuilderProto.prismaObjectFields = function prismaObjectFields(type, fields) { addPrismaFields(this, type as never, fields as never); }; schemaBuilderProto.prismaInterfaceFields = function prismaInterfaceFields(type, fields) { addPrismaFields(this, type as never, fields as never, 'Interface'); };