GraphQL.Client 未返回结果,但服务器完成请求

GraphQL.Client not returning results, but server completes the request

提问人:BumpaRoy 提问时间:11/7/2023 更新时间:11/8/2023 访问量:36

问:

我无法检索返回的 GraphQL 查询响应的结果。服务器记录请求已完成,没有错误。但该计划仍在等待响应。在提供的代码中,永远不会访问 await graphQLHttpClient.SendQueryAsync 后面的 Debug.Print 语句。它只是无限期地挂起此语句,并且没有超时或错误。

此代码在 ViewModel 初始值设定项中执行。

'''

using System.Diagnostics;
using GraphQL;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.Newtonsoft;

namespace peMove.Maui.LookupViewModels
{
    public class RootObject
    {
        public Documents ErpDocuments { get; set; }
    }

    public class Documents
    {
        public getDocument[] Rows { get; set; }
    }

    public class getDocument
    {
        public string id1 { get; set; }
        public string id2 { get; set; }
        public string name { get; set; }
    }

    public class Document
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public string TypeShort { get; set; }
        public decimal Onhand { get; set; }
        public string UofM { get; set; }
        public string Note { get; set; }
        public Color Color { get; set; }

    }

    public class Onhand
    {
        public Location[] Locations { get; set; }
        //public Location[] items { get; set; }  This will also work on the non-alias
    }

    public class Location
    {
        public string location { get; set; }
        public string bin { get; set; }
        public string lotsn { get; set; }
        public float onhand { get; set; }
    }

    public class ViewModel_L
    {
        public static Document erpDoc;

        static readonly string Url = "https://1eaa-98-103-130-98.ngrok-free.app";    // ngrok public url
        private static GraphQLHttpClient graphQLHttpClient;
        
        private static string qlQuery = @"{ 
                            products(filter:{and: [{fac: {eq: ""Default""}}, {fpartno: {eq: ""WF201W""}}, {frev: {eq: ""000""}} ]})
                            {
                                parts : items {
                                    id1 : fpartno
                                    id2 : frev
                                    name : fdescript
                                }
                            }
            }";

        public static async Task RunAsync()

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

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

            var productRequest = new GraphQLRequest { Query = qlQuery };

            getDocument[] docs = null;
            try
            {
                var graphQLResponse = await graphQLHttpClient.SendQueryAsync<RootObject>(productRequest);
                Debug.Print("");
                docs = graphQLResponse.Data.ErpDocuments.Rows;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }

            if (docs !=null)
            {
                foreach (var doc in docs)
                {
                    erpDoc.Id = doc.id1 + ":" + doc.id2;
                    erpDoc.Name = doc.name;
                    erpDoc.Type = "Part";
                    erpDoc.TypeShort = erpDoc.Type[..1];      // First charater
                    erpDoc.Onhand = 0;
                    //erpDoc.Note = "UofM: " + doc.uofm + " Source: " + doc.source + " Purchased: " + doc.purchased + " Status: " + doc.status;
                    erpDoc.Color = MauiProgram.colorPart;
                }
            }
            else
            {
                Console.WriteLine("No matching Parts found in query.");
            }
        }

        public ViewModel_L()
        {
            RunAsync().GetAwaiter().GetResult();
            this.ErpDocument = erpDoc;
        }

        public Document ErpDocument { get; set; }
    }       
}

'''

C# GraphQL Maui

评论

0赞 Jason 11/7/2023
您正在通过在构造函数中调用异步方法来创建死锁。
0赞 BumpaRoy 11/7/2023
如果我希望该方法在页面加载时立即执行,并且此 VM 绑定到页面,则在哪里调用该方法。
0赞 Jason 11/7/2023
将其放在从页面 OnAppearing 调用的 Init 方法中

答: 暂无答案