Skip to content

Instantly share code, notes, and snippets.

@umutyerebakmaz
Created December 2, 2019 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umutyerebakmaz/101dd19f7669bcf41deeea42d3161300 to your computer and use it in GitHub Desktop.
Save umutyerebakmaz/101dd19f7669bcf41deeea42d3161300 to your computer and use it in GitHub Desktop.
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