jaydenseric / graphql-upload

Middleware and a scalar Upload to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 132 forks source link

Resolver in apollo server #245

Closed snaplesec closed 3 years ago

snaplesec commented 3 years ago

Hello, I'm trying to use this in apollo-server v2, and not quite sure how to implement the resolver. I am trying to do multiple files upload and don't understand how or what to do in the resolver. Do you have any examples that show how to implement this?

snaplesec commented 3 years ago

I have tried to implement according to the graphql-multipart spec, and I get an error with POST telling me that I don't have body-parser installed. On apollo-express-graphql do I need to use the middleware approach?

jaydenseric commented 3 years ago

There is a full stack example of file uploads with apollo-upload-client, graphql-upload, and apollo-server-koa. You have to turn off the outdated Apollo Server file upload implementation using the constructor option uploads: false:

https://github.com/jaydenseric/apollo-upload-examples/blob/be1444a0aa8768c6fe2e2c96119eb2b6344fcc50/api/server.js#L78-L81

Then you need to setup graphql-upload from scratch (follow the setup instructions) starting with the middleware; the example is using Koa but you can adapt it for Express.

Regarding the resolver for multiple uploads; here is one:

https://github.com/jaydenseric/apollo-upload-examples/blob/be1444a0aa8768c6fe2e2c96119eb2b6344fcc50/api/schema/MutationType.js#L21-L40