Created
December 2, 2019 14:53
-
-
Save umutyerebakmaz/101dd19f7669bcf41deeea42d3161300 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Resolver, Query, Args, Arg, Mutation } from "type-graphql"; | |
import { Book, BookFilter, BooksFilter, CreateBookInput } from './book.entity'; | |
import { Like } from 'typeorm'; | |
@Resolver(Book) | |
export class BookResolver { | |
@Query(returns => [Book]) | |
async allBooks( | |
@Args() { title, skip, take }: BooksFilter | |
): Promise<Book[]> { | |
if (title) { | |
return Book.find({ where: { name: Like(`%${title}%`), skip, take } }) | |
} | |
return Book.find({ skip, take }); | |
} | |
@Query(returns => Book) | |
async book( | |
@Args() { id, title, slug }: BookFilter | |
): Promise<Book> { | |
if (id) { | |
return Book.findOne({ id }) | |
} | |
if (title) { | |
return Book.findOne({ title }) | |
} | |
if (slug) { | |
return Book.findOne({ slug }) | |
} | |
throw new Error('book not found'); | |
}; | |
@Mutation(returns => Book) | |
async createBook( | |
@Arg('book') bookInput: CreateBookInput | |
): Promise<CreateBookInput> { | |
const book = new Book(); | |
book.title = bookInput.title; | |
book.slug = bookInput.slug; | |
book.image = bookInput.image; | |
book.description = bookInput.description; | |
book.firstEditionYear = bookInput.firstEditionYear; | |
book.edition = bookInput.numberOfPage; | |
book.numberOfPage = bookInput.numberOfPage; | |
book.isbn = bookInput.isbn; | |
book.language = bookInput.language; | |
await book.save(); | |
return book; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment