diff --git a/bun.lockb b/bun.lockb index 83c74e3..48530f5 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index a87cdc4..87c7b5f 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ }, "dependencies": { "@pothos/core": "^4.3.0", + "@pothos/plugin-prisma": "^4.4.0", "@prisma/client": "6.0.1", "@tailwindcss/typography": "^0.5.15", "@types/bun": "^1.1.14", diff --git a/src/lib/pothos/index.ts b/src/lib/pothos/index.ts index 934f50a..c8ee3d6 100644 --- a/src/lib/pothos/index.ts +++ b/src/lib/pothos/index.ts @@ -1,14 +1,63 @@ +import { prisma } from '$lib/prisma'; import { Context } from '$lib/yoga/context'; import SchemaBuilder from '@pothos/core'; +import PrismaPlugin, { type PrismaTypesFromClient } from '@pothos/plugin-prisma'; type ContextType = ReturnType; -export const builder = new SchemaBuilder<{ Context: ContextType }>({}); +export const builder = new SchemaBuilder<{ + Context: ContextType; + PrismaTypes: PrismaTypesFromClient; +}>({ + plugins: [PrismaPlugin], + prisma: { + client: prisma, + // defaults to false, uses /// comments from prisma schema as descriptions + // for object types, relations and exposed fields. + // descriptions can be omitted by setting description to false + exposeDescriptions: false, + // use where clause from prismaRelatedConnection for totalCount (defaults to true) + filterConnectionTotalCount: true, + // warn when not using a query parameter correctly + onUnusedQuery: process.env.NODE_ENV === 'production' ? null : 'warn' + } +}); + +const User = builder.prismaObject('User', { + fields: (t) => ({ + id: t.exposeID('id'), + email: t.exposeString('email'), + name: t.exposeString('name'), + posts: t.relation('posts') + }) +}); + +const Post = builder.prismaObject('Post', { + fields: (t) => ({ + id: t.exposeID('id'), + title: t.exposeString('title'), + content: t.exposeString('content'), + published: t.exposeBoolean('published'), + author: t.relation('author') + }) +}); builder.queryType({ fields: (t) => ({ version: t.string({ resolve: (parent, args, context) => context.config.app_version + }), + users: t.prismaField({ + type: [User], + resolve: async () => { + return await prisma.user.findMany(); + } + }), + posts: t.prismaField({ + type: [Post], + resolve: async () => { + return await prisma.post.findMany(); + } }) }) });