Drizzle plugin
A plugin to support efficient queries through drizzles relational query builder API
This plugin uses drizzle's relational query builder v2,
which ships in drizzle-orm 1.0. That release is still a release candidate, and npm's latest
tag is the 0.x line, so install drizzle with the rc tag. Its API can still change before 1.0 is
final.
If you are upgrading from an older version of this plugin, read drizzle's relations v1 to v2 guide, then this package's changelog for the Pothos specific changes.
The Drizzle plugin maps database tables to GraphQL types and plans database queries from the fields a client selects. It supports relations, computed fields, and Relay nodes and connections.
The examples share a small author-and-post schema. The publishing playground includes its database definitions and initialization, and executes queries against SQLite in the browser.
Getting started
Setup connects your Drizzle client to Pothos. Objects covers type definitions and fields such as this lookup:
builder.queryType({
fields: (t) => ({
author: t.drizzleField({
type: 'users',
nullable: true,
args: { id: t.arg.int({ required: true }) },
resolve: (query, _root, args) => {
return db.query.users.findFirst(
query({
where: { id: args.id },
}),
);
},
}),
}),
});query AuthorPage {
author(id: 1) {
fullName
posts {
title
}
}
}The resolver passes Pothos's selection plan to Drizzle, loading the requested posts with their author. Adding GraphQL fields does not require rewriting this root resolver.
Relations covers filtering and counts; Selections explains computed fields and profiles; Connections handles pagination. Variants and Interfaces keep private account data separate from public authors. For more specialized schemas, Connection helpers and Async selections cover custom pagination and asynchronous planning.