graphql.js icon indicating copy to clipboard operation
graphql.js copied to clipboard

Handle file uploads over GraphQL

Open f opened this issue 8 years ago • 3 comments

Add file upload handling.

f avatar Feb 21 '17 13:02 f

Is there a workaround for this? I'd like to send a file at the same time I'm sending data.

mraak avatar Jul 21 '17 14:07 mraak

Most likely the way to proceed would be to use the FormData API in the internal __request method. This would take care of the content type headers, so you would need to modify the internals to not use x-www-form-urlencoded as the content-type. In fact, with FormData, you're better off not setting content-type at all.

Once that's done, you'd need to work out a protocol in your code for adding files and referring to them in the variables of a mutation. One possibility is having the input fields in the mutation input refer to the file input, such that each file input would have the same key in the form input that you used as a value in the mutation input field. Then the server would need to know to decode this and match up the values; that's a matter for your server-side resolvers to figure out.

That's how I'm trying to proceed, anyway.

flewellyn avatar Dec 03 '18 20:12 flewellyn

In my projects im converting image to Base64 format and im sending it in normal mutation query. But in this approach you should change request limit on your server. And of course you need to convert Base64 format to file on server side. Okay this approach is difficult but its flexible. You can send multiple images and other datas in one request.

kemalturk avatar Jul 07 '19 11:07 kemalturk