-
-
Save hylickipiotr/60b1da63843b634d6c05f96f9d01b2cd to your computer and use it in GitHub Desktop.
TypeORM many to many lazy relation jointable custom table name TypeGraphQL Example (with bi-directional conect GraphQL Approach)
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 { Entity, PrimaryGeneratedColumn, Column, BaseEntity, ManyToOne, ManyToMany, JoinTable} from 'typeorm'; | |
import { ObjectType, Field, ID, Int, ArgsType, InputType } from 'type-graphql'; | |
import { Publisher } from '../publisher/publisher.entity'; | |
import { Author } from '../author/author.entity'; | |
@Entity() | |
@ObjectType() | |
export class Book extends BaseEntity { | |
@PrimaryGeneratedColumn("uuid") | |
@Field(type => ID) | |
id: string; | |
@Column() | |
@Field() | |
title: string; | |
@Column({ unique: true }) | |
@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; | |
@Column() | |
publisherId: string; | |
@ManyToOne(type => Publisher, { lazy: true }) | |
@Field(type => Publisher) | |
publisher: Promise<Publisher>; | |
// book_authors | |
@ManyToMany(type => Author, author => author.books, { lazy: true }) | |
@JoinTable({ | |
name: "book_author", // table name for the junction table of this relation | |
joinColumn: { | |
name: "bookId", | |
referencedColumnName: "id" | |
}, | |
inverseJoinColumn: { | |
name: "authorId", | |
referencedColumnName: "id" | |
} | |
}) | |
@Field(type => [Author]) | |
authors: Promise<Author[]>; | |
} | |
@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; | |
@Field() | |
publisherId: 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; | |
} | |
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, Authorized } from "type-graphql"; | |
import { Book, BookFilter, BooksFilter, CreateBookInput } from './book.entity'; | |
import { Like, getRepository } from 'typeorm'; | |
import { GraphQLUpload } from 'graphql-upload'; | |
import { createWriteStream } from 'fs'; | |
import { Upload } from '../../types/Upload'; | |
import * as path from 'path'; | |
import { Author, AuthorInput } from '../author/author.entity'; | |
@Resolver(Book) | |
export class BookResolver { | |
@Query(returns => [Book]) | |
async allBooks( | |
@Args() { title, skip, take }: BooksFilter | |
): Promise<Book[]> { | |
if (title) { | |
return Book.find({ where: { title: 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') input: CreateBookInput, | |
@Arg('authors', type => [AuthorInput]) authors: Author[] | |
): Promise<CreateBookInput> { | |
const book = new Book(); | |
book.title = input.title; | |
book.slug = input.slug; | |
book.image = input.image; | |
book.description = input.description; | |
book.firstEditionYear = input.firstEditionYear; | |
book.edition = input.numberOfPage; | |
book.numberOfPage = input.numberOfPage; | |
book.isbn = input.isbn; | |
book.language = input.language; | |
book.publisherId = input.publisherId; | |
book.authors = Promise.resolve(authors); | |
await book.save(); | |
return book; | |
} | |
@Mutation(returns => Book) | |
async updateBook( | |
@Arg('id') id: string, | |
@Arg('book') input: CreateBookInput | |
): Promise<CreateBookInput> { | |
const bookRepository = getRepository(Book); | |
const book = await bookRepository.findOne(id); | |
if (!book) { | |
throw new Error('book not found'); | |
} | |
book.title = input.title; | |
book.slug = input.slug; | |
book.image = input.image; | |
book.description = input.description; | |
book.firstEditionYear = input.firstEditionYear; | |
book.edition = input.edition; | |
book.numberOfPage = input.numberOfPage; | |
book.isbn = input.isbn; | |
book.language = input.language; | |
book.publisherId = input.publisherId; | |
await bookRepository.save(book); | |
return book; | |
} | |
@Authorized("ADMIN") | |
@Mutation(returns => Book) | |
async deleteBook( | |
@Arg('id') id: string | |
): Promise<Book> { | |
const bookRepository = getRepository(Book); | |
const book = await bookRepository.findOne(id); | |
if (!book) { | |
throw new Error('book not found!') | |
} | |
await bookRepository.delete({ id }); | |
return book; | |
} | |
@Mutation(returns => Boolean) | |
async createBookImage(@Arg('file', () => GraphQLUpload) file: Upload) { | |
const { filename, mimetype, createReadStream } = await file; | |
const acceptedTypes = ['image/jpeg', 'image/png']; | |
if (acceptedTypes.indexOf(mimetype) !== -1) { | |
const stream = createReadStream(); | |
stream.pipe(createWriteStream(path.join(__dirname, `../../../images/books/${filename}`))); | |
return true; | |
} | |
throw new Error('Unsupported image type.') | |
}; | |
} | |
// postman operations | |
// {"query":"mutation CreateBookImage($file: Upload!) {\n createBookImage(file: $file)\n}\n"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment