import '@things-factory/auth-base' /* for Resolver Context */

import { Resolver, Query, FieldResolver, Root, Args, Arg, Ctx, Directive } from 'type-graphql'
import { getRepository } from '@things-factory/shell'
import { Analysis } from './analysis'
import { Process } from '../process/process'

@Resolver(Process)
export class ProcessQuery {
  @FieldResolver(type => Analysis)
  async analysis(@Root() process: Process): Promise<Analysis> {
    return await getRepository(Analysis).findOneBy({ process: { id: process.id } })
  }
}

@Resolver(Analysis)
export class AnalysisQuery {
  @Query(returns => Analysis!, { nullable: true, description: 'To fetch a Analysis' })
  async analysis(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<Analysis> {
    return await getRepository(Analysis).findOne({
      where: { id }
    })
  }
}
