Graphql突变以与其他字段一起上传文件 [英] Graphql mutation to upload file with other fields

查看:64
本文介绍了Graphql突变以与其他字段一起上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!
}

和以下curl命令上传图像:

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.

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

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!
}

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

I'm struggling to get the correct curl command to upload the image with the 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'

但是我得到这个响应条目键'1'(https://github.com/jaydenseric/graphql-multipart-request-spec)缺少文件数据.

如何通过传递 userid image 来调用我的GraphQL端点?

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

推荐答案

仅文件需要特殊处理",其他数据以经典方式通过变量" 传递.

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

这篇关于Graphql突变以与其他字段一起上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆