add pothos prisma plugin

This commit is contained in:
Benjamin Palko 2024-12-10 10:45:11 -05:00
parent a286fa4e0d
commit 0abdd5481d
3 changed files with 51 additions and 1 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -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",

View file

@ -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<typeof Context>;
export const builder = new SchemaBuilder<{ Context: ContextType }>({});
export const builder = new SchemaBuilder<{
Context: ContextType;
PrismaTypes: PrismaTypesFromClient<typeof prisma>;
}>({
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();
}
})
})
});