12

Is it possible to trigger a file download in a browser from the GraphQL endpoint on an apollo-server-express application?

I have the endpoint written in a standard express app.get function (see below) but I would like to make use of the GraphQL context for file download and so I'm wondering if it's possible to cause a download from a GraphQL endpoint.

Here's a bare-bones example of what I have on the express end in the app.get function:

app.get('/download-batch/:batchId', async (req, res) => {
  res.send(new Buffer('test'));
});

Any help would me much appreciated. Thanks!

jasonmerino
  • 3,220
  • 1
  • 21
  • 38
  • 1
    Hello I also have the same requirement of downloading csv from graphql endpoint. did you get any solution yet ? – Ashish Panchal Jan 24 '19 at 03:20
  • @AshishPanchal I stuck with this route of doing things, through just the normal Express.js route, but I haven't been back to revisit it recently. – jasonmerino Jan 24 '19 at 19:36

2 Answers2

3

Yes, but you would be required to create a custom endpoint for that. You can't use the existing endpoint which you are using for making requests. Using the custom endpoint, you have to add a middleware and process the data into a buffer or whatever format you need. But it would not be recommended. That would again become one more endpoint instead which you can write an API to serve that.(After all graphql is built mainly on the focus of single endpoint).

Sandeep Kumar
  • 106
  • 1
  • 8
0

Boštjan Cigan mentions here some solutions and gives details about using GraphQL as a proxy with Minio. The backend would ask Mino to generate a temporary link that can be sent back to the browser for direct access.

This is a valid solution for many use cases.

tiomno
  • 2,178
  • 26
  • 31