question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

0 bytes on multiple files when use nestjs inputType

See original GitHub issue

When I load images with a resolver in GraphQL using a inputType class in NestJS, I get 0-byte files when i upload to s3. However, if I add everything as args, the files arrive correctly, I don’t see too many differences, what could be the problem?

mutation:

@Mutation(() => Course)
async addResources(
  @Args({ name: 'input', type: () => CreateResources }) input: CreateResources,
  @CurrentUser() user: User
) {
  const type = EXTRARESOURCES;
  const result = await this.courseService.addExtraResources(
    user,
    input,
    type
  );
  if (!result) errorInvalidInput(type);
  return result;
}

inputType

@InputType()
export class CreateResources {
  @IsOptional()
  @Field(() => [CreateResourceLinkInput], { nullable: true })
  links: CreateResourceLinkInput[];

  @IsOptional()
  @Field(() => [CreateResourceVideoInput], { nullable: true })
  videos: CreateResourceVideoInput[];

  @IsOptional()
  @Field(() => [GraphQLUpload], { nullable: true })
  documents: Promise<[FileUpload]>;

  @IsOptional()
  @Field(() => [GraphQLUpload], { nullable: true })
  images: Promise<[FileUpload]>;
}

All as args

@Mutation(() => Course)
async AddResources(
  @Args({ name: 'links', type: () => [CreateResourceLinkInput], nullable: true }) links: [CreateResourceLinkInput],
  @Args({ name: 'videos', type: () => [CreateResourceVideoInput], nullable: true }) videos: [CreateResourceVideoInput],
  @Args({ name: 'documents', type: () => [GraphQLUpload], nullable: true }) documents: [FileUpload],
  @Args({ name: 'images', type: () => [GraphQLUpload], nullable: true }) images: [FileUpload],
  @CurrentUser() user: User
) {
  const type =EXTRARESOURCES;
  const input = {
    links,
    videos,
    documents,
    images
  };
  const result = await this.courseService.addExtraResources(
    user,
    input,
    type
  );
  if (!result) errorInvalidInput(type);
  return result;
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
SergioSuarezDevcommented, Nov 5, 2021

Hi guys.

After reading and reading and testing and testing. The problem comes from the “class-validator” module that is used in Nestjs because the validator internally tries to transform the object (since the file instance is an object) and fails. By adding the @Exclude decorator to the GraphQLUpload field, the files are received correctly.

Like this:

@InputType('CreateResources')
export class CreateResources {
  @Exclude()
  @IsOptional()
  @Field(() => [GraphQLUpload], { nullable: true })
  documents?: Promise<FileUpload>[];

  @Exclude()
  @IsOptional()
  @Field(() => [GraphQLUpload], { nullable: true })
  images?: Promise<FileUpload>[];

  @IsOptional()
  @IsNumber()
  @Field(() => Int, { nullable: true })
  order: number;
}
1reaction
jaydensericcommented, Nov 5, 2021

Closing because this issue doesn’t appear to be actionable on the graphql-upload side, but feel free to keep the conversation going. I’m happy to answer any questions you may have about graphql-upload, but I can’t be of much help with NestJS stuff since I’ve never used it in a project before. Maybe you’ll figure it out, or someone here can offer more advice, but the NestJS community might be a better place to ask about these problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why <input type="file"> return 0 bytes after input.files overwrite?
Why I'm receiving 0 bytes on server side? How it should be handled? Is there a way to manage this with multiple files...
Read more >
defaultValue makes property optional and it can be overridden ...
Current behavior. I am using the code first approach. TLDR: in the @Field() decorator, the defaultValue option always makes the property nullable ...
Read more >
Validation | NestJS - A progressive Node.js framework
It is best practice to validate the correctness of any data sent into a web application. To automatically validate incoming requests, Nest provides...
Read more >
Handling File Uploads in Ionic
Extension: Handling Multiple File Uploads · form action="/upload" method="post" enctype="multipart/form-data"> · <input type="file" name="photos[] ...
Read more >
How to build a file upload service with vanilla JavaScript
For our use case, we will be using the readAsArrayBuffer method to read the file in bytes and stream it to the backend...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found