apollo-upload-network-interface icon indicating copy to clipboard operation
apollo-upload-network-interface copied to clipboard

Files variable type

Open sandervanhooft opened this issue 8 years ago • 5 comments

@HriBB @thebigredgeek

What type should the files variable be when passed to the mutation?

The file upload works fine when using your example code. But when using react-dropzone or tcomb-form the files seem to be not handled appropriately.

sandervanhooft avatar Dec 04 '16 14:12 sandervanhooft

@sandervanhooft you have to send files as form data. https://github.com/okonet/react-dropzone doesn't appear to have any opinions as to how you send the data, they just provide hooks to receive the dropped files via callbacks

thebigredgeek avatar Dec 04 '16 21:12 thebigredgeek

Hi @thebigredgeek,

I have tried the following, but it didn't work:

// in component that uses react-dropzone:
onDrop: function (acceptedFiles, rejectedFiles) {
      console.log('Accepted files: ', acceptedFiles) // console.log: [File]
      const { onUpload, token, offerid } = this.props
      const fd = new FormData()
      acceptedFiles.map( file => fd.append('files', file, file.name) )
      onUpload(token, offerid, fd)
      console.log('Rejected files: ', rejectedFiles) // console.log: []
    },

The onUpload mutation is received by the server, but yields an empty object for the files variable. What am I missing?

Kind regards,

Sander

sandervanhooft avatar Dec 05 '16 16:12 sandervanhooft

@HriBB @thebigredgeek How about accepting an array of Files (array<File>) as input?

Given the MDN description there seems to be no way to programmatically create a FileList.

sandervanhooft avatar Dec 06 '16 21:12 sandervanhooft

@sandervanhooft take a look at this line. I am checking for the presence of FileList. There we need to add additional check for a single File or array or Files.

I am busy ATM, I have a presentation about React and GraphQL at the office in a few hours, hopefully after that I will have more time to fix these issues. You are welcome to make a PR for this ;)

HriBB avatar Dec 07 '16 08:12 HriBB

I'll try to look into it. Good luck with your presentation :)

sandervanhooft avatar Dec 07 '16 10:12 sandervanhooft