How to send binary data to a client using GraphQL

I have a GraphQL server hosted on express. I want to return images to the client by sending back nodejs buffer objects. How can I configure graphql server to return bytes instead of json? I do not want to do this through base64, as the image is large.

+4
source share
1 answer

You should return JSON, but there is still a way. We use GraphQL to return images stored in Blob fields in the old sql database. We use sequelize, graphql-sequelize and graphql-js. We have determined that Blob fields are String types in our graphql schema, and therefore they are great for json's answer. Then we convert to buffer before delivery, for example

const imgBuffer = new Buffer.from(imgObj.data, 'ascii');

The only problem is that now we are having problems saving the image data back to the database via the graphql interface. Our mutation function gives us a syntax error when it detects some invalid Unicode characters in strings, such as \ U0000 and whatnot (so I found your question looking for a solution to this).

+1
source

Source: https://habr.com/ru/post/1675050/


All Articles