f / graphql.js

A Simple and Isomorphic GraphQL Client for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle file uploads over GraphQL

f opened this issue · comments

Add file upload handling.

commented

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

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.

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.