import { Resolver, Args, Mutation, Subscription, Query } from '@nestjs/graphql'; import { PipelineTask } from './pipeline-task.entity'; import { PipelineTasksService } from './pipeline-tasks.service'; import { CreatePipelineTaskInput } from './dtos/create-pipeline-task.input'; import { PipelineTaskLogArgs } from './dtos/pipeline-task-log.args'; import { plainToClass } from 'class-transformer'; import { PipelineTaskLogger } from './pipeline-task.logger'; import { observableToAsyncIterable } from '@graphql-tools/utils'; import { PipelineTaskEvent } from './models/pipeline-task-event'; @Resolver() export class PipelineTasksResolver { constructor( private readonly service: PipelineTasksService, private readonly taskLogger: PipelineTaskLogger, ) {} @Mutation(() => PipelineTask) async createPipelineTask(@Args('task') taskDto: CreatePipelineTaskInput) { return await this.service.addTask(taskDto); } @Subscription(() => PipelineTaskEvent, { resolve: (value) => { const data = plainToClass(PipelineTaskEvent, value); return data; }, }) async pipelineTaskEvent(@Args() args: PipelineTaskLogArgs) { const task = await this.service.findTaskById(args.taskId); return observableToAsyncIterable( this.taskLogger.getMessage$(task.id), ); } @Subscription(() => PipelineTask, { resolve: (value) => { return value; }, }) async pipelineTaskChanged(@Args('id') id: string) { // return await this.service.watchTaskUpdated(id); } @Query(() => [PipelineTask]) async listPipelineTaskByPipelineId(@Args('pipelineId') pipelineId: string) { return await this.service.listTasksByPipelineId(pipelineId); } @Query(() => PipelineTask) async pipelineTask(@Args('id') id: string) { return await this.service.findTaskById(id); } @Mutation(() => Boolean) async stopPipelineTask(@Args('id') id: string) { const task = await this.service.findTaskById(id); await this.service.stopTask(task); return true; } }