我正在制作一个使用GraphQL API的dotnet Web应用程序。我的问题是执行查询后,响应与我的实体模型不匹配,我认为这是因为响应具有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"
}
}
]
}
}
}
]
}
}
}