Skip to content

Instantly share code, notes, and snippets.

@umutyerebakmaz
Last active April 7, 2020 08:36
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/0f7f2bb7f093f7971b73b6750775cfcc to your computer and use it in GitHub Desktop.
Save umutyerebakmaz/0f7f2bb7f093f7971b73b6750775cfcc to your computer and use it in GitHub Desktop.
How can I get the parent's argument to FieldResolver?
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;
}
}
@umutyerebakmaz
Copy link
Author

How can I get the parent's argument to FieldResolver?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment