【问题标题】:Graphql mutation to upload file with other fieldsGraphql 突变以上传具有其他字段的文件
【发布时间】:2020-10-22 05:24:10
【问题描述】:

我正在使用 Ariadne 创建一个 python GraphQL 服务器,并且我有以下突变来上传文件:

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

还有如下 curl 命令上传图片:

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

上面的命令可以完美地将图像上传到端点。

但是,我需要将用户 ID 与图像一起传递,如下所示:

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

我正在努力获取正确的 curl 命令以使用 userid 上传图像。

我试过下面的命令

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'

但是我收到了这个回复File data was missing for entry key '1' (https://github.com/jaydenseric/graphql-multipart-request-spec).

如何调用我的 GraphQL 端点,通过 useridimage 进行上传?

【问题讨论】:

  • 你试过没有abc1234 周围的单引号吗?所以最后一位是-F 1=abc1234

标签: python graphql ariadne-graphql


【解决方案1】:

不要像-F name=content 那样包装表单数据的content 部分,就像您对用户ID 所做的那样。试试这个:

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

我没有尝试过,但我阅读了 curl 文档 (man curl) 并阅读了有关表单数据的部分 -F

【讨论】:

  • 没用,得到这个File data was missing for entry key '1' (https://github.com/jaydenseric/graphql-multipart-request-spec)
  • 好的,我没有其他想法。祝你好运。
【解决方案2】:

只有文件需要“特殊处理”,其他数据以经典方式传递,"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

【讨论】:

    猜你喜欢
    • 2020-08-02
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 2014-05-19
    • 2018-09-21
    • 2013-01-26
    • 2015-06-04
    相关资源
    最近更新 更多