import { Controller , Mutation , Query } from "../../../../src" ; import { Post } from "../entity/Post" ; import { PostSaveArgs } from "../args/PostSaveArgs" ; import { EntityManager , FindManyOptions } from "typeorm" ; import { PostsArgs } from "../args/PostsArgs" ; @ Controller ( ) export class PostController { constructor ( private entityManager ) { } @ Query ( ) posts ( args ) { const findOptions = { } ; if ( args . limit ) findOptions . take = args . limit ; if ( args . offset ) findOptions . skip = args . offset ; if ( args . sortBy === "last" ) findOptions . order = { "id" : "DESC" } ; if ( args . sortBy === "name" ) findOptions . order = { "name" : "ASC" } ; return this . entityManager . find ( Post , findOptions ) ; } @ Query ( ) post ( { id } ) { return this . entityManager . findOne ( Post , id ) ; } @ Mutation ( ) async postSave ( args ) { const post = args . id ? await this . entityManager . findOneOrFail ( Post , args . id ) : new Post ( ) ; post . title = args . title ; post . text = args . text ; return this . entityManager . save ( post ) ; } @ Mutation ( ) async postDelete ( { id } ) { const post = await this . entityManager . findOne ( Post , id ) ; await this . entityManager . remove ( post ) ; return true ; } }