c# - Dotnet Graphql 响应反序列化
问题描述
我正在制作一个使用 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"
}
}
]
}
}
}
]
}
}
}
解决方案
将 GraphQL 响应粘贴到https://app.quicktype.io/?l=csharp,它将生成您需要的所有类
推荐阅读
- c++ - 使用变体时模板化的子代不被接受为父代
- postgis - 将本地 Geoserver 的数据目录移动到生产服务器无法创建新存储
- sas - Sas Viya 部署
- node.js - 带有 Promises.all 的 Firestore 功能需要很长时间才能完成
- git - Visual Studio Git - 是否有防止提交未暂存文件的设置
- c++ - 在 Clang C++ 上使用 __super
- html - 实现在某些点之间有间隙的 CSS 边框
- javascript - 如何不使用 cferdinandi/smooth-scroll 在 URL 中显示 #id
- android - 如何在不使用 GPS 的情况下在 android 9 ( Pie ) 设备中获取 Cell ID 和 Location Area 代码?
- excel - 如何在 Excel 宏中为标题传递外部变量?