From 2e1d0b2b5efad43c1b2bfab08c188c07870fd5af Mon Sep 17 00:00:00 2001 From: Benjamin Palko Date: Wed, 11 Dec 2024 15:26:24 -0500 Subject: [PATCH] separate schema into files and add mutations --- prisma/dev.db | Bin 28672 -> 32768 bytes prisma/dev.db-journal | Bin 8720 -> 0 bytes .../migration.sql | 37 ++++++ prisma/schema.prisma | 10 +- src/lib/server/pothos/builder.ts | 4 +- src/lib/server/pothos/index.ts | 4 +- src/lib/server/pothos/schema.ts | 53 --------- .../pothos/{ => schema}/Scalars/Date.ts | 2 +- .../pothos/{ => schema}/Scalars/index.ts | 0 src/lib/server/pothos/schema/index.ts | 18 +++ src/lib/server/pothos/schema/posts.ts | 110 ++++++++++++++++++ src/lib/server/pothos/schema/users.ts | 83 +++++++++++++ 12 files changed, 258 insertions(+), 63 deletions(-) delete mode 100644 prisma/dev.db-journal create mode 100644 prisma/migrations/20241211200947_add_timestamps/migration.sql delete mode 100644 src/lib/server/pothos/schema.ts rename src/lib/server/pothos/{ => schema}/Scalars/Date.ts (87%) rename src/lib/server/pothos/{ => schema}/Scalars/index.ts (100%) create mode 100644 src/lib/server/pothos/schema/index.ts create mode 100644 src/lib/server/pothos/schema/posts.ts create mode 100644 src/lib/server/pothos/schema/users.ts diff --git a/prisma/dev.db b/prisma/dev.db index 14dfc1187da260604e354d3f3987a255b013567c..78861349513cb8604672483db113172bbcab8ee6 100644 GIT binary patch delta 989 zcmc&yJ#5oJ6h1qDcAVrIAc{m4)fg%owP|g~&d*fRHnbIK616cuLJ*y6TZATYl(|1M4^z&X7c8YPh4ddn<_F?C1Wu3 zRHl?I&CX@%e4#|=rCg4l%AU_ixe`4o&Ch4^rLrAeC}rk~BVO7%%yL8d=gX6Y{6fjx z@U^`DX+T6n*j>qMW+*9=Bi-TCAxy8-6?H{wsm*dlDKDvaY}IL&sgq+po1qJNnz6mi z5K~c@=!Tm(oIzVZoPiy&$!(VXSA9o_>!_wO=Ep#2^*=n^8!bBB;o)KIih<0Zz8NR{ z0UmWO_}}0J9_Rwt2y{Eaz7K)b^)T+0*8Q$&09+)JSuu@xF(xKCEhfZKDwY-$F2->j z%W6nt6*iTWwWJp3M3jhgNtEK`cuGtr#I&FaikeEZaXyU_yr5`;s3v$8@rsT=0y{U)ka>gNm#@#vynn>cdNMO0nVZTvfL zd%TBW55PU(v|4?J11B6~esKv1vrVq!teE_tQ;NgSKSaSV)W>JC z1efXL<6IS!zw!jJH1aUBZ}#Th$;8IUU%&icOf@r diff --git a/prisma/dev.db-journal b/prisma/dev.db-journal deleted file mode 100644 index be0024efeb2a325009d71b5f0c2fb3a1a4e8d1c0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8720 zcmeI$F$w}P5CzbzAfg~zcmW%`g_+DGn|Kdv$!@Z+jg{DnXYeFe9?r(aGgt`z&(t6O zE5{h*&jlvS5ujRK%y; @@ -10,6 +10,8 @@ type PothosType = { Scalars: Scalars; }; +SchemaBuilder.allowPluginReRegistration = true; + export const builder = new SchemaBuilder({ plugins: [PrismaPlugin], prisma: { diff --git a/src/lib/server/pothos/index.ts b/src/lib/server/pothos/index.ts index d6bd62d..5d5aa13 100644 --- a/src/lib/server/pothos/index.ts +++ b/src/lib/server/pothos/index.ts @@ -1,3 +1 @@ -import { builder } from './builder'; - -export const Schema = builder.toSchema(); \ No newline at end of file +export * from './schema'; \ No newline at end of file diff --git a/src/lib/server/pothos/schema.ts b/src/lib/server/pothos/schema.ts deleted file mode 100644 index 0789677..0000000 --- a/src/lib/server/pothos/schema.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { prisma } from '$lib/server/prisma'; -import { builder } from './builder'; - -const User = builder.prismaObject('User', { - fields: (t) => ({ - id: t.exposeID('id'), - email: t.exposeString('email'), - name: t.exposeString('name'), - posts: t.relation('posts'), - createdAt: t.expose('createdAt', { - type: 'Date' - }), - updatedAt: t.expose('updatedAt', { - type: 'Date' - }) - }) -}); - -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'), - createdAt: t.expose('createdAt', { - type: 'Date' - }), - updatedAt: t.expose('updatedAt', { - type: 'Date' - }) - }) -}); - -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(); - } - }) - }) -}); \ No newline at end of file diff --git a/src/lib/server/pothos/Scalars/Date.ts b/src/lib/server/pothos/schema/Scalars/Date.ts similarity index 87% rename from src/lib/server/pothos/Scalars/Date.ts rename to src/lib/server/pothos/schema/Scalars/Date.ts index 6d61571..6573f0a 100644 --- a/src/lib/server/pothos/Scalars/Date.ts +++ b/src/lib/server/pothos/schema/Scalars/Date.ts @@ -1,4 +1,4 @@ -import { builder } from '../builder'; +import { builder } from '../../builder'; export const DateScalar = builder.scalarType('Date', { description: 'Date Scalar in ISO format', diff --git a/src/lib/server/pothos/Scalars/index.ts b/src/lib/server/pothos/schema/Scalars/index.ts similarity index 100% rename from src/lib/server/pothos/Scalars/index.ts rename to src/lib/server/pothos/schema/Scalars/index.ts diff --git a/src/lib/server/pothos/schema/index.ts b/src/lib/server/pothos/schema/index.ts new file mode 100644 index 0000000..acdab44 --- /dev/null +++ b/src/lib/server/pothos/schema/index.ts @@ -0,0 +1,18 @@ +import { builder } from '../builder'; + +builder.queryType({}); + +builder.queryField('version', (t) => + t.string({ + description: 'Application version', + resolve: (parent, args, context) => context.config.app_version + }) +); + +builder.mutationType({}); + +import './Scalars'; +import './posts'; +import './users'; + +export const Schema = builder.toSchema(); \ No newline at end of file diff --git a/src/lib/server/pothos/schema/posts.ts b/src/lib/server/pothos/schema/posts.ts new file mode 100644 index 0000000..f616871 --- /dev/null +++ b/src/lib/server/pothos/schema/posts.ts @@ -0,0 +1,110 @@ +import { prisma } from '$lib/server/prisma'; +import { builder } from '../builder'; + +export 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'), + createdAt: t.expose('createdAt', { + type: 'Date' + }), + updatedAt: t.expose('updatedAt', { + type: 'Date' + }) + }) +}); + +const CreatePost = builder.inputType('CreatePost', { + fields: (t) => ({ + title: t.string({ + required: true + }), + content: t.string({ + required: true + }), + published: t.boolean(), + authorId: t.id({ + required: true + }) + }) +}); + +const UpdatePost = builder.inputType('UpdatePost', { + fields: (t) => ({ + id: t.id({ + required: true + }), + title: t.string(), + content: t.string(), + published: t.boolean(), + authorId: t.id() + }) +}); + +builder.queryFields((t) => ({ + posts: t.prismaField({ + type: [Post], + resolve: async () => { + return await prisma.post.findMany(); + } + }) +})); + +builder.mutationFields((t) => ({ + createPost: t.field({ + type: Post, + args: { + input: t.arg({ required: true, type: CreatePost }) + }, + resolve: async (parent, args) => { + const author = await prisma.user.findUnique({ + where: { id: Number(args.input.authorId) } + }); + if (!author) { + throw new Error('Author does not exist!'); + } + const post = await prisma.post.create({ + data: { + title: args.input.title, + content: args.input.content, + published: args.input.published, + author: { + connect: { + id: author.id + } + } + } + }); + return post; + } + }), + updatePost: t.field({ + type: Post, + args: { + input: t.arg({ required: true, type: UpdatePost }) + }, + resolve: async (parent, args) => { + const post = await prisma.post.update({ + where: { + id: Number(args.input.id) + }, + data: { + title: args.input.title ?? undefined, + content: args.input.content ?? undefined, + published: args.input.published, + ...(args.input.authorId && { + author: { + connect: { + id: Number(args.input.authorId) + } + } + }) + } + }); + return post; + } + }) +})); \ No newline at end of file diff --git a/src/lib/server/pothos/schema/users.ts b/src/lib/server/pothos/schema/users.ts new file mode 100644 index 0000000..c15b8bb --- /dev/null +++ b/src/lib/server/pothos/schema/users.ts @@ -0,0 +1,83 @@ +import { prisma } from '$lib/server/prisma'; +import { builder } from '../builder'; + +export const User = builder.prismaObject('User', { + fields: (t) => ({ + id: t.exposeID('id'), + email: t.exposeString('email'), + name: t.exposeString('name'), + posts: t.relation('posts'), + createdAt: t.expose('createdAt', { + type: 'Date' + }), + updatedAt: t.expose('updatedAt', { + type: 'Date' + }) + }) +}); + +const CreateUser = builder.inputType('CreateUser', { + fields: (t) => ({ + email: t.string({ + required: true + }), + name: t.string({ + required: true + }) + }) +}); + +const UpdateUser = builder.inputType('UpdateUser', { + fields: (t) => ({ + id: t.id({ + required: true + }), + email: t.string(), + name: t.string() + }) +}); + +builder.queryFields((t) => ({ + users: t.prismaField({ + type: [User], + resolve: async () => { + return await prisma.user.findMany(); + } + }) +})); + +builder.mutationFields((t) => ({ + createUser: t.field({ + type: User, + args: { + input: t.arg({ required: true, type: CreateUser }) + }, + resolve: async (parent, args) => { + const post = await prisma.user.create({ + data: { + email: args.input.email, + name: args.input.name + } + }); + return post; + } + }), + updateUser: t.field({ + type: User, + args: { + input: t.arg({ required: true, type: UpdateUser }) + }, + resolve: async (parent, args) => { + const post = await prisma.user.update({ + where: { + id: Number(args.input.id) + }, + data: { + email: args.input.email, + name: args.input.name ?? undefined + } + }); + return post; + } + }) +})); \ No newline at end of file