ZeroQL icon indicating copy to clipboard operation
ZeroQL copied to clipboard

Do ZeroQL support file sending?

Open ShSSP opened this issue 3 years ago • 8 comments

I have not found an example or words about sending a file to GraphQL in the documentation. I had problems using GraphqlClient to send files. Can I use ZqroQL for it?

ShSSP avatar Aug 10 '22 06:08 ShSSP

GraphQL has a special way to upload a file via the Upload type. This way is not supported. The workaround would be to pack the file into the base64 string and send it like a simple string. Also, you can create a REST endpoint to do that.

Meanwhile, I think I can look at it, but I don't think it will be fast. It requires significant changes inside to make it work.

byme8 avatar Aug 10 '22 07:08 byme8

Thank you for so fast answer. Actually I've already solve this problem using standard HttpClient, MultipartFormDataContent and file mapping to contexts. It was required to simulate the structure of the http request to GraphQL with Upload file type.

I afraid to offer my help, though :D

Meanwhile, I think I can look at it, but I don't think it will be fast. It requires significant changes inside to make it work.

ShSSP avatar Aug 10 '22 07:08 ShSSP

Hi ! I'm also interested in this feature !

@ShSSP : Can you share some code to show how you did it ? Thanks a lot.

GautierT avatar Aug 18 '22 08:08 GautierT

In progress

byme8 avatar Aug 18 '22 18:08 byme8

@GautierT, it was something like that: `

        var images = new[] { new { Name = "file.txt", Bytes = new byte[10] } };
        var query = $"mutation ($images: [Upload]) {{..}} ";
        var variables = new { images = Enumerable.Repeat<object>(null, images.Length) };
        var index1 = 0; 
        var mapings = string.Join(",", 
            Enumerable.Range(0, images.Length)
                .Select(x => @$"""{index1}"":[""variables.images.{index1++}""]")//"images" = $images 
        );
        var map = $"{{{mapings}}}";
        var form = new MultipartFormDataContent();
        var operationsJs = JsonConvert.SerializeObject(new { query, variables });
        form.Add(new StringContent(operationsJs), "operations");
        form.Add(new StringContent(map), "map");
        var index2 = 0;
        foreach (var i in images)
            form.Add(new ByteArrayContent(i.Bytes, 0, i.Bytes.Length), (index2++).ToString(), i.Name);

        using var httpClient = new HttpClient();
        var response = httpClient.PostAsync(_endPointUrl, form).Result;

`

ShSSP avatar Aug 19 '22 16:08 ShSSP

Thanks a lot @ShSSP !

GautierT avatar Aug 24 '22 07:08 GautierT

@GautierT @ShSSP You can try version 2.0.0-preview.2. It has support for Upload type. Don't forget to update the CLI and the NuGet package.

You can use it like that:


var variables = new { Id = 2, File = new Upload("image.png", new MemoryStream()) };
var user = await client.Mutation(variables, (i,m) => m.AddAvatar(i.Id, i.File));

byme8 avatar Aug 26 '22 19:08 byme8

@byme8 Much appreciated. I'll try it as fast as possible!

ShSSP avatar Aug 27 '22 20:08 ShSSP