Skip to content
Snippets Groups Projects
agent.resolver.ts 1.42 KiB
Newer Older
  • Learn to ignore specific revisions
  • Pascal Kosak's avatar
    Pascal Kosak committed
    import { Args, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
    import { Agent } from './models/agent.model';
    import { PrismaService } from '../prisma/prisma.service';
    import { GraphQLString } from 'graphql';
    import { NotFoundException } from '@nestjs/common';
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    
    @Resolver(() => Agent)
    export class AgentResolver {
    
    Pascal Kosak's avatar
    Pascal Kosak committed
        constructor(private prismaService: PrismaService) {}
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    
        @Query(() => [Agent])
        listAgents() {
            return this.prismaService.agent.findMany({
                include: {
                    unlocks: true,
                    entries: true,
                },
            });
        }
    
        @Query(() => Agent)
        async getAgent(
            @Args({ name: 'code', type: () => GraphQLString }) code: string,
        ) {
            const agent = await this.prismaService.agent.findFirst({
                where: { slug: code },
            });
    
    
    Pascal Kosak's avatar
    Pascal Kosak committed
            if (!agent) throw new NotFoundException();
    
    Pascal Kosak's avatar
    Pascal Kosak committed
    
            return agent;
        }
    
        @ResolveField('unlocks')
        async unlocks(@Parent() entry: Agent) {
            const result = await this.prismaService.agent.findFirst({
                where: { id: entry.id },
                select: { unlocks: true },
            });
    
            return result.unlocks;
        }
    
        @ResolveField('entries')
        async entries(@Parent() entry: Agent) {
            const result = await this.prismaService.agent.findFirst({
                where: { id: entry.id },
                select: { entries: true },
            });
    
            return result.entries;
        }
    }