Skip to content

Schema API Reference

function buildSchema(
db: PgDatabase<any, any, any>,
config?: BuildSchemaConfig,
): {
schema: GraphQLSchema
entities: GeneratedEntities
withPermissions: (permissions: PermissionConfig) => GraphQLSchema
clearPermissionCache: (id?: string) => void
}

Main entry point. Takes a Drizzle PgDatabase instance and optional configuration, returns the generated GraphQL schema, entity definitions, and permission helpers.

function buildSchemaFromDrizzle(
drizzleSchema: Record<string, unknown>,
config?: BuildSchemaConfig,
): {
schema: GraphQLSchema
entities: GeneratedEntities
withPermissions: (permissions: PermissionConfig) => GraphQLSchema
clearPermissionCache: (id?: string) => void
}

Builds a schema directly from Drizzle schema exports without a database connection. Creates stub resolvers — intended for introspection, codegen, and testing.

function buildEntities(
db: PgDatabase<any, any, any>,
config?: BuildSchemaConfig,
): GeneratedEntities

Returns the GeneratedEntities object (queries, mutations, inputs, types) without building a full GraphQLSchema. Useful for composing into an existing schema.

function generateSDL(
schema: GraphQLSchema,
): string

Generates the GraphQL SDL string from a GraphQLSchema (as produced by buildSchema or buildSchemaFromDrizzle). Codegen utility.

function generateTypes(
schema: GraphQLSchema,
options?: CodegenOptions,
): string

Generates TypeScript type definitions from a GraphQLSchema. Codegen utility.

function generateEntityDefs(
schema: GraphQLSchema,
options?: CodegenOptions,
): string

Generates entity definition types for use with @graphql-suite/client from a GraphQLSchema. Codegen utility.

function permissive(
id: string,
tables?: Record<string, boolean | TableAccess>,
): PermissionConfig

Creates a permissive permission config. In permissive mode, all tables are accessible unless explicitly denied.

function restricted(
id: string,
tables?: Record<string, boolean | TableAccess>,
): PermissionConfig

Creates a restricted permission config. In restricted mode, all tables are denied unless explicitly allowed.

function readOnly(): TableAccess

Returns a TableAccess object with query: true and all mutations set to false.

function withRowSecurity(
rules: Record<string, (context: any) => Record<string, unknown>>,
): HooksConfig

Generates a HooksConfig that injects WHERE clauses from row-level security rules. Rules are applied as before hooks on query, querySingle, count, update, and delete operations.

function mergeHooks(...configs: HooksConfig[]): HooksConfig

Merges multiple hook configurations. When multiple hooks target the same table and operation, they are composed in order.

class SchemaBuilder {
constructor(db: PgDatabase<any, any, any>, config?: BuildSchemaConfig)
build(): {
schema: GraphQLSchema
entities: GeneratedEntities
withPermissions: (permissions: PermissionConfig) => GraphQLSchema
clearPermissionCache: (id?: string) => void
}
buildEntities(): GeneratedEntities
}

The underlying builder class. Exposed for advanced use cases where you need more control over the build process.

Custom GraphQL scalar type for JSONB columns. Serializes and parses arbitrary JSON values.

type BuildSchemaConfig = {
mutations?: boolean
limitRelationDepth?: number
limitSelfRelationDepth?: number
suffixes?: { list?: string; single?: string }
hooks?: HooksConfig
tables?: {
exclude?: readonly string[]
config?: Record<string, TableOperations>
}
pruneRelations?: Record<string, RelationPruneRule>
debug?: boolean | { schemaSize?: boolean; relationTree?: boolean }
}
type GeneratedEntities = {
queries: Record<string, GraphQLFieldConfig<any, any>>
mutations: Record<string, GraphQLFieldConfig<any, any>>
inputs: Record<string, GraphQLInputObjectType>
types: Record<string, GraphQLObjectType>
}
type PermissionConfig = {
id: string
mode: 'permissive' | 'restricted'
tables?: Record<string, boolean | TableAccess>
}
type TableAccess = {
query?: boolean
insert?: boolean
update?: boolean
delete?: boolean
}
type TableOperations = {
queries?: boolean
mutations?: boolean
}
type RelationPruneRule = false | 'leaf' | { only: string[] }
type HooksConfig = {
[tableName: string]: TableHookConfig
}
type TableHookConfig = {
[K in OperationType]?: OperationHooks
}
type OperationHooks =
| { before?: BeforeHookFn; after?: AfterHookFn }
| { resolve: ResolveHookFn }
type BeforeHookFn = (
ctx: HookContext,
) => Promise<BeforeHookResult | undefined> | BeforeHookResult | undefined
type AfterHookFn = (ctx: AfterHookContext) => Promise<any> | any
type ResolveHookFn = (ctx: ResolveHookContext) => Promise<any> | any
type HookContext = {
args: any
context: any
info: GraphQLResolveInfo
}
type AfterHookContext = {
result: any
beforeData: any
context: any
info: GraphQLResolveInfo
}
type ResolveHookContext = HookContext & {
defaultResolve: (overrideArgs?: any) => Promise<any>
}
type OperationType =
| 'query'
| 'querySingle'
| 'count'
| 'insert'
| 'insertSingle'
| 'update'
| 'delete'
type BeforeHookResult = {
args?: any
data?: any
}
type CodegenOptions = {
drizzle?: {
importPath: string
typeNames?: Record<string, string>
}
}