Setup
Setting up the Prisma ORM 8 plugin
Install
Set up Prisma ORM 8 and your contract with the Prisma ORM documentation, then install the plugin with its Prisma peers:
npm install --save @pothos/core @pothos/plugin-prisma-next graphql @prisma/orm-family-sql@8.0.0-rc.23 @prisma/orm-framework@8.0.0-rc.23The plugin targets Prisma ORM 8.0.0-rc.23 exactly. Install every @prisma/orm-* package,
including the one for your database (such as @prisma/orm-postgres), at that version. Two copies
of the same package won't work together. The database packages need temporal-polyfill. npm and
pnpm install it automatically, but with Yarn you have to add it yourself.
Requires Node.js 22.20+ or 24.7+. This plugin and
@pothos/plugin-prisma can't be used in the same program.
If you use @pothos/plugin-scope-auth, it needs to be 4.3.0 or newer.
Configure the builder
Pass your Prisma client to the plugin, and your contract's type as PrismaContract:
import SchemaBuilder from '@pothos/core';
import PrismaPlugin from '@pothos/plugin-prisma-next';
import { contract, db } from './prisma/db';
const builder = new SchemaBuilder<{
PrismaContract: typeof contract;
}>({
plugins: [PrismaPlugin],
prisma: { client: db },
});
const User = builder.prismaObject('User', {
fields: (t) => ({
id: t.exposeID('id'),
email: t.exposeString('email'),
}),
});
const Post = builder.prismaObject('Post', {
fields: (t) => ({
id: t.exposeID('id'),
title: t.exposeString('title'),
author: t.relation('author'),
}),
});
builder.queryType({
fields: (t) => ({
recentPosts: t.prismaField({
type: ['Post'],
query: { where: { published: 1 }, orderBy: { createdAt: 'desc' }, limit: 10 },
}),
}),
});
export const schema = builder.toSchema();Later examples pass the User and Post refs as a field's type. A model name such as 'Post'
works too. Later pages add more fields to these types, which you can also do with
prismaObjectFields.
For nodes and connections, also install and add @pothos/plugin-relay.
This example uses PostgreSQL:
- The client groups models by schema (
db.orm.public.Post). The plugin chooses that Collection automatically for the field above. - SQLite and MySQL work the same way with their own Prisma packages. Their clients don't group
models, and the rest of these docs use that shorter form,
db.orm.Post.
Pass a context object, such as contextValue: {}, when you execute an operation. The plugin keeps
per-request state on it.
Examples using authentication assume context fields userId: string and isAdmin: boolean. Add
Context: YourContext alongside PrismaContract in the builder's type parameter, and supply those
values for each request. See Context for configuring and typing it.
Models in several namespaces
If your contract has more than one namespace, refer to models by their full name, such as
'auth.User'. GraphQL type names leave the namespace out, so when two namespaces both have a
User, give one of them a name:
builder.prismaObject('auth.User', {
name: 'AuthUser',
fields: (t) => ({
id: t.exposeID('id'),
}),
});Plugin options
These go under prisma in the builder options.
client: your Prisma client. Required. For a request-scoped client, pass(ctx) => clientand setcontextto the base client'scontext.skipDeferredFragments: load@deferfragments when they execute instead of in the initial query. Defaults totrue.defaultConnectionSize/maxConnectionSize: default and maximum page sizes. Default to 20 and 100.filterConnectionTotalCount: whentrue(the default), arelatedConnection'stotalCountonly counts rows matching itsquery.where.
Transactions
For multi-step mutations and request-scoped clients, see the Transactions guide.
TypeScript and Temporal
If you use Temporal columns and declaration emit fails with TS2742 or TS2883 mentioning
temporal-spec, add it as a direct dependency:
npm install --save temporal-specIf TypeScript reports missing Temporal globals, import their declarations once in your project
(for example in a globals.d.ts file):
import type {} from 'temporal-spec/global';This only adds types. You still need Temporal at runtime, natively or from a polyfill.
Limitations
- On MySQL, Prisma reads rows but can't write them, count them or run aggregates yet. MongoDB isn't supported.
- Clients see
Invalid ID.orInvalid cursor.for a malformed ID or cursor, and the Relay plugin'sInvalid global ID: …for an ID that isn't a global ID. If the plugin finds a problem in your configuration while running a request, the field fails withUnexpected error.. The original error is onerror.cause. Errors from the ORM and from your code are not changed and can include model and column names, so mask them in your GraphQL server. - Prisma's packages are ESM-only, so use
"moduleResolution": "nodenext"or"bundler". A CommonJS project onnode16gets TS1541 errors unlessskipLibCheckis on. - graphql-jit isn't supported. Fields that load rows fail with
Unexpected error.under it.
Next steps
Read Queries and selections for how collection, query and select work together,
then Objects and Relations for defining the rest of your schema.