ShopifySharp
ShopifySharp copied to clipboard
Could not deserialize ProductSetPayload
Hello,
I was using the ProductSet mutation with the method that automatically deserializes the result, but at one moment is stopped deserializing and I am not sure why.
How I am using it?
This is an extract of what I was doing:
internal record ProductSetResult
{
[JsonPropertyName("productSet")]
internal required ProductSetPayload ProductSet { get; set; }
}
...
/// Then bellow in my code
...
var graphRequest = new GraphRequest()
{
Query =
"""
mutation upsertProduct($productSet: ProductSetInput!, $synchronous: Boolean!) {
productSet(synchronous: $synchronous, input: $productSet) {
product {
id
handle
createdAt
publishedAt
variants(first: 50) {
nodes {
id
selectedOptions {
name
value
}
}
}
}
userErrors {
field
message
code
}
}
}
""",
Variables = new Dictionary<string, object>
{
// Here my variable properties, but this is not the issue so I will not waste space with this
}
};
var result = await graphService.PostAsync<ProductSetResult>(graphRequest);
The deserialization does not occur, I have no error but the result.Data.ProductSet is always null.
I ended up doing the deserialization myself, and I have no issues doing this without problem (I try catch user and query errors and have none in both cases)
using var result = await graphService.PostAsync(graphRequest);
var rawJsonObject = result.Json.GetRawObject();
if (rawJsonObject is not JsonElement jsonElement)
throw new Exception("Unexpected json object type " + rawJsonObject.GetType().FullName);
if (jsonElement.GetProperty("data").GetProperty("productSet").ValueKind == JsonValueKind.Undefined || jsonElement.GetProperty("data").GetProperty("productSet").ValueKind == JsonValueKind.Null)
throw new Exception("Json value is not defined or null for the product");
return new ProductSetResult()
{
ProductSet = new ProductSetPayload()
{
product = new ShopifySharp.GraphQL.Product()
{
id = jsonElement.GetProperty("data").GetProperty("productSet").GetProperty("product").GetProperty("id").GetString(),
handle = jsonElement.GetProperty("data").GetProperty("productSet").GetProperty("product").GetProperty("handle").GetString(),
createdAt = jsonElement.GetProperty("data").GetProperty("productSet").GetProperty("product").GetProperty("createdAt").GetDateTime(),
variants = new ProductVariantConnection()
{
nodes = [.. jsonElement.GetProperty("data").GetProperty("productSet").GetProperty("product").GetProperty("variants").GetProperty("nodes").EnumerateArray().Select(variantNode => new ShopifySharp.GraphQL.ProductVariant()
{
id = variantNode.GetProperty("id").GetString(),
selectedOptions = [.. variantNode.GetProperty("selectedOptions").EnumerateArray().Select(optionNode => new SelectedOption()
{
name = optionNode.GetProperty("name").GetString(),
value = optionNode.GetProperty("value").GetString()
})]
})]
}
}
}
};
For now is not urgent, but I would like to understand why in some cases it should work the automatic deserialization and other no (I used with other queries and mutations without problem i.e. CreateMetafieldDefinition)