Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL endpoint for file download

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!

like image 652
jasonmerino Avatar asked Oct 15 '18 23:10

jasonmerino


People also ask

Can GraphQL download file?

the Graphql responses JSON format only if you like to download files in the front end you can implement another app like express to upload and download files from/to it.

How do you find the endpoint in GraphQL?

Your GraphQL endpoint can be found on the dashboard. It will look something like: https://example.us-west-2.aws.cloud.dgraph.io/cloud . If you open up the docs tab on the right side, you'll see that GraphQL playground has pulled all the possible resolvers that our server supports.

Can GraphQL serve files?

Our GraphQL server still provides a mutation for uploading files, but the mutation is only used to setup a file upload. The actual file upload is done through a dedicated endpoint. We can accomplish this by returning presigned upload URLs from our mutations.

Does GraphQL have endpoints?

GraphQL is typically served over HTTP via a single endpoint which expresses the full set of capabilities of the service. This is in contrast to REST APIs which expose a suite of URLs each of which expose a single resource.


1 Answers

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).

like image 69
Sandeep Kumar Avatar answered Oct 19 '22 17:10

Sandeep Kumar