How to send byte array (Blob) to GraphQL mutation
Asked Answered
A

1

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
  }
}
Aeriela answered 11/8, 2019 at 2:16 Comment(0)
V
0

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

Vinita answered 20/12, 2020 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.