Skip to content

Could not deserialize ProductSetPayload #1179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
amcroce opened this issue Apr 29, 2025 · 0 comments
Open

Could not deserialize ProductSetPayload #1179

amcroce opened this issue Apr 29, 2025 · 0 comments

Comments

@amcroce
Copy link

amcroce commented Apr 29, 2025

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant