/** biome-ignore-all lint/complexity/noVoid: type testing */ import { and, count, eq, sql } from 'drizzle-orm'; import { builder } from '../builder'; import { db } from '../db'; import { comments, postLikes, posts } from '../db/schema'; import { Comment } from './comment'; const Post = builder.drizzleObject('posts', { name: 'Post', select: { columns: { postId: true, }, }, fields: (t) => ({ id: t.exposeID('postId'), title: t.exposeString('title'), content: t.exposeString('content'), author: t.relation('author'), category: t.string({ select: { with: { category: true, }, }, resolve: (post) => post.category?.name, }), createdAt: t.field({ select: { columns: { createdAt: true, }, }, type: 'DateTime', resolve: (post) => new Date(post.createdAt), }), updatedAt: t.field({ type: 'DateTime', select: { columns: { updatedAt: true, }, }, resolve: (post) => new Date(post.updatedAt), }), published: t.boolean({ select: { columns: { published: true, }, }, resolve: (post) => !!post.published, }), comments: t.relation('comments'), likes: t.int({ resolve: async (post) => { const result = await db .select({ count: count() }) .from(postLikes) .where(eq(postLikes.postId, post.postId!)); return result[0].count ?? 0; }, }), }), }); builder.queryFields((t) => ({ post: t.drizzleField({ type: 'posts', args: { id: t.arg.id({ required: true }), }, resolve: async (query, _root, args) => { const result = await db.query.posts.findFirst( query({ columns: { postId: true, }, where: { postId: Number.parseInt(args.id, 10), }, }), ); void result?.postId; // @ts-expect-error only selected columns are accessible void result?.authorId; return result; }, }), postWithErrors: t.drizzleField({ type: 'posts', errors: { types: [Error], }, args: { id: t.arg.id({ required: true }), }, resolve: (query, _root, args) => db.query.posts.findFirst( query({ where: { postId: Number.parseInt(args.id, 10), }, }), ), }), posts: t.drizzleConnection({ type: 'posts', args: { category: t.arg.string(), invert: t.arg.boolean(), sortByCategory: t.arg.boolean(), }, resolve: (query, _root, args) => { const q = query({ where: { published: 1, ...(args.category ? { category: { name: args.category, }, } : {}), }, orderBy: () => { if (args.sortByCategory) { return { categoryId: 'asc', postId: 'asc', }; } return args.invert ? { postId: 'asc' } : { postId: 'desc' }; }, }); // console.dir(q, { depth: null }); return db.query.posts.findMany(q); }, }), postsByAuthor: t.drizzleConnection({ type: 'posts', resolve: (query) => db.query.posts.findMany( query({ // authorId is not unique: 10 authors with 15 posts each orderBy: { authorId: 'asc' }, }), ), }), postsByTitleLength: t.drizzleConnection({ type: 'posts', resolve: (query) => db.query.posts.findMany( query({ // ordering by an expression the query selects, rather than a column extras: { titleLength: (table) => sql`length(${table.title})` }, orderBy: { titleLength: 'asc' }, }), ), }), postsMissingOrderByExtra: t.drizzleConnection({ type: 'posts', resolve: (query) => db.query.posts.findMany(query({ orderBy: { notAColumn: 'asc' } })), }), postsBySlug: t.drizzleConnection({ type: 'posts', resolve: (query) => // slug is unique but nullable, so it neither counts as a unique ordering // nor compares like an ordinary value db.query.posts.findMany(query({ orderBy: { slug: 'asc' } })), }), postsWithCount: t.drizzleConnection({ type: 'posts', totalCount: () => db.$count(posts, eq(posts.published, 1)), resolve: (query) => { const q = query({ where: { published: 1, }, orderBy: { postId: 'desc' }, }); return db.query.posts.findMany(q); }, }), })); builder.mutationFields((t) => ({ likePost: t.withAuth({ loggedIn: true }).drizzleField({ type: 'posts', args: { postId: t.arg.id({ required: true }), }, resolve: async (query, _root, args, ctx) => { const postId = Number.parseInt(args.postId, 10); await db .insert(postLikes) .values({ postId, userId: ctx.user.id, }) .onConflictDoNothing(); return db.query.posts.findFirst( query({ where: { postId, }, }), ); }, }), commentOnPost: t.withAuth({ loggedIn: true }).fieldWithInput({ type: Comment, input: { postId: t.input.id({ required: true }), text: t.input.string({ required: true }), }, resolve: async (_root, { input }, ctx) => { const result = await db .insert(comments) .values([ { postId: Number.parseInt(input.postId, 10), authorId: ctx.user.id, text: input.text, }, ]) .returning(); return result[0]; }, }), createPost: t.withAuth({ role: 'author' }).fieldWithInput({ type: Post, input: { title: t.input.string({ required: true }), content: t.input.string({ required: true }), category: t.input.string({ required: true }), published: t.input.boolean(), }, resolve: async (_root, { input }, ctx) => { const result = await db .insert(posts) .values([ { title: input.title, content: input.content, authorId: ctx.user.id, categoryId: sql`(select id from categories where name = ${input.category})`, published: input.published ? 1 : 0, }, ]) .returning(); return result[0]; }, }), publishPost: t.withAuth({ role: 'author' }).drizzleField({ type: 'posts', args: { postId: t.arg.id({ required: true }), }, resolve: async (query, _root, args, ctx) => { const postId = Number.parseInt(args.postId, 10); await db .update(posts) .set({ published: 1, }) .where(and(eq(posts.postId, postId), eq(posts.authorId, ctx.user.id))); return db.query.posts.findFirst( query({ where: { postId, }, }), ); }, }), })); builder.objectType(Error, { name: 'Error', fields: (t) => ({ message: t.exposeString('message'), stack: t.exposeString('stack'), }), });