I’ve been working on migrating an API from REST to GraphQL, and I've run into an issue with input validation.
Before migrating, I used class-validator to validate the input data, which worked flawlessly. But after moving to GragpQL, the data is being validated by GraphQL itself, so now I end up with these generic error messages that I don't like.
For example:
name should be a string
mutation {
createCustomer(input: {
name: 999
address: "addressaddress"
city: "citycity"
ice:"iceiceice"
country:"countrycountry"
}){
data{
name
}
}
}```
Instead of my validation error, I get a GraphQL error:
"String cannot represent a non string value: 999".
Now, my custom error messages aren't being shown at all.
I get why this is happening, but is there some way to show my errors again?
Edit: Forgot to add the Input class example
@InputType()
export class CreateCustomerInput {
@Field()
@IsNotEmpty()
@IsString()
name!: string;
@Field()
@IsNotEmpty()
@IsString()
address!: string;
@Field()
@IsNotEmpty()
@IsString()
city!: string;
@Field()
@IsNotEmpty()
@IsString()
ice!: string;
@Field()
@IsNotEmpty()
@IsString()
country!: string;
@Field({ nullable: true })
@IsOptional()
@IsString()
contact_name?: string;
@Field({ nullable: true })
@IsOptional()
@IsPhoneNumber('MA')
contact_phone?: string;
@Field({ nullable: true })
@IsOptional()
@IsEmail()
contact_email?: string;
}