简体   繁体   中英

Graphql Nestjs add arguments to type field

Hello how are you doing, I have been learning Nestjs for the past week and enjoy it a lot. I am currently using it with Prisma 2 to build a graphql server. I want to add arguments in a graphql type field to be able to filter or order the result using prisma. to be clear, I want to be able to execute the following schema

query {
  findUniqueUser(where: { id: 1 }) {
    id
    email
    name
    posts(where: { title: { contains: "a" } }, orderBy: { createdAt: asc }, first: 10, skip: 5) {
      id
      title
      comments(where: { contain: { contains: "a" } }) {
        id
        contain
      }
    }
  }
}

I am using the library @paljs/plugins to extract information from the GraphQLResolveInfo and make the query to prisma. I am also using the library prisma-nestjs-graphql to automatically generate all graphql type definitions. Has anyone ever attempted that using nestjs? Thank you and have a great day

Edit: I am using code-first graphql

Edit 2: After digging around I found a way using @ResolveField .

@Query((returns) => Patient, { nullable: true, name: "patient" })
    async getPatient(@Args() params: FindUniquePatientArgs, @Info() info: GraphQLResolveInfo) {
        const select = new PrismaSelect(info).value;
        params = { ...params, ...select };
        return this.prismaService.patient.findUnique(params);
    }

    @ResolveField(() => File)
    async files(@Parent() patient: Patient, @Args() params: FindManyFileArgs) {
        return patient.files;
    }

I don't know if this is the best way of doing it so I leave the question unanswered for now.

I will leave that here as the answer

    @Query((returns) => Patient, { nullable: true, name: "patient" })
    async getPatient(@Args() params: FindUniquePatientArgs, @Info() info: GraphQLResolveInfo) {
        const select = new PrismaSelect(info).value;
        params = { ...params, ...select };
        return this.prismaService.patient.findUnique(params);
    }

    @ResolveField(() => File)
    async files(@Parent() patient: Patient, @Args() params: FindManyFileArgs) {
        return patient.files;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM