jaydenseric / apollo-upload-client

A terminating Apollo Link for Apollo Client that fetches a GraphQL multipart request if the GraphQL variables contain files (by default FileList, File, or Blob instances), or else fetches a regular GraphQL POST or GET request (depending on the config and GraphQL operation).
https://npm.im/apollo-upload-client
1.53k stars 156 forks source link

How to handle different upload scenarios #283

Closed yazer79 closed 2 years ago

yazer79 commented 2 years ago

This is not a problem, just help or explanation needed about how uploads are best handled in graphql. I have upload client / singleUpload apolloserver resolver, I feel lost after this basic step. I do not feel that uploading all files just in 1 directory is a good idea. My question is, if I need multiple upload types in my app, how do I handle this? For example, uploading an avatar, a profile picture, ..etc Do I do repeat a resolver (on server side) for each upload type? eg: avatarUpload resolver, pictureUpload resolver,... etc Or there is some way to send more info about the "upload type or group / save directory" from client? Or simply all uploads are saved in one directory? Any ideas are welcome

jaydenseric commented 2 years ago

Treat user uploaded files in a similar way to user database content. They should be stored somewhere other than the GraphQL API server, which you want to be able to redeploy/discard/upgrade/etc. without worrying about losing user data.

You might be interested in this old comment on the topic: https://github.com/jaydenseric/graphql-upload/issues/251#issuecomment-862135269 .

I don't suggest using a mutation named something like uploadFile to upload all files for various purposes; instead it should be for example userCreate and one of the arguments is avatar of Upload scalar type.

Here is a realistic example from a real API to add an artwork that can have images, and later, to add, reorder, and delete artwork images:

Screen Shot 2021-12-13 at 1 05 47 pm Screen Shot 2021-12-13 at 1 05 32 pm

Notice how the naming convention allows mutations relating to the same things to sort together.

I make a few generic helpers like storeS3Image and getImgixImageMeta to make resolver code more DRY.