Skip to content
This repository was archived by the owner on Apr 14, 2023. It is now read-only.
This repository was archived by the owner on Apr 14, 2023. It is now read-only.

[Proposal] File Uploads #65

@jangerhofer

Description

@jangerhofer

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-data POST request, whereas Apollo Client interfaces with the Server exclusively with application/json requests 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-data POST to the specified Apollo Server endpoint. The request would be identical to the normal request apart from the inclusion of a new files field or similar which contains the uploading files. I think I would lean towards the clarity of a new method on account of readability.

  • Apollo Server will then need to check each request for its content-type; if it is application/json, then no upload-specific logic happens and the mutation continues as usual. If, however, the POST is of content-type multipart/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 root argument and subsequently to connectors if need-be.

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions