Skip to content
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

Drivine @Transactional() decorator breaks @UsePipes(new ValidationPipe) #49

Open
myflowpl opened this issue Jun 10, 2020 · 3 comments
Open
Labels

Comments

@myflowpl
Copy link
Collaborator

Drivine @transactional() decorator breaks Nest ValidationPipe if used with @UsePies()

import { Controller, Post, Body, ValidationPipe, UsePipes } from '@nestjs/common';
import { Transactional } from '@liberation-data/drivine';
import { SpotSaveRequestDto, SpotSaveDto } from '@slackmap/api/spot/dto';
import { SpotRepository } from '@slackmap/api/spot/data';

@Controller()
export class SpotSaveController {

  constructor(private spotRepository: SpotRepository) {}

  @Post('spot/save')
  @Transactional() // <<< if you comment this out, validation will work
  @UsePipes(new ValidationPipe())
  async process(@Body() data: SpotSaveRequestDto): Promise<SpotSaveDto> {

    const spot = await this.spotRepository.create(data.spot)

    return {spot}
  }
}

It will work if you use it on the method param like:

  async process(@Body(new ValidationPipe()) data: SpotSaveRequestDto): Promise<SpotSaveDto> {

    const spot = await this.spotRepository.create(data.spot)

    return {spot}
  }
@jasperblues
Copy link
Member

Workaround: Change the order that the decorators are applied.

@jasperblues jasperblues added bug Something isn't working has-work-around labels Jun 10, 2020
@NilsMoller
Copy link

NilsMoller commented May 25, 2022

The order of the @Transactional decorator is important in other ways too it seems.
Using Nest's default GraphQL implementation, the following breaks:

// Transactional first, Query second
@Transactional()
@Query() 
async someQuery(): Promise<SomeModel> {
  // ...
}

However this works fine:

// Query first, Transactional second
@Query() 
@Transactional() 
async someQuery(): Promise<SomeModel> {
  // ...
}

@jasperblues
Copy link
Member

@NilsMoller If you have time and able to submit some usage patterns you have with GraphQL, I would like to provide some guidance for that in the docs.

Meanwhile, for the txn decorator. One solution would be to more tightly integrate with Nest. Originally I created this decorator to not use anything from Nest, so it would be easy to port Drivine to use with eg AWS Lambda. But didn't get time to take that forward. Eg Nest's way would use reflect-metadata, and their interceptor hooks.

Or another solution is just trace why order is important, and workaround what's happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants