-
-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation issues on version 2.0.0-beta.1 with @[email protected] #1397
Comments
First problemFrom class-validator v0.14.0 CHANGELOG:
What we are interested is the first point: When attempting to validate a class instance without metadata for it const schema = await buildSchema({
// ...
validate: { forbidUnknownValues: false } // <--
}); This will solve the issue. Second problemError handling has changed in both GraphQL > 16 and Apollo Server. For more information see Apollo Server v4 errors. const apolloServer = new ApolloServer<MyContext>({
schema,
formatError: myFormatError // <--
}); The import type { GraphQLFormattedError } from 'graphql';
import { ArgumentValidationError } from 'type-graphql';
import { unwrapResolverError } from '@apollo/server/errors';
import { ValidationError } from './ValidationError ';
function myFormatError(
formattedError: GraphQLFormattedError,
error: unknown
): GraphQLFormattedError {
const originalError = unwrapResolverError(error);
// Validation
if (originalError instanceof ArgumentValidationError) {
return new ValidationError(originalError.validationErrors);
}
// Generic
return formattedError;
}
import { GraphQLError } from 'graphql';
import type { ValidationError as ClassValidatorValidationError } from 'class-validator';
type IValidationError = Pick<
ClassValidatorValidationError,
'property' | 'value' | 'constraints' | 'children'
>;
function formatValidationErrors(
validationError: IValidationError
): IValidationError {
return {
property: validationError.property,
...(validationError.value && { value: validationError.value }),
...(validationError.constraints && {
constraints: validationError.constraints
}),
...(validationError.children &&
validationError.children.length !== 0 && {
children: validationError.children.map((child) =>
formatValidationErrors(child)
)
})
};
}
export class ValidationError extends GraphQLError {
public constructor(validationErrors: ClassValidatorValidationError[]) {
super('Validation Error', {
extensions: {
code: 'BAD_USER_INPUT',
validationErrors: validationErrors.map((validationError) =>
formatValidationErrors(validationError)
)
}
});
Object.setPrototypeOf(this, ValidationError.prototype);
}
}
|
@carlocorradini Thanks a lot for such detailed answer! I've already found these workarounds in your repository and it seems to work great For the first problem as I understand there is nothing can be done in the type-graphql package, but I would like to have this documented, it's not obvious and will make someone to spend quite a lot of time to figure out what's wrong and how to make it work. Especially together with the second problem For the second problem, I'm just wondering, is it possible to have such behaviour in the ArgumentValidationError class itself as it's part of type-graphql? |
|
@YMSpektor Can we close this? |
@carlocorradini I think about creating a PR with the following changes:
I think this small change allows to avoid this wierd issue with the class-validator v14, which is the current version of this package. But I haven't tested this change yet.
In my opinion these two issues are bugs, because they don't work as described in the documentation with the current versons of class-validator and apollo server and can be fixed in the source code (Apollo server v3 is now deprecated). I don't agree with tagging this issue as a question. Unfortunatelly I have no much time to work on such PR for this moment, so if possible I would like to have this issue open to be able to work on PR when I have more time (if you agree with my opinion and with the proposed code changes) What do you think, @carlocorradini @MichalLytek ? |
@MichalLytekDo you want me to take care of updating all errors? |
I got these issues using type-graphql 2.0.0-beta.1 |
It's still a beta :) |
@carlocorradini thanks again! I understand that it's a beta, and I don't have any complaints :) Just wanted to said that it seems not to be reflected in V2 for this moment and I wanted to draw your attention to it. TypeGraphQL is my absolute favorite GraphQL library, I used it in my recent projects (and plan to use in future) and it worked great! |
@YMSpektor Awesome 🥳 |
Hey @YMSpektor |
This happened to me. I had to add a validation on the input to get around it. |
Yep, it's in the changelog: |
As #1331 is merged, the For using with other older version, we have a workaround for providing that option manually via So nothing more to add here, closing this issue 🔒 |
Just a note: I had the same issue, and forbidUnknownValues=false fixed it for me. |
Any update on beta-2 @MichalLytek ? Even if it has only this fix in it, it would be worth it :) |
Describe the Bug
I'm using type-graphql v2.0.0-beta.1 together with @[email protected]. (As mentioned here type-graphql v1.1.1 doesn't work fine with apollo-server v4) There are some issues related to validation:
I'm not able to call a mutation passing correct variables in the body, it leads to Argument Validation Error in the response. The issue seems to be related to "forbidUnknownValues" option in the class-validator library, at least setting it to false allows to avoid the error.
The"validationErrors" field is always missing in the ArgumentValidationError response (even if I set forbidUnknownValues to false, add some validator like MaxLength and pass incorrect value - "validationErrors" will be missing in the response JSON)
To Reproduce
Here is the code:
Here is my package.json:
And this is the Mutation I'm trying to call:
Variables:
The error response I get:
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: