'Upload in GraphQL with NestJs and ValidationPipe

Im trying to make file upload in my api using this strategy: https://stephen-knutter.github.io/2020-02-07-nestjs-graphql-file-upload/.

Without the ValidationPipe works, but when i enable ValidationPipe this apresent error on class-transformer:

TypeError: Promise resolver undefined is not a function
    at new Promise (<anonymous>)
    at TransformOperationExecutor.transform (/Users/victorassis/Workspace/barreiroclub/api/node_modules/class-transformer/TransformOperationExecutor.js:117:32)
    at _loop_1 (/Users/victorassis/Workspace/barreiroclub/api/node_modules/class-transformer/TransformOperationExecutor.js:235:45)
    at TransformOperationExecutor.transform (/Users/victorassis/Workspace/barreiroclub/api/node_modules/class-transformer/TransformOperationExecutor.js:260:17)
    at ClassTransformer.plainToClass (/Users/victorassis/Workspace/barreiroclub/api/node_modules/class-transformer/ClassTransformer.js:17:25)
    at Object.plainToClass (/Users/victorassis/Workspace/barreiroclub/api/node_modules/class-transformer/index.js:20:29)
    at ValidationPipe.transform (/Users/victorassis/Workspace/barreiroclub/api/node_modules/@nestjs/common/pipes/validation.pipe.js:40:39)
    at /Users/victorassis/Workspace/barreiroclub/api/node_modules/@nestjs/core/pipes/pipes-consumer.js:15:33
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

I searched a lot, but seens like class-transformer is abandoned, and the answers was to not use ValidationPipe with upload.

Someone pass for this and found a solution?



Solution 1:[1]

I try to follow the example you posted above and then enable the transformation of class and I got no error as you mentioned. But I met this error before when I was trying to put the wrong type of argument in the resolver. Below is where I setup my app bootstrap:

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.enableCors({
    origin: extractOrigins(app.get(ConfigService).get('CORS_ORIGINS')),
  });
  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
    }),
  );
  app.use(graphqlUploadExpress());
  await app.listen(app.get(ConfigService).get('PORT') ?? 3000);

  logScaffoldApp(app);
}
bootstrap();

The resolver code:

@Mutation(() => Boolean, { nullable: true })
  async uploadVocabularies(
    @Args({
      name: 'file',
      type: () => GraphQLUpload,
    })
    { createReadStream, filename }: FileUpload,
  ) {
    console.log('attachment:', filename);

    const stream = createReadStream();
    stream.on('data', (chunk: Buffer) => {
      console.log(chunk);
    });
  }

And I did get the error when I try to follow another tutorial and trying to make the argument as a Promise so then the class transformer got the same error:

@Mutation(() => Boolean, { nullable: true })
  async uploadVocabularies(
    @Args({
      name: 'file',
      type: () => GraphQLUpload,
    })
    attachment: Promise<FileUpload>,
  ) {
    const { filename, createReadStream } = await attachment;
    console.log('attachment:', filename);

    const stream = createReadStream();
    stream.on('data', (chunk: Buffer) => {
      console.log(chunk);
    });
  }

I hope this can help you and other people who viewed this post ^^

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ??ng PhĂș