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/64cbc9374b4f54c5c1be65062af2cb73 to your computer and use it in GitHub Desktop.
Save umutyerebakmaz/64cbc9374b4f54c5c1be65062af2cb73 to your computer and use it in GitHub Desktop.
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from 'typeorm';
import { ObjectType, Field, ID, Int, ArgsType, InputType } from 'type-graphql';
@Entity()
@ObjectType()
export class Book extends BaseEntity {
@PrimaryGeneratedColumn("uuid")
@Field(type => ID)
id: string;
@Column()
@Field()
title: string;
@Column()
@Field()
slug: string;
@Column()
@Field()
image: string;
@Column()
@Field()
description: string;
@Column()
@Field()
firstEditionYear: string;
@Column()
@Field()
edition: string;
@Column()
@Field()
numberOfPage: string;
@Column()
@Field()
isbn: string;
@Column()
@Field()
language: string;
}
@InputType()
export class CreateBookInput {
@Field()
title: string;
@Field()
slug: string;
@Field()
image: string;
@Field()
description: string;
@Field()
firstEditionYear: string;
@Field()
edition: string;
@Field()
numberOfPage: string;
@Field()
isbn: string;
@Field()
language: string;
}
@ArgsType()
export class BooksFilter {
@Field({ nullable: true })
title?: string;
@Field(type => Int, { nullable: true })
skip?: number;
@Field(type => Int, { nullable: true })
take?: number;
}
@ArgsType()
export class BookFilter {
@Field({ nullable: true })
id?: string;
@Field({ nullable: true })
title?: string;
@Field({ nullable: true })
slug?: string;
}
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