Deadlocks whenever calling graphQLClient from Maui

Deadlocks whenever calling graphQLClient from Maui


0

I am really struggling to get any data returned from a GraphQL Query in Maui. I had the code in the initializer of the Page and was hitting Deadlocks. I get that now. So I have move the code to a button on a page. It is hanging on the line below the Task GetDocumentAsync:

var graphQLResponse = await graphQLHttpClient.SendQueryAsync<RootObject>(documentRequest);

The almost similar code works fine in a console app. But I can’t get it to return in the Maui app. No errors, just hangs.

Here is some relevant code:

Button on a page:

MAINPAGE XAML

<VerticalStackLayout Margin="20">
    <Button Text="Load"
            VerticalOptions="Center"
            HorizontalOptions="Center"
            Clicked="OnButtonClicked"/>
    <Entry x:Name="documentId"
           FontSize="24"
           Keyboard="Chat"
           Placeholder="EnterScan Id"
           ClearButtonVisibility="WhileEditing"/>
    <ListView ItemsSource="{Binding ErpDocument}" 
              HasUnevenRows="True" 
              VerticalOptions="FillAndExpand"
              SeparatorColor="Black"
              ItemSelected="OnListItemSelected"
              SelectedItem="{Binding SelectedListItem,Mode=TwoWay}">
        <ListView.BindingContext>
            <viewModels:DocumentViewModel />
        </ListView.BindingContext>
    

CODE-BEHIND

async void OnButtonClicked(object sender, EventArgs args)
{
    DocId key = new DocId();
    key.Id1 = "Default";
    key.Id2 = "WF201B";
    key.Id3 = "000";
    ErpPart erpDocument = ErpPart.Load(key); ;
}
    

DOCUMENTVIEWMODEL CODE

public static ErpPart Load(DocId key)
    {
        string qlQuery = "{ products(filter:{and: [{fac: {eq: "" + key.Id1 + "" }}, {fpartno: {eq: "" + key.Id2 + ""}}, {frev: {eq: "" + key.Id3 + "" }} ]}) " +
                        @"{
                                items {
                                    id1 : fpartno
                                    id2 : frev
                                    id3 : fac
                                    id4 : fac
                                    name : fdescript
                                    OnHand {
                                        locations: items {
                                            location: flocation
                                            bin : fbinno
                                            lotsn : flot
                                            onhand : fonhand
                                        }
                                    }
                                    uofm : fmeasure
                                    source : fsource
                                    purchased : fcpurchase
                                    status : fcstscode
                                }
                            }
                        }";
    Item doc = new ();
    Item[] docs = null;

    try
    {
        var rootObj = new RootObject();
        rootObj = GetDocumentAsync(qlQuery).GetAwaiter().GetResult();
        docs = rootObj.products.items;
    }
    catch (Exception ex)
    {
        Debug.Print(ex.Message);
    }

    if (docs != null)
    {
        foreach (var row in docs)
        {
            doc.Id1 = row.Id1;
            doc.Id2 = row.Id2;
            doc.Id3 = row.Id3;
            doc.Id4 = row.Id4;
            doc.Name = row.Name;
            doc.Type = "Part";
            doc.Onhand = row.Onhand;
            doc.UofM = row.UofM;
            doc.Source = row.Source;
            doc.Purchased = row.Purchased;
            doc.Status = row.Status;
            //doc.Color = MauiProgram.colorPart;

            Location[] inventory = row.Onhand.Locations;
            Debug.Print("");
            decimal onhand = 0;
            if (inventory != null && inventory.Length > 0)
            {

            }
            else
            {
                //Debug.Print("No OnHand Exists.");
            }
        }
    }
    else
    {
        Debug.Print("No matching Parts found in query.");
    }
    return new() { Part = doc };
}

private static async Task<RootObject> GetDocumentAsync(string iQuery)
{

    var graphQLOptions = new GraphQLHttpClientOptions
    {
        EndPoint = new Uri($"{Url}/graphql", UriKind.Absolute)
    };

    var graphQLHttpClient = new GraphQLHttpClient(graphQLOptions, new NewtonsoftJsonSerializer());

    var documentRequest = new GraphQLRequest { Query = iQuery };

    try
    {
        var graphQLResponse = await graphQLHttpClient.SendQueryAsync<RootObject>(documentRequest);
        Debug.Print("");
        return null;
    }
    catch (Exception ex)
    {
        Debug.Print(ex.Message);
        return null;
    }

}

1

  • you need to use await when calling GetDocumentAsync, not GetAwaiter().GetResult()

    – Jason

    14 mins ago


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *