Kleyson Rios
Kleyson Rios

Reputation: 2877

Graphql mutation to upload file with other fields

I'm using Ariadne to create a python GraphQL server and a I have the following mutation to upload a file:

type Mutation {
    uploadUserAvatar(file: Upload!): Boolean!
}

And the following curl command to upload a image:

curl http://localhost:8080/ \
  -F operations='{ "query":"mutation ($file: Upload!) { uploadUserAvatar(file: $file) }","variables": { "file": null} }' \
  -F map='{ "0": ["variables.file"] }' \
  -F [email protected]

The command above works perfectly to upload the image into the endpoint.

But, I need to pass together with the image the user id, in a mutation like this:

type Mutation {
    uploadUserAvatar(userid: String!, file: Upload!): Boolean!
}

I'm struggling to get the correct curl command to upload the image with the userid.

I tried the command below

curl http://localhost:8080/ \
  -F operations='{ "query":"mutation ($userid: String!, $file: Upload!) { uploadUserAvatar(userid: $userid, file: $file) }","variables": { "file": null, "userid": null } }' \
  -F map='{ "0": ["variables.file"], "1": ["variables.userid"] }' \
  -F 0=@etiqueta_LG.jpeg \
  -F 1='abc1234'

But I'm getting this response File data was missing for entry key '1' (https://github.com/jaydenseric/graphql-multipart-request-spec).

How can I call my GraphQL endpoint passing the userid and the image for upload ?

Upvotes: 0

Views: 3863

Answers (2)

xadm
xadm

Reputation: 8418

Only files needs 'special treatment', other data pass in classic way, by "variables".

curl http://localhost:8080/ \
  -F operations='{ "query":"mutation ($userid: String!, $file: Upload!) { uploadUserAvatar(userid: $userid, file: $file) }", "variables": { "file": null, "userid": "abc1234" } }' \
  -F map='{ "0": ["variables.file"] }' \
  -F [email protected]

Upvotes: 3

Peaceful James
Peaceful James

Reputation: 2233

Don't wrap the content part of the form data like -F name=content, like you are doing with the userid. Try this:

curl http://localhost:8080/ \
  -F operations='{ "query":"mutation ($userid: String!, $file: Upload!) { uploadUserAvatar(userid: $userid, file: $file) }","variables": { "file": null, "userid": null } }' \
  -F map='{ "0": ["variables.file"], "1": ["variables.userid"] }' \
  -F 0=@etiqueta_LG.jpeg \
  -F 1=abc1234

I have not tried this but I read the curl docs (man curl) and read the section on form data -F.

Upvotes: 0

Related Questions