Do tnet Десериализация ответа Graphql - PullRequest
0 голосов
/ 03 августа 2020

Я делаю веб-приложение do tnet, которое использует GraphQL API. Моя проблема заключается в том, что после того, как я выполняю запрос, ответ не совпадает с моей моделью сущности, я предполагаю, что это потому, что ответ имеет теги edge и node.

может ли какое-то тело мне помочь?

Следуйте моему коду:

Модель:

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