Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Use of Apollo Client to Upload Files #1

Open
jangerhofer opened this issue Aug 17, 2016 · 2 comments
Open

Show Use of Apollo Client to Upload Files #1

jangerhofer opened this issue Aug 17, 2016 · 2 comments
Assignees

Comments

@jangerhofer
Copy link
Owner

Write out an example using Apollo Client to attach files to a mutation.

@jangerhofer
Copy link
Owner Author

jangerhofer commented Aug 28, 2016

This is currently infeasible using the backend in 0.1 without modifying Apollo Client code.

My original thought was to use Apollo Client middleware to modify the POST request made by the Client, but I discovered: Multer works only on multipart/form-data requests, while Apollo Client sends application/json type requests to Apollo Server.

My new line of thinking is to use a dedicated Express endpoint (e.g. /tmp/uploads) to receive files. The Client will first initiate the upload and then call the GQL mutation. The server will handle the file based on information in the mutation.

@HriBB
Copy link

HriBB commented Sep 7, 2016

Hey. Great to see someone else trying to integrate file uploads with Apollostack. Here are some of my thoughts.

You could create a custom createNetworkInterface that uses multipart/form-data. However, I think the best way would be to integrate this directly into apollo-client and apollo-server. The guys at Apollo are open for this, but ATM I think they have more important stuff to do. But I believe that both express-graphql and relay now support file uploads, so Apollo simply cannot go without it :) Probably a post-1.0 feature.

Your approach with a dedicated Express endpoint could work. But what happens if a mutation fails? You end up with some random files in /tmp/uploads and you need to do manual garbage collection. I don't really like it, but that's just my opinion. Maybe you could propose this on the Apollo repositories to get some feedback.

Again, I think that this needs to be in the core Apollo. It will be very tricky to implement it however. For example, apollo-server has 3 integrations: Express, Koa and Hapi, and each uses a different way of handling file uploads. I already have a working implementation for Koa in my fork, but it needs to be rewritten into a bodyParser-like middleware.

As for the client, I think that it should be implemented in createNetworkInterface. Check out this tutorial. Maybe we can start working on a PR together. I'm sure the guys at Apollo will help us with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants