graphql icon indicating copy to clipboard operation
graphql copied to clipboard

File upload

Open asoseil opened this issue 9 years ago • 15 comments

is there a way to upload files?

asoseil avatar Jun 24 '16 10:06 asoseil

Hi Alessio,

Have you by any chance figured out how to upload files?

Thanks and best,

Jens

schumajs avatar Dec 08 '16 22:12 schumajs

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.

narqo avatar Dec 09 '16 08:12 narqo

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.

bbuck avatar Dec 09 '16 17:12 bbuck

Thanks for the answers. I followed the approach discussed here. Works pretty well.

schumajs avatar Dec 09 '16 17:12 schumajs

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.

saveliy-kremen avatar May 30 '18 14:05 saveliy-kremen

can you post your upload code for sharing? because i have got the same problem @schumajs

jianjungki avatar Jun 09 '18 02:06 jianjungki

There is a specification for handling multipart in graphql: https://github.com/jaydenseric/graphql-multipart-request-spec

glaslos avatar Aug 02 '18 13:08 glaslos

@glaslos cool

chienfuchen32 avatar Aug 02 '18 15:08 chienfuchen32

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

niondir avatar Sep 11 '19 13:09 niondir

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

koblas avatar Sep 16 '19 14:09 koblas

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.

exapsy avatar Aug 23 '20 09:08 exapsy

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

koblas avatar Aug 24 '20 19:08 koblas

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.

bbuck avatar Aug 26 '20 17:08 bbuck

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.

koblas avatar Aug 26 '20 20:08 koblas

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.

bbuck avatar Aug 27 '20 19:08 bbuck