ln/app/server/graphql/resolvers/bookManyResolver.ts

33 lines
1,007 B
TypeScript

import { IdentityContext } from '@app/server/identity';
import { prisma } from '@app/server/prisma/client';
import { BookManyOutput, QueryBookManyArgs } from '@generated/types';
import { Prisma } from '@prisma/client';
import { toBookSchema } from './mappers/bookMapper';
export async function BookManyResolver(
_: any,
{ input }: QueryBookManyArgs,
{ req, res, identity }: IdentityContext
): Promise<BookManyOutput> {
const { filter } = input;
const terms = filter.terms?.toLowerCase();
const bookWhere: Prisma.BookWhereInput = {
OR:
terms != null ? [{ title: { contains: terms } }, { author: { username: terms } }] : undefined
};
const items = await prisma.book.findMany({
where: bookWhere,
skip: filter.offset ?? 0,
take: filter.size ?? 10,
orderBy: {
createdAt: 'desc'
}
});
return {
items: await Promise.all(items.map(value => toBookSchema(value, { req, res, identity }))),
total: await prisma.book.count({ where: bookWhere })
};
}