File upload
is there a way to upload files?
Hi Alessio,
Have you by any chance figured out how to upload files?
Thanks and best,
Jens
Why not ask the question on some of GraphQL Community Resources? I don't think there might be any technical issues related to Go implementation. You only need to figure out how the pattern applied GraphQL in general.
An option here is to provide a standard endpoint for file uploads and route the user through it. With some iframe trickery (unless things have changed since I've done this) you can do "background" file uploads. Your file upload route is just a standard non-GraphQL endpoint and would store the file in some temporary (safe) location. It returns the filename. Then you submit your GraphQL request with the filename and the handler can access the file in the temporary location (or S3 or wherever it is).
edit
A secondary option might be to use middleware and have a handler situated in front of your GraphQL handler to process the multipart portion of the file and store it, making it accessible via context or any number of other means and then passing forward to the next handler in line, the GraphQL handler.
Thanks for the answers. I followed the approach discussed here. Works pretty well.
GraphQL multipart request specification allows you to nest files anywhere within GraphQL mutations like this:
{
query: mutation($file: Upload!) { uploadFile(file: $file) { id } },
variables: {
file: File // somefile.jpg
}
}
It would be nice to add a type to graphql *multipart.FileHeader to use it.
can you post your upload code for sharing? because i have got the same problem @schumajs
There is a specification for handling multipart in graphql: https://github.com/jaydenseric/graphql-multipart-request-spec
@glaslos cool
I'm also looking for file uploads.
Is there some working solution / example out there?
I will start with trying: https://github.com/jpascal/graphql-upload
FYI - this PR has been around for a long time to add support at the handler level
https://github.com/graphql-go/handler/pull/43
Any updates on this? Seems like its been literally a year ago since last post :P And there seems to be a PR hanging there for a year, which could potentially solve the problem altogether.
I'm happy updating the PR, but given that it's been 2 years of silence not racing to rebase until I have some indication that it'll be merged...
I don't think that the GraphQL spec has any details about handling multi-part/file uploads. There is no reason you cannot have non-GraphQL endpoints along with GraphQL endpoints, like for uploading files.
In past projects, I've typically uploaded files directly to S3 and then passed the URL of said files down in the request to the server. This is great because then I don't have to concern my app with passing files through to S3 and also don't need to handle file uploads directly.
I can't find a reference to the spec that details handling file uploads so I don't see any reason to go off-spec for this spec-compliant implementation.
While I've not read the details, the Apollo team wrote up this in May 2020.
https://www.apollographql.com/blog/apollo-server-file-upload-best-practices-1e7f24cdc050/
Where the apollo-upload-client package is still a recommended option.
Referring to the official GraphQL spec: https://spec.graphql.org/
I still think files should be handled outside of GraphQL endpoints.
edit: I do not speak for this project's owners.