-
Notifications
You must be signed in to change notification settings - Fork 413
[Proposal] File Uploads #65
Description
After toying with the Express-GraphQL file upload demo, I have been thinking about how to bring file uploads inside GraphQL mutations to Apollo.
Here is the rundown of what I've learned and the best solution I have formulated:
-
Canonical file uploads happen via a
mutipart/form-dataPOST request, whereas Apollo Client interfaces with the Server exclusively withapplication/jsonrequests at present. -
There are other ways to transmit a file from browser to server, but they are often flimsy and poor solutions.
-
The ideal solution delivers both a GraphQL mutation and the uploaded file(s) to the server in such a way that the schema resolvers have access to the files, such that custom logic can test, modify, and/or store the uploads. The best way to ensure both mutation string and file(s) reach the server at the same time is to merge them into the same request.
-
Thus, I think that Apollo Client needs to either
- gain a new method, e.g.
mutateWithFiles( "gqlQueryString", { apolloOptions }, [ files ] )or - include a new option, e.g.
uploadFiles : [ ... ]
which, when used, sends a
multipart/form-dataPOST to the specified Apollo Server endpoint. The request would be identical to the normal request apart from the inclusion of a newfilesfield or similar which contains the uploading files. I think I would lean towards the clarity of a new method on account of readability. - gain a new method, e.g.
-
Apollo Server will then need to check each request for its
content-type; if it isapplication/json, then no upload-specific logic happens and the mutation continues as usual. If, however, the POST is ofcontent-typemultipart/form-data, the server will append the files in memory to the root query object. The mutation then continues as usual.- This way, the files are available to the resolvers via the first
rootargument and subsequently to connectors if need-be.
- This way, the files are available to the resolvers via the first
Please chime in if you have any thoughts. I'd especially like for members of the Apollo team to offer their opinions. If we get to a design that satisfies the dev team, I am happy to work alongside any other interested parties to create a PR!