Reputation: 11
I am making a dotnet web appplication that consumes a GraphQL API. The problem I have is that after I execute the query the response doesn't match with my entity model, I supose that it is because the response has a edge and node tags.
can some body help me?
Follow my code:
Model:
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; }
}
}
Query excecution:
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;
}
the response i get when i use the postman:
"data": {
"products": {
"edges": [
{
"node": {
"title": "the tittle",
"variants": {
"edges": [
{
"node": {
"id": "The ID",
"title": "The variant tittle",
"price": "0.00",
"sku": "the sku code"
}
}
]
}
}
}
]
}
}
}
Upvotes: 1
Views: 8319
Reputation: 1
Did you look at the JSON response as interpreted by GraphQL.Client
? What I noticed is that, while my JSON looks perfect in Postman, the first node (products
in your case) comes back like this:
"products", [{
instead of like this:
"products": {
So now I'm trying to rewrite my code without using GraphQL.client
.
Upvotes: 0
Reputation: 296
Paste the GraphQL response into https://app.quicktype.io/?l=csharp and it will generate all the classes you need
Upvotes: 4