Skip to content

Commit 16517cb

Browse files
committed
refactor: switch from getRawMany to getMany for entity mapping
1 parent ff0fd46 commit 16517cb

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/graphql/resolvers/Book.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,34 @@ export const BookResolvers = {
4343
});
4444
if (user) {
4545
userId = user.id;
46-
queryBuilder.leftJoinAndSelect(
47-
Favorite,
48-
"favorite",
49-
"favorite.bookId = book.id AND favorite.userId = :userId",
50-
{ userId }
51-
);
5246
}
5347
}
5448

49+
queryBuilder.leftJoinAndSelect("book.favorites", "favorite");
50+
5551
books = await queryBuilder
5652
.orderBy("book.id", "ASC")
5753
.skip(skip)
5854
.take(take)
59-
.getRawMany();
55+
.getMany();
6056

6157
const newCursor = skip + books.length;
6258

6359
return {
6460
cursor: newCursor,
6561
books: books.map((book) => {
66-
const isFavorited = book.favorite_id !== null;
62+
const isFavorited = userId
63+
? book.favorites.some((favorite) => favorite.userId === userId)
64+
: false;
6765

6866
return {
69-
id: book.book_id,
70-
title: book.book_title,
71-
author: book.book_author,
72-
publicationDate: book.book_publicationDate,
73-
image: book.book_image,
74-
rating: book.book_rating,
75-
ratingsCount: book.book_ratingsCount,
67+
id: book.id,
68+
title: book.title,
69+
author: book.author,
70+
publicationDate: book.publicationDate,
71+
image: book.image,
72+
rating: book.rating,
73+
ratingsCount: book.ratingsCount,
7674
isFavorited,
7775
};
7876
}),

0 commit comments

Comments
 (0)