DEV Community

Discussion on: Handling File Uploads with Apollo Server 2.0.

Collapse
 
dnature profile image
Divine Hycenth

Hi, Please provide a link to your code?

Collapse
 
receiver412 profile image
Receiver412

actually the version of node js (v14) that i have does not support that .but i switch it to version 12. now it works.

Thread Thread
 
dnature profile image
Divine Hycenth

Great!, If it works, don't touch it šŸ˜†

Thread Thread
 
receiver412 profile image
Receiver412

can you make example about multi file Upload .
I appreciate it .

Thread Thread
 
dnature profile image
Divine Hycenth • Edited

Hi, here's how I would go about it.

1: Modify the uploadFile mutation to accept an array of files

Mutation {
  uploadFile(files: [Upload!]!): [File!]!
}
Enter fullscreen mode Exit fullscreen mode

2: replace your upload file resolver with this:

uploadFile: async (_, { files }) => {
      mkdir("images", { recursive: true }, (err) => {
        if (err) throw err;
      });
      return files.map(async (file) => {
            const upload = await processUpload(file)
            await File.create(upload)
            return upload           
      })
 },
Enter fullscreen mode Exit fullscreen mode

Note: I haven't tested this out but I hope it works.

Thread Thread
 
receiver412 profile image
Receiver412

thanks a lot bro. it works.
have a nice day .

Thread Thread
 
dnature profile image
Divine Hycenth

I'm glad it worked :)
Enjoy your day!