Skip to content

Commit ed29b15

Browse files
committed
feat: refactor book query to filter by title
1 parent a5297d0 commit ed29b15

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/graphql/resolvers/Book.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { Book } from "../../entity/Book";
33

44
export const BookResolvers = {
55
Query: {
6-
books: async () => {
6+
books: async (_: any, args: { title?: string }) => {
77
try {
88
const dataSourceInstance = await dataSource;
99
const bookRepository = dataSourceInstance.getRepository(Book);
10-
const books = await bookRepository.find();
10+
const books = await bookRepository.find({
11+
where: args.title ? { title: args.title } : {},
12+
});
1113
return books;
1214
} catch (error) {
1315
console.error("Error fetching books:", error);

src/graphql/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const typeDefs = `#graphql
77
type Query {
88
hello: String!
99
dbInfo: String!
10-
books: [Book!]!
1110
users: [User!]!
11+
books(title: String): [Book!]!
1212
}
1313
`;

0 commit comments

Comments
 (0)