Pothos

Drizzle plugin

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.

This plugin provides tighter integration with drizzle, making it easier to define GraphQL object types based on your drizzle tables, and resolving the relations between them in as few queries as possible. It also has integrations for the relay plugin to make defining nodes and connections easy and efficient.

Getting started

Install the plugin and add it to your builder, as described in Setup:

npm install --save @pothos/plugin-drizzle drizzle-orm@rc

Define an object type for a table, expose the columns you want in your API, and add the relations you want to be able to query through:

const UserRef = builder.drizzleObject('users', {
  name: 'User',
  fields: (t) => ({
    firstName: t.exposeString('firstName'),
    lastName: t.exposeString('lastName'),
    posts: t.relation('posts'),
  }),
});

builder.queryType({
  fields: (t) => ({
    me: t.drizzleField({
      type: 'users',
      resolve: (query, root, args, ctx) =>
        db.query.users.findFirst(query({ where: { id: ctx.userId } })),
    }),
  }),
});

Querying me { firstName posts { title } } loads the user and their posts in a single query through drizzles relational query builder.

On this page