prisma-nestjs-graphql
prisma-nestjs-graphql copied to clipboard
Input is empty when using generated InputType
Its a bit confusing to me but when using the generated input types im not able to get args from my graphql mutations. When using my own dto as described in the nestjs docs everything works as expected.
My resolver:
@Mutation(() => Business, { nullable: true })
async createBusiness(
@UserEntity() user: User,
@Args('business') business: BusinessCreateInput,
) {
console.log(business); // output: BusinessCreateInput {}
return this.businessesService.create(business, user);
}
My graphql mutation
mutation CreateBusiness{
createBusiness(business: { name: "Test business" })
}
Any ideas why this happens when using the generated types?
What is your prisma schema, what version of nestjs and library are you using?
Prisma schema:
model Business {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
address Address? @relation("Address")
billingAddress Address? @relation("BillingAddress")
users User[]
name String
legalName String
moderationStatus ModerationStatus @default(PENDING)
vatId String?
legalForm String?
stripeId String?
websiteUrl String?
addressId String?
billingAddressId String?
}
Versions
nestjs/core: 8.0.6
prisma-nestjs-graphql: ^14.6.0
Generated InputType
@InputType()
export class BusinessCreateInput {
@Field(() => String, {nullable:true})
id?: string;
@Field(() => Date, {nullable:true})
createdAt?: Date | string;
@Field(() => Date, {nullable:true})
updatedAt?: Date | string;
@Field(() => String, {nullable:false})
name!: string;
@Field(() => String, {nullable:false})
legalName!: string;
@Field(() => ModerationStatus, {nullable:true})
moderationStatus?: keyof typeof ModerationStatus;
@Field(() => String, {nullable:true})
vatId?: string;
@Field(() => String, {nullable:true})
legalForm?: string;
@Field(() => String, {nullable:true})
stripeId?: string;
@Field(() => String, {nullable:true})
websiteUrl?: string;
@Field(() => String, {nullable:true})
addressId?: string;
@Field(() => String, {nullable:true})
billingAddressId?: string;
@Field(() => AddressCreateNestedOneWithoutBusinessInput, {nullable:true})
address?: AddressCreateNestedOneWithoutBusinessInput;
@Field(() => AddressCreateNestedOneWithoutBillingAddressFromInput, {nullable:true})
billingAddress?: AddressCreateNestedOneWithoutBillingAddressFromInput;
@Field(() => UserCreateNestedManyWithoutBusinessInput, {nullable:true})
users?: UserCreateNestedManyWithoutBusinessInput;
}
Looks normal and it works for me. Are you using standard set of tools like tsc compiler, reflect-metadata, etc? Or non-standard, like swc/esbuild, lite reflection lib?
Yes im using the standard set wich comes with nest out of the box
Sorry, but I dont have any ideas why it's not working. Can you provide minimal repository to reproduce error?
Ok I managed to debug the cause of this problem: I was using the ValidationPipe globally like this:
// main.ts
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
}),
);
The request was working with my usual dto's because im using class-validator on every field. None of the generated types has class-validators so none of the fields was able to go through. Deactivating the validation pipe fixes this problem for now.
Is there a way to use the generated types together with a global validation pipe? Im still using some manually created dtos with class-validation and it would be cool if I could still use them :-)
I think problem in whitelist = true. But it does not make sense to use whitelist = true with graphql, graphql is typed.
I think you have some options here:
- Disable whitelist, and use ValidationPipe in specific methods (decorator
@UsePipes) or parameters where its needed (@Param,@Body) - You can try to attach
@Allowdecorator to all fields (from class-validator, see https://github.com/typestack/class-validator#whitelisting) to all generated graphql classes, seedecorateconfig option - https://github.com/unlight/prisma-nestjs-graphql#decorate