pythongraphqlariadne-graphql

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 0=@image.jpeg

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 ?


Solution

  • 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 0=@image.jpeg