From 1a1830572d440bb874cf58a23d43914686012662 Mon Sep 17 00:00:00 2001 From: Benjamin Palko Date: Mon, 2 Dec 2024 18:05:53 -0500 Subject: [PATCH] users and posts --- src/yoga/schema.ts | 55 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/yoga/schema.ts b/src/yoga/schema.ts index 8636b24..fad1003 100644 --- a/src/yoga/schema.ts +++ b/src/yoga/schema.ts @@ -1,11 +1,60 @@ import SchemaBuilder from "@pothos/core"; +import PrismaPlugin, { + type PrismaTypesFromClient, +} from "@pothos/plugin-prisma"; +import { PrismaClient } from "@prisma/client"; -const builder = new SchemaBuilder({}); +const prisma = new PrismaClient(); + +const builder = new SchemaBuilder<{ + 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) => ({ - hello: t.string({ - resolve: () => "world", + users: t.prismaField({ + type: [User], + resolve: async () => { + return await prisma.user.findMany(); + }, + }), + posts: t.prismaField({ + type: [Post], + resolve: async () => { + return await prisma.post.findMany(); + }, }), }), });