3 Answers
There’s a great blog post by Apollo on Apollo file uploads here, essentially they recommend using a 3rd party image service or pre-signed upload urls (e.g. fetch a pre-signed url via graphql, upload the image directly to that url, then save the location / id of the image through a graphql mutation).
There is a standard for uploading images through a graphql request, although Apollo don’t recommend it, but the most popular Node.js package to help with this is graphql-upload.
To get this working with Apollo 4 and Express (if this is what you’re using), you’ll need to follow their instructions for using express middleware here.
Note that Apollo will block "simple" CORS requests to prevent CSRF attacks, which is likely to affect your image upload, so you need to follow the instructions here to include additional headers in your request or disable CSRF protection (not recommended). You can see an example of this by excluding the Apollo-Require-Preflight
header from the CURL request below.
Here’s a minimal example of graphql upload in codesandbox.
And a CURL request to test it
$ curl https://2nnbw4-4000.csb.app/
-F operations='{ "query": "mutation ($id: String!, $image: Upload!) { uploadImage(id: $id, image: $image) { id filename } }", "variables": { "image": null, "id": "1" } }'
-F map='{ "0": ["variables.image"] }'
-F [email protected]
-H 'Apollo-Require-Preflight: true'
{"data":{"uploadImage":{"id":"1","filename":"tmp.jpg"}}}
1
-
Sharing @phdesign's blog post about how he came up with this solution, too. Thanks! phdesign.com.au/programming/…
– Doug AyersOct 3 at 21:16
In the response section of the Upload request to Cloudinary, you will receive the URL in the key: url
.
Sample response: https://cloudinary.com/documentation/image_upload_api_reference#upload_response
You would be able to save this to your database through GraphQL. I’m not sure where you’re stuck ( is the Apollo server connected to the DB? ) but I would advise looking up some guides on how to store data utilizing Apollo/GraphQL.
https://www.apollographql.com/tutorials/fullstack-quickstart/connecting-to-data-sources
https://hasura.io/learn/graphql/intro-graphql/graphql-mutations/
3
-
Thank you for your response, another problem I’m having is I can’t add Upload scalar to the newer Apollo/server v4 GraphQL schema… so there is no way to to upload the file for mutation
– user17371035Oct 26, 2022 at 22:41
-
Maybe I don’t ask the right question. I’m unable to create a schema for upload on the newer Apollo server version
– user17371035Oct 26, 2022 at 22:48
-
In the original question it sounds like you're using Cloudinary as an image upload service, so usually you would 1. Upload your image to Cloudinary and receive an id / location in the response, then 2. Make GraphQL request to your server to save the id / location. In your comment above you're asking about uploading files in GraphQL so I've added another answer on how to do that.
– phdesignMar 18 at 4:28
did you figure this one out?
Mar 15 at 8:04