Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/graphql/post/resolvers/Query/getPosts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import type { QueryResolvers } from '@/graphql/types';
export const getPosts: NonNullable<QueryResolvers['getPosts']> = async (_parent, _arg, _ctx) => {
/* Implement Query.getPosts resolver logic here */

export const getPosts: NonNullable<QueryResolvers['getPosts']> = (_parent, { input, page }, { services }) => {
const filter = input.userId != null ? { userId: input.userId } : {};

const pagination: Pagination | undefined = page
? {
limit: page.limit ?? undefined,
offset: page.offset ?? undefined,
}
: undefined;

return services.Post.getPosts(filter, pagination);
};
17 changes: 17 additions & 0 deletions src/modules/post/datasources/query.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Repo } from '@/infra/database';
import type { Post } from '@/modules/post/types';
import type { Store } from '@/modules/types';

Expand All @@ -15,4 +16,20 @@ export const PostQueries = (store: Store) => ({
where,
});
},

async getPosts(where: Repo.PostWhereInput | undefined, page?: Pagination): Promise<Post[]> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Las consultas deben desacoplarse a travez de interfaces, no usemos directamente la de Prisma, sino que establezcamos una nueva en "types", verifica como esta implementado en "Comments". La finalidad es desacoplar, si usamos el mismo de prisma, acoplamos por el tipo, lo que vuelve dependiente a nuestro datasource de prisma directamente, si en el futuro quisiera cambiar el ORM lib, esto causaria problemas.

return store.pa.post.findMany({
include: { base: true },
where,
orderBy: [
{
base: {
createdAt: 'desc',
},
},
],
skip: page?.offset ?? undefined,
take: page?.limit ?? undefined,
});
},
});
10 changes: 9 additions & 1 deletion src/modules/post/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CreatePostDTO, Post } from '@/modules/post/types';
import type { Repo } from '@/infra/database';
import type { CreatePostDTO, Post, PostFilter } from '@/modules/post/types';
import type { Context } from '@/modules/types';

export const PostService = (ctx: Context) => ({
Expand Down Expand Up @@ -29,6 +30,13 @@ export const PostService = (ctx: Context) => ({
return ctx.ds.Post.getPost(input);
},

async getPosts(input: PostFilter, page?: Pagination): Promise<Post[]> {
const where: Repo.PostWhereInput | undefined =
input.userId !== undefined ? { base: { userId: input.userId } } : undefined;

return ctx.ds.Post.getPosts(where, page);
},

// getPosts(query: string, limit?: number): Promise<Post[]> {
// return this.ds.Posts.getPosts(query, limit);
// }
Expand Down
1 change: 1 addition & 0 deletions src/modules/post/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Visibility, type Repo } from '@/infra/database';
export type Post = Repo.PostGetPayload<{ include: { base: true } }>;
export type PostFilter = Partial<UserId>;

export type RepoUpdatePost = Tools.AtLeastOne<{
title?: string;
Expand Down