Skip to content

Conversation

@doylefermi-kv
Copy link
Collaborator

  1. Using the Logger -
...
import { LoggerService } from 'src/logger/logger.service';

@Resolver('User')
export class UserResolver {
  private readonly logger: LoggerService = LoggerService.getInstance(
    UserResolver.name,
  );

  constructor(private userService: UserService) {}

  @Query()
  getUsers(): Promise<User[]> {
    this.logger.info('Starting getUsers()');
    return this.userService.getAllUsers();
  }
...
  1. Added validation pipe.

@doylefermi-kv doylefermi-kv requested a review from jinilcs May 18, 2021 08:32
export const UserSchema = Joi.object({
email: Joi.string().email({ tlds: { allow: false } }),
firstName: Joi.string(),
middleName: Joi.string(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it throw error if middleName is optional and not passed in request?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, MiddleName will remain as an optional field. No error will be thrown. However, this will remain as an additional validation over the existing graphql validation that MiddleName is a string.

transform(value: any, metadata: ArgumentMetadata): any {
const { error } = this.schema.validate(value);
if (error) {
throw new BadRequestException('Validation failed', error.message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be handled in exception filter and sent the custom error response?
Also if validation fails for multiple fields, will it send all error messages in single response?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jinilcs , yes the validation thrown will be handled by the custom error response. Also, all the validations will be collated together -

Example:

{
  "errors": [
    {
      "message": "Failed to validate the input payload",
      "path": [
        "createUser"
      ],
      "extensions": {
        "statusCode": 400,
        "error": "\"email\" must be a valid email. \"firstName\" is not allowed to be empty"
      }
    }
  ],
  "data": {
    "createUser": null
  }
}```

@jinilcs jinilcs requested review from johnspaulka and vinayak-keyvalue and removed request for johnspaulka and vinayak-keyvalue May 19, 2021 06:09
@doylefermi-kv doylefermi-kv requested review from johnspaulka and vinayak-keyvalue and removed request for johnspaulka and vinayak-keyvalue May 19, 2021 06:15
- All validations are returned in a single response

- Handled by the global exception filter

- Reword the validation message
@jinilcs jinilcs merged commit 928756e into jinil/boiler-plate May 22, 2021
@jinilcs jinilcs deleted the doyle/boiler-plate branch May 22, 2021 16:30
doylefermi-kv pushed a commit that referenced this pull request Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants