jaydenseric / graphql-multipart-request-spec

A spec for GraphQL multipart form requests (file uploads).
1.01k stars 56 forks source link

Curl example #6

Closed lunchboxer closed 6 years ago

lunchboxer commented 6 years ago

The readme could be improved by adding an example of usage with CURL. I myself am having difficulty figuring out exactly how it would look.

jaydenseric commented 6 years ago

In the meantime, you might find the apollo-upload-server tests to be a handy reference.

giautm commented 6 years ago

@lunchboxer Here is an example, I copied from Postman. ^_^

curl -X POST \
  http://localhost:3001/graphql \
  -H 'Cache-Control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F 'operations={"query":"mutation ($files: [Upload!]!) {multipleUpload(files: $files) {id filename encoding mimetype path __typename}}","variables":{}}' \
  -F 'map={"0":["variables.files.0"],"1":["variables.files.1"]}' \
  -F '"0"=@/Users/giautm/Devel/IMG_0636.JPG' \
  -F '"1"=@/Users/giautm/Devel/IMG_0637.JPG'

Ps: Replace files 0, 1 path with your files.

jaydenseric commented 6 years ago

Done! The examples now match those of apollo-upload-examples, and they each display a cURL request.

gustavomc commented 6 years ago

im getting:

Error: Must provide document
  at invariant (/app/node_modules/graphql/jsutils/invariant.js:19:11)
  at Object.validate (/app/node_modules/graphql/validation/validate.js:59:34)
  at doRunQuery (/app/node_modules/apollo-server-core/src/runQuery.ts:137:30)
  at /app/node_modules/apollo-server-core/src/runQuery.ts:70:39
  at <anonymous>
  at process._tickDomainCallback (internal/process/next_tick.js:228:7)

when executing curl

any idea? @jaydenseric

jaydenseric commented 6 years ago

@gustavomc No solid ideas, but are you sure apollo-upload-server is setup correctly?

gohelkiran30 commented 6 years ago

I am getting below error with above curl command

curl -X POST \
  http://localhost:3001/graphql \
  -H 'Cache-Control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F 'operations={"query":"mutation ($files: [Upload!]!) {multipleUpload(files: $files) {id filename encoding mimetype path __typename}}","variables":{}}' \
  -F 'map={"0":["variables.files.0"],"1":["variables.files.1"]}' \
  -F '"0"=@/Users/giautm/Devel/IMG_0636.JPG' \
  -F '"1"=@/Users/giautm/Devel/IMG_0637.JPG'

{"errors":[{"message":"GraphQL Request must include at least one of those two parameters: \"query\" or \"queryId\"","category":"request"}]}

jaydenseric commented 6 years ago

@gohelkiran30 that might be because the server is not configured to handle multipart requests. Have you setup the apollo-upload-server middleware?

gohelkiran30 commented 6 years ago

I havn't used any middleware, I am firing a request from console using a curl command hence no middleware required.

jaydenseric commented 6 years ago

The GraphQL server that you are sending the request to (http://localhost:3001/graphql) needs to support GraphQL multipart requests, typically via middleware. Comments in existing issues in this repo is not the right place to asking such usage questions.

gohelkiran30 commented 6 years ago

ok, I will create a new issue thanks

yfyfyfyfyfyf commented 6 years ago

@gustavomc I'm running into the same issue. Any luck solving this one? Thanks a lot!

sunapi386 commented 6 years ago

@gohelkiran30 @giautm I noticed you both are using "variables":{} in a multipleUpload query. This didn't work for me, I get this error:

file-upload> curl localhost:4000 \
                                 -F operations='{ "query": "mutation ($files: [Upload!]!) { multipleUpload(files: $files) { id } }", "variables": {  } }' \
                                 -F map='{ "0": ["variables.file.0"], "1": ["variables.file.1"] }' \
                                 -F 0=@yarn.lock \
                                 -F 1=@package.json
{"errors":[{"message":"Variable \"$files\" of required type \"[Upload!]!\" was not provided.","locations":[{"line":1,"column":11}]}]}⏎    

and actually in the curl example given, you can remove the variable part for singleUpload and it'll still work

file-upload> curl localhost:4000 \
                                 -F operations='{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { id } }" }' \
                                 -F map='{ "0": ["variables.file"] }' \
                                 -F 0=@yarn.lock
{"data":{"singleUpload":{"id":"TLoQ9s44G"}}}⏎      

but multipleUpload won't

file-upload> curl localhost:4000 \
                                 -F operations='{ "query": "mutation ($files: [Upload!]!) { multipleUpload(files: $files) { id } }" }' \
                                 -F map='{ "0": ["variables.file.0"], "1": ["variables.file.1"] }' \
                                 -F 0=@yarn.lock \
                                 -F 1=@package.json
{"errors":[{"message":"Variable \"$files\" of required type \"[Upload!]!\" was not provided.","locations":[{"line":1,"column":11}]}]}⏎       
jaydenseric commented 6 years ago

@sunapi386 follow the example in the spec: https://github.com/jaydenseric/graphql-multipart-request-spec#curl-request-1

sunapi386 commented 6 years ago

@jaydenseric my bad, I mistyped variables.file.1 when it should have been variables.files.1

Very subtle error.