【问题标题】:Dotnet Graphql Response DeserializationDotnet Graphql 响应反序列化
【发布时间】:2020-08-03 17:56:20
【问题描述】:

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

有人能帮我吗?

按照我的代码:

型号:

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"
                                    }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
}

【问题讨论】:

  • 您应该使您的模型适应 JSON 反序列化返回的内容,或者将这些类型映射到您的模型。前者可以使用 Visual Studio 的“将 JSON 粘贴为类”功能轻松完成,后者可以使用 AutoMapper 完成。两种选择都应该起作用,决定你想去的那个,如果你遇到问题,用具体的问题、错误和行为编辑你的问题
  • 我试过但还是不行...
  • 任何例子都将不胜感激

标签: c# json serialization graphql json-deserialization


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    相关资源
    最近更新 更多