1

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

1 回答 1

0

你可以发送这样的数据,但是我知道 GraphQL 发送的是字节列表而不是数组。在 C# 中,这样一个字段(我正在上传照片)将被描述为

Field<ListGraphType<ByteGraphType>>("photo");

并且需要转换回数组才能保存到数据库中,例如


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

于 2020-12-20T17:19:48.413 回答