jaydenseric / graphql-upload

Middleware and an Upload scalar 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 131 forks source link

Uploading file with extra payload in variables doesn't seem working #331

Closed vincentwinkel closed 2 years ago

vincentwinkel commented 2 years ago

After days of investigation (supertest, apollo, graphql-upload, ...) I finally could find the origin of my issue: when I upload a file, using extra payload directly in the query, it works:

Screenshot 2022-08-15 at 15 31 43

If I add the extra payload in variables, it does'nt work (I replaced here input from an object to a string for simplicity of debugging):

Screenshot 2022-08-16 at 09 35 30

Operations: { "query": "mutation a($file: Upload!, $input: String!) { uploadFile(file: $file, input: $input) }" } Map: { "0": ["variables.file"], "1": ["variables.input"] }

I found a lot of examples / tuto / forum topics explaininig how to deal with supertest, like this one, very helpful, but then I realiseed nobody was trying to add more than 1 param (the file to upload). Is there really a bug or I misunderstand something? I also tried to use FormData (not compatible anymore with supertest), to inject the variables directly in operations, to try with postman, curl, supertest, nodejs, ...

jaydenseric commented 2 years ago

Hi @vincentwinkel, if you paste the operations JSON in a markdown code block I can copy from I'll write the structure of a correct request for you; the problem is that the GraphQL multipart request form fields you are attempting is not according with the spec:

https://github.com/jaydenseric/graphql-multipart-request-spec#multipart-form-field-structure

vincentwinkel commented 2 years ago

My bad @jaydenseric I didnt expect an anwer so fast. I wanted to add the version used but then I realised it's out of date (v13), so I'm trying to install the last version, but get another issue aas mentionned here

jaydenseric commented 2 years ago

Closing because this is not an issue with graphql-upload but a usage question about how to structure a GraphQL multipart request.

The numbered fields following operations and map should only be file fields, but you are incorrectly adding one for a string value. You are also missing the variables JSON within the operations field. The variables should contain the same stuff as a regular GraphQL post request, just with file values replaced with null.

vincentwinkel commented 2 years ago

Like said above, I tried to add the variables directly in the operations field, it also didnt work, because I kept the wrong mapping. Now, it indeed works. I post for the ones who are stuck as well.

Screenshot 2022-08-16 at 11 12 09

Operations: { "query": "mutation a($file: Upload!, $input: String!) { uploadFile(file: $file, input: $input) }", "variables": { "file": true, "input": "toto" } } Map: { "0": ["variables.file"] }

Thanks