Setup
Installing
npm install --save @pothos/plugin-drizzle drizzle-orm@rcThe drizzle plugin is built on top of drizzles relational query builder, and requires that you define and configure all the relevant relations in your drizzle schema. See https://orm.drizzle.team/docs/relations-v2 for detailed documentation on the relations API.
Once you have configured your drizzle schema, you can initialize your Pothos SchemaBuilder with the drizzle plugin:
import { drizzle } from 'drizzle-orm/...';
// Import the appropriate getTableConfig for your dialect
import { getTableConfig } from 'drizzle-orm/sqlite-core';
import SchemaBuilder from '@pothos/core';
import DrizzlePlugin from '@pothos/plugin-drizzle';
import { relations } from './db/relations';
const db = drizzle({ client, relations });
type DrizzleRelations = typeof relations
export interface PothosTypes {
DrizzleRelations: DrizzleRelations;
}
const builder = new SchemaBuilder<PothosTypes>({
plugins: [DrizzlePlugin],
drizzle: {
client: db, // or (ctx) => db if you want to create a request specific client
getTableConfig,
relations,
},
});Integration with other plugins
The drizzle plugin has integrations with several other plugins. While the with-input and relay
plugins are not required, many examples will assume these plugins have been installed:
import { drizzle } from 'drizzle-orm/...';
import SchemaBuilder from '@pothos/core';
import DrizzlePlugin from '@pothos/plugin-drizzle';
import RelayPlugin from '@pothos/plugin-relay';
import WithInputPlugin from '@pothos/plugin-with-input';
import { getTableConfig } from 'drizzle-orm/sqlite-core';
import { relations } from './db/relations';
const db = drizzle({ client, relations });
export interface PothosTypes {
DrizzleRelations: typeof relations;
}
const builder = new SchemaBuilder<PothosTypes>({
plugins: [RelayPlugin, WithInputPlugin, DrizzlePlugin],
drizzle: {
client: db,
getTableConfig,
relations,
},
});Plugin options
client: the drizzle client, or a function returning one from the request context.getTableConfig: thegetTableConfigof your dialect, used to read primary keys and unique constraints.relations: the relations passed todrizzle().defaultConnectionSize/maxConnectionSize: the page size a connection uses when the query does not ask for one (defaults to 20), and the largest size it will accept (defaults to 100). Both can also be set per field withdefaultSizeandmaxSize.filterConnectionTotalCount: see Connection totalCount.skipDeferredFragments: selections inside a@deferfragment are left out of the planned query by default, and the fragment's fields are loaded through fallback queries when it resolves. Set this tofalseto plan deferred selections with the rest of the query.