import { Resolver , Mutation , Args , Query , Context } from '@nestjs/graphql' ; import { getMongoRepository } from 'typeorm' ; import { ForbiddenError , ApolloError } from 'apollo-server-core' ; import { User } from '../entities/user.entity' ; import { Node } from '@entities' ; import { CreateNodeInput , UpdateNodeInput , SearchNodeInput , NodeCategory } from '../generator/graphql.schema' ; @ Resolver ( 'Node' ) export class NodeResolver { @ Query ( ) async nodes ( ) { return await getMongoRepository ( Node ) . find ( { cache : true } ) ; } @ Mutation ( ) async createNode ( @ Args ( 'input' ) input ) { const { parentId , name , category } = input ; const { CITY , STORE , DEPARTMENT , POSITION , JOB } = NodeCategory ; let foundNode ; let node ; switch ( category ) { case CITY : foundNode = await getMongoRepository ( Node ) . findOne ( { where : { city : { name } } } ) ; if ( foundNode ) { throw new ForbiddenError ( 'Node already existed.' ) ; } node = { ... input , city : { name } } ; break ; case STORE : foundNode = await getMongoRepository ( Node ) . findOne ( { where : { store : { name } } } ) ; if ( foundNode ) { throw new ForbiddenError ( 'Node already existed.' ) ; } node = { ... input , store : { name } } ; break ; case DEPARTMENT : foundNode = await getMongoRepository ( Node ) . findOne ( { where : { department : { name } } } ) ; if ( foundNode ) { throw new ForbiddenError ( 'Node already existed.' ) ; } node = { ... input , department : { name } } ; break ; case POSITION : foundNode = await getMongoRepository ( Node ) . findOne ( { where : { position : { name } } } ) ; if ( foundNode ) { throw new ForbiddenError ( 'Node already existed.' ) ; } node = { ... input , position : { name } } ; break ; case JOB : foundNode = await getMongoRepository ( Node ) . findOne ( { where : { job : { name } } } ) ; if ( foundNode ) { throw new ForbiddenError ( 'Node already existed.' ) ; } node = { ... input , job : { name } } ; break ; default : if ( parentId ) { throw new ForbiddenError ( "category is COMPANY don't need parentId." ) ; } foundNode = await getMongoRepository ( Node ) . findOne ( { where : { company : { name } } } ) ; if ( foundNode ) { throw new ForbiddenError ( 'Node already existed.' ) ; } node = { ... input , company : { name } } ; } const newNode = await getMongoRepository ( Node ) . save ( new Node ( { ... node } ) ) ; return newNode ; } @ Query ( ) async nodesById ( @ Args ( '_id' ) _id ) { const nodes = await getMongoRepository ( Node ) . find ( { where : { parentId : _id , category : NodeCategory . POSITION } } ) ; if ( nodes . length < 1 ) { throw new ForbiddenError ( 'Nodes not found.' ) ; } return nodes ; } @ Mutation ( ) async updateNode ( @ Args ( '_id' ) _id , @ Args ( 'input' ) input ) { const node = await getMongoRepository ( Node ) . findOne ( { _id } ) ; if ( ! node ) { throw new ForbiddenError ( 'Node not found.' ) ; } const newNode = { ... node , ... input } ; const updatedNode = await getMongoRepository ( Node ) . save ( new Node ( { ... newNode } ) ) ; return updatedNode ; } }