Saeid Ostad
Saeid Ostad

Reputation: 727

How to send a file to response a graphql query? (send a csv file from server to client)

I wanna send a csv file in repose of graphql query. I am using Apollo server and Apollo client and Reactjs. (send a csv file from server to client)

can anybody give me an idea how can I implement it?

Upvotes: 11

Views: 7577

Answers (2)

eDriven_Levar
eDriven_Levar

Reputation: 396

YOU DO NOT HAVE MAKE AN EXPRESS (REST API) SERVER FOR THIS. There are a number of ways in which you achieve this.

  1. You can send and receive the file if you base64 encode it. On the client side to receive the file you will need to decode it from your JSON response. Then create an object to attach the object to in order to download it.

  2. You can generate the csv server side and pass back a URI to the file. Often this is a great use case for S3 or some other object storage.

  3. You can convert the JSON formatted data into CSV client side. Here's an article on the concept. https://medium.com/@danny.pule/export-json-to-csv-file-using-javascript-a0b7bc5b00d2

If you have a large dataset , I'd recommend option 2. If you don't have access to object storage then 3 would be best for download.

Upvotes: 2

Amir Mehrabi
Amir Mehrabi

Reputation: 95

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.

Upvotes: 1

Related Questions