Sending a downloadable file using absinthe graphql

I currently have a grahql API which sends out json as string. I now want to tweak this to be able send a downloadable json file in the API response. My schema currently currently looks like this:

object :export_data do
    field :resp_data, :json

My initial idea was to change the resp_data type of a file data type and then in the resolver function return a file (using File.write/2). Something like this:

object :export_data do
    field :resp_data, :file

But I couldn’t find a data type in the absinthe documentation that supports a file. How do I send a downloadable file in a graphql API response?

You would have to build a custom scalar on both sides or base64 encode.

A much easier solution is to make a short lived Phoenix.Token or JTW that allows for file download (like a url with the token on it, similar to how signed urls work in s3). Pass that in the response and then use js to instigate a download.

Hi @olivermt ! Thank you for the response.

That’s correct that’s too much of work for a simple task. Should the API just send the json as a string and let the frontend (react in this case) handle it? Is it possible for frontend to convert string to a file download?