Prisma Next plugin
An experimental Pothos plugin for the Prisma ORM 8 collection client
The Prisma Next plugin integrates Prisma ORM 8 with Pothos to build a strongly typed GraphQL API from your Prisma models. Expose database fields and relations, add computed fields, and support Relay nodes and paginated connections. Your GraphQL types and fields can have different names and shapes from the underlying models.
Declare what each field needs, and the plugin combines those requirements with the incoming GraphQL query. Prisma loads related data together, often in a single SQL statement, so you don't need a separate resolver or database call for every relation. Field types are inferred from your Prisma contract.
Experimental
This plugin targets Prisma ORM 8.0.0-rc.23 exactly. Its API, and Prisma's, may change in any
release. For Prisma ORM 7, use the Prisma plugin.
The playground example runs the examples below against SQLite in the browser, and its SQL tab shows the statements each query runs.
Example
With a configured builder, define the posts your API exposes
and their author relation. These examples use the playground's Post and User models:
builder.prismaObject('Post', {
fields: (t) => ({
id: t.exposeID('id'),
title: t.exposeString('title'),
author: t.relation('author'),
}),
});The author's computed fullName declares the columns its resolver reads:
builder.prismaObject('User', {
fields: (t) => ({
id: t.exposeID('id'),
email: t.exposeString('email'),
// Selects its columns only when a query asks for this field.
fullName: t.string({
select: ['firstName', 'lastName'],
resolve: (user) => `${user.firstName} ${user.lastName}`,
}),
}),
});Add a recent-posts query that returns the ten latest published posts:
builder.queryType({
fields: (t) => ({
recentPosts: t.prismaField({
type: ['Post'],
query: { where: { published: 1 }, orderBy: { id: 'desc' }, limit: 10 },
}),
}),
});For { recentPosts { title author { fullName } } }, the posts and their authors load in one SQL
statement, including the columns needed to compute each author's name. Each root field usually
runs as one statement, with its requested relations, counts and connection pages included.
Relay connections
With the Relay plugin, t.relatedConnection adds a paginated list of
published posts to an author profile:
// Pages through the relation inside the query that loads the user.
builder.prismaObjectField('User', 'postConnection', (t) =>
t.relatedConnection('posts', {
query: { where: { published: 1 } },
totalCount: true,
}),
);t.prismaConnection does the same for query fields. See
Connections.
Next steps
To get started, follow Setup, then read Queries and selections, Objects and Relations. For an existing Prisma ORM 7 schema, see Migrating from the Prisma plugin.