Dotnet Graphql 响应反序列化

问题描述 投票:0回答:2

我正在制作一个使用 GraphQL API 的 dotnet 网络应用程序。我遇到的问题是,在我执行查询后,响应与我的实体模型不匹配,我想这是因为响应有边和节点标签。

有人可以帮助我吗?

按照我的代码:

型号:

public class ProductResponseType 
    {
        
        public ListGraphType<Product> products { get; set; }
    }


    public class Product : ObjectGraphType
    {
        public List<ProductNode> edgeProduct { get; set; }

        public class ProductNode
        {
            public string title { get; set; }
            public ListGraphType<Variant> variants { get; set; }

        }

    }

    public class Variant : ObjectGraphType
    {
        public List<VariantNode> variantProduct { get; set; }
        public class VariantNode
        {

            public string Id { get; set; }


            public string Title { get; set; }


            public string Price { get; set; }


            public string Sku { get; set; }

        }

    }

查询执行:

try
            {
                GraphQLHttpClientOptions graphQLOptions = new GraphQLHttpClientOptions
                {
                    EndPoint = new Uri(_GraphQlURI),

                };

                var graphQLClient = new GraphQLHttpClient(graphQLOptions, new GraphQL.Client.Serializer.Newtonsoft.NewtonsoftJsonSerializer());
                graphQLClient.HttpClient.DefaultRequestHeaders.Add("Access-Token", "token");
                graphQLClient.HttpClient.DefaultRequestHeaders.Add("Accept", "application/json");
                    
             
                var productRequest = new GraphQLRequest
                {
                    Query = @"query {
                              products(first:2) {
                                edges {
                                  node {
                                      title
                                    variants(first: 2) {
                                      edges {
                                        node {
                                          id
                                          title
                                          price
                                          sku
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }"
                };

               
                var productResponse =  await graphQLClient.SendQueryAsync<ProductResponseType>(productRequest);



                return "";
                //return graphQLResponse.Data.WebUrl;
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Error al introducir crear el checkout");
                return null;
            }

我使用邮递员时得到的回应:

 "data": {
        "products": {
            "edges": [
                {
                    "node": {
                        "title": "the tittle",
                        "variants": {
                            "edges": [
                                {
                                    "node": {
                                        "id": "The ID",
                                        "title": "The variant tittle",
                                        "price": "0.00",
                                        "sku": "the sku code"
                                    }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
}
c# json serialization graphql json-deserialization
2个回答
4
投票

将 GraphQL 响应粘贴到 https://app.quicktype.io/?l=csharp 它将生成您需要的所有类


0
投票

您是否看过

GraphQL.Client
解释的 JSON 响应?我注意到的是,虽然我的 JSON 在 Postman 中看起来很完美,但第一个节点(在您的情况下为
products
)像这样返回:

"products", [{

而不是像这样:

"products": {

所以现在我试图在不使用

GraphQL.client
的情况下重写我的代码。

© www.soinside.com 2019 - 2024. All rights reserved.