Last active
April 7, 2020 08:36
-
-
Save umutyerebakmaz/0f7f2bb7f093f7971b73b6750775cfcc to your computer and use it in GitHub Desktop.
How can I get the parent's argument to FieldResolver?
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 { getRepository } from 'typeorm'; | |
import { Resolver, Query, Args, Arg, Mutation, Authorized, FieldResolver, Root, Ctx } from 'type-graphql'; | |
import { Book, BookFilter, BooksFilter, CreateBookInput } from './book.entity'; | |
import { Author, AuthorInput } from '../author/author.entity'; | |
import { Category, CategoryInput } from '../category/category.entity'; | |
import { AuthorBook } from '../author-book/author-book.entity'; | |
import { BookReadingStatus } from '../book-reading-status/book-reading-status.entity'; | |
import { BookLike } from '../book-like/book-like.entity'; | |
import { UserLibrary } from '../user-library/user-library.entity'; | |
import { BookRating } from '../book-rating/book-rating.entity'; | |
@Resolver(Book) | |
export class BookResolver { | |
@Query(() => Book, { nullable: true }) | |
async book( | |
@Args() { id, title, slug }: BookFilter | |
): Promise<Book> { | |
if (id) { return this.bookRepository.findOne({ id }); } | |
if (title) { return this.bookRepository.findOne({ title }); } | |
if (slug) { return this.bookRepository.findOne({ slug }); } | |
} | |
@Query(() => [Book]) | |
async allBooks( | |
@Args() { title, skip, take }: BooksFilter | |
): Promise<Book[]> { | |
if (title) { return this.bookRepository.find({ where: `"title" ILIKE '${title}%'`, skip, take }); } | |
return this.bookRepository.find({ skip, take }); | |
} | |
@Authorized('ADMIN', 'MODERATOR', 'MEMBER') | |
@FieldResolver(() => BookReadingStatus, { nullable: true }) | |
async bookReadingStatus( | |
@Root() book: Book, | |
@Ctx() context: any | |
): Promise<BookReadingStatus> { | |
const bookId = book.id; | |
const userId = context.req.session.userId; | |
const data = await this.bookReadingStatusRepository.findOne({ userId, bookId }); | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I get the parent's argument to FieldResolver?