import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
import { Types } from 'mongoose';
import { BuildParts } from '@/models/build-parts.model';
import { CreateBuildPartsInput } from './dto/create-build-part.input';
import graphqlFields from 'graphql-fields';
import type { GraphQLResolveInfo } from 'graphql';
import { BuildPartsService } from './build-parts.service';

@Resolver()
export class BuildPartsResolver {

    constructor(private readonly buildPartsService: BuildPartsService) { }

    @Query(() => [BuildParts], { name: 'BuildParts' })
    async getBuildParts(@Info() info: GraphQLResolveInfo): Promise<BuildParts[]> {
        const fields = graphqlFields(info); const projection = this.buildPartsService.buildProjection(fields); return this.buildPartsService.findAll(projection);
    }

    @Query(() => BuildParts, { name: 'BuildParts', nullable: true })
    async getBuildPart(@Args('id', { type: () => ID }) id: string, @Info() info: GraphQLResolveInfo): Promise<BuildParts | null> {
        const fields = graphqlFields(info); const projection = this.buildPartsService.buildProjection(fields); return this.buildPartsService.findById(new Types.ObjectId(id), projection);
    }

    @Mutation(() => BuildParts, { name: 'createBuildPart' })
    async createBuildPart(@Args('input') input: CreateBuildPartsInput): Promise<BuildParts> {
        return this.buildPartsService.create(input);
    }
}
