Reputation: 203
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.
Upvotes: 1
Views: 3835
Reputation: 203
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;
}
Upvotes: 4