0 bytes on multiple files when use nestjs inputType
See original GitHub issueWhen 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:
- Created 2 years ago
- Reactions:1
- Comments:17 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
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 aboutgraphql-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.