unlight/prisma-nestjs-graphql

[Question] Unable to apply filters on _count using ResolveField in Prisma with NestJS-GraphQL

florianrusso opened this issue · 0 comments

Description

I have User and Message entity. A user can have many messages.
I want to apply a filter on count to only count draft messages.

Gql query

When trying to retrieve a user message with a draft status, I applied a filter using @ResolveField

query User {
    user(where: {id: "<id>"}) {
        id
        _count {
            messages
        }
        messages(take: 1, where: {status: {equals: "draft"}}) {
            _count {
                answers
            }
            content
            answers{
                content
            }
        }
    }
}

Resolve field

I successfully implemented the ResolveField for messages to cater to multi-level queries:

// in the user resolver
  @ResolveField(() => [Message], { name: 'messages' })
  public getMessageProperty(
    @Parent() user: User,
    @Args() _findManyMessageArgs?: FindManyMessageArgs, // event if not used in function need to be here to allow us to use params when selecting user.messages like take, skip, ...
  ) {
    return user.messages;
  }

However, I'm struggling to achieve the same for the _count field. I aim to apply filters to the count, since Prisma supports this feature.

  @ResolveField(() => UserCount, { name: '_count' })
  public getCountProperty(
    @Parent() user: User,
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
   @Args('where') countargs?: <TO_DO>,
  ) {
    return user._count;
  }
  

Problem:

I've experimented with different argument types, but none appear to be compatible with Prisma's requirements. Specifically, when I try using @Args() query: FindUniqueUserArgs, the selection derived from PrismaSelect isn't accepted by Prisma.

Question:

Is my approach correct, or is there another recommended way to achieve this with the prisma-nestjs-graphql integration?