jaydenseric / graphql-multipart-request-spec

A spec for GraphQL multipart form requests (file uploads).
993 stars 54 forks source link

In your example you only show not nullable file upload system? #59

Closed flimybook closed 2 years ago

flimybook commented 2 years ago

If there is a optional file upload system then how can I send data. It is very sad that in your example I can't see any example. If user want to upload file then he upload otherwise not, for this case you do not show any example. Please throw an example for the following oparations-

{
  query: `
    mutation($file: Upload) {
      singleUpload(file: $file) {
        id
      }
    }
  `,
  variables: {
    file: null(No file)
  }
}

Here file upload is optional. Thank you very much.

For above operations If I do this-

curl localhost:3001/graphql \
  -F operations='{ "query": "mutation ($file: Upload) { singleUpload(file: $file) { id } }", "variables": { "file": null } }' \
  -F map='{ "0": ["variables.file"] }' \
  -F 0=@null

It throw a error message like image- Screenshot 2021-10-26 133513

jaydenseric commented 2 years ago

If there are no files to upload (i.e. the Upload scalar variables/args are all optional and have null values), then the request should be a regular GraphQL POST request and not a GraphQL multipart request.

In theory, you could post a GraphQL multipart request that doesn't have any files, but it's more expensive for the server to deal with so I've never seen anyone do that on purpose. I haven't thought carefully about this so don't take this example as gospel, but perhaps it would look like this:

curl localhost:3001/graphql \
  -F operations='{ "query": "mutation ($file: Upload) { singleUpload(file: $file) { id } }", "variables": { "file": null } }' \
  -F map='{}'

If there are no files to upload, then definitely there should not be any file fields in the multipart request.

While there shouldn't be any problems, it might be a good idea to add a graphql-upload processRequest function test for a GraphQL multipart request that is well structured but without any files, since it appears we don't have a test for that yet:

https://github.com/jaydenseric/graphql-upload/blob/v12.0.0/test/public/processRequest.test.mjs

If for an operation you have a few variables of Upload scalar type with some populated by files and others optional and null, then for the populated ones do the regular map field entries and append the file fields, but for the unpopulated ones just leave the null values alone in the operations field JSON like how non Upload values are normally treated.