如何将字节数组(Blob)发送到 GraphQL 突变 [英] How to send byte array (Blob) to GraphQL mutation
本文介绍了如何将字节数组(Blob)发送到 GraphQL 突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们有一个带有字节数组 (Blob) 字段的 GraphQL 突变.如何使用 Insomnia 或 GraphQL playground 等工具发送字节数组数据来测试 API?
We have a GraphQL mutation with a byte array (Blob) field. How can I use tools like Insomnia or GraphQL playground to send byte array data to test the API?
mutation {
saveSomething(something: {
contentByteArray: [97, 110, 103, 101, 108, 111]
}) {
content
}
}
推荐答案
你可以这样发送数据,但是我理解 GraphQL 发送一个List 字节而不是一个数组.在 C# 中,这样的字段(我正在上传照片)将被描述为
You can send data like that, however I understand that GraphQL sends a List of bytes rather than an array. In C# such a field (I'm uploading a photo) would be described as
Field<ListGraphType<ByteGraphType>>("photo");
并且需要转换回数组才能保存到数据库中例如
and would need to be converted back into an array in order to be saved to the database e.g.
IDictionary<string, object> dicPlayer = (IDictionary<string, object>)context.Arguments["input"];
...
if (dicPlayer.ContainsKey("photo")) {
if (dicPlayer["photo"] == null) {
playerInput.Photo = null;
} else {
List<byte> lstB = new List<byte>();
foreach (var objB in (List<object>)dicPlayer["photo"]) {
byte b = (byte)objB;
lstB.Add(b);
}
byte[] arrB = lstB.ToArray();
playerInput.Photo = arrB;
}
}
这篇关于如何将字节数组(Blob)发送到 GraphQL 突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文