fernando jr Singson
fernando jr Singson

Reputation: 61

How to handle file uploads in GraphQL over Websocket Protocol?

I'm creating a web app that uses GraphQL, the requirement is to handle GraphQL operations over WebSocket. Although I managed to achieve this by using subscriptions-transport-ws. however, I'm quite stuck with handling file uploads. and somehow I came across with streaming file from client to server using socket.io-stream, but this leads to having 2 separate API for textual data and files. So, I was wondering if there is a way to combine this functionality into GraphQL.

Upvotes: 1

Views: 433

Answers (2)

Matt Winn
Matt Winn

Reputation: 51

I ran into the exact same problem, and my file sizes were such that converting to base64 wasn't a feasible option. I also didn't want to use a separate library outside of GraphQL because that would require substantial setup changes in my server (to handle both GraphQL and non-GraphQL).

Fortunately, the solution ended up being fairly simple. I created two GraphQL clients on the front-end - one for the majority of my traffic exclusively over WebSockets, and another exclusively over HTTP just for operations that involved file uploads.

Now I can simply specify which client I want depending on whether the operation involves file uploads, without complex changes on my server or impacting the real-time benefits of all of my other queries.

Upvotes: 2

Anirudh Singh
Anirudh Singh

Reputation: 17

currently, I am also finding a proper solution for this but you can use an alternative approach by converting your file data into base64 and pass that data as a string. This approach can only work for a small size file as string data-type can not store large amounts of data as buffers do.

Upvotes: 0

Related Questions