@vyrel/morph
v0.0.8
Published
Vyrel morph
Maintainers
Readme
@vyrel/morph
Bridge Zod row schemas to Pothos GraphQL fields, args, and inputs.
Use it to derive GraphQL object fields, list filters, and input shapes from Zod models without hand-mapping every column.
Install
bun install @vyrel/morphPeer dependencies:
bun install @pothos/core @pothos/plugin-validation @pothos/plugin-with-input zodQuick start
import SchemaBuilder from "@pothos/core";
import ValidationPlugin from "@pothos/plugin-validation";
import WithInputPlugin from "@pothos/plugin-with-input";
import { initializeDrizzleGraphqlBridge } from "@vyrel/morph";
import { z } from "zod/v4";
const builder = new SchemaBuilder({
plugins: [ValidationPlugin, WithInputPlugin],
});
const bridge = initializeDrizzleGraphqlBridge(builder, {
defaultIdFields: ["id", "orgId"],
unmappedFields: "throw",
});
const userRowSchema = z.object({
id: z.string(),
active: z.boolean(),
role: z.enum(["admin", "member"]),
});
const userGraphql = bridge.model({
objectName: "User",
rowSchema: userRowSchema,
listArgsSchema: {
filters: z.object({
role: z.enum(["admin", "member"]),
}),
},
});
const User = builder.objectRef<z.infer<typeof userRowSchema>>("User");
builder.objectType(User, {
fields: (t) => userGraphql.exposeFields(t),
});API
initializeDrizzleGraphqlBridge(builder, options?)
Creates a bridge bound to your Pothos builder.
Options:
defaultIdFields— columns exposed as GraphQLID(default: none; bridge defaults to["id", "orgId"]when omitted in model config)defaultEnumName— naming strategy for generated enumsscalarTypes— extra scalar names registered on the builderunmappedFields—"throw","warn", or"omit"when a Zod field cannot map to GraphQL
Returns:
bridge.model(config)— model helpers for a Zod row schemabridge.fields(config)— alias ofmodelbridge.inputsFrom(schema, options?)— shared input mapper
bridge.model(config)
rowSchema— Zod object schema for the rowobjectName— GraphQL type name used for enum registrationexclude— row keys omitted from GraphQL exposureidFields— override ID columns for this modellistArgsSchema— named Zod schemas converted to query/list argsextraEnums/extraEnumsFrom— register additional enum sourcescomputedEnumFields— resolver-backed enum fields
Model helpers:
exposeFields(t, options?)— map row columns to Pothos object fieldsinputsFrom(schema, options?)— map a Zod schema to input fieldsargsFrom(schema, options?)— map a Zod schema to field argsargs— pre-built args whenlistArgsSchemais configured
Mapping behavior
z.string()→String, orIDwhen the key is listed inidFieldsz.boolean()→Booleanz.enum()/z.literal()→ generated GraphQL enums- nullable and optional Zod fields → nullable GraphQL fields
- Zod
.describe()→ GraphQL field descriptions
Unmapped Zod types follow unmappedFields (throw by default).
License
MIT
