ShopifySharp icon indicating copy to clipboard operation
ShopifySharp copied to clipboard

Product Image metafields

Open carldebilly opened this issue 5 years ago • 0 comments
trafficstars

I'm unable to get metafields on a product_image using ShopifySharp.

Doing so will produce a 404 Not Found: the request is not following the Shopify Documentation

Code

var metafields = await metafieldService.GetList(imageId, "product_image");

Current url

GET https://my-store.myshopify.com/admin/api/2020-07/product_image/<image_id>/metafields.json

Expected url

GET https://my-store.myshopify.com/admin/metafields.json?metafield[owner_id]=#<image_id>&metafield[owner_resource]=product_image

Workaround

I found a workaround for this:

var query = $@"
    query ImagesWithFields {{
        product(id: ""gid://shopify/Product/{productId.ToString(NumberFormatInfo.InvariantInfo)}"") {{
            id
            images(first: 30) {{
                edges {{
                    node {{
                        id
                        src
                        metafields(first: 10) {{
                            edges {{
                                node {{
                                    id
                                    key
                                    value
                                    namespace
                                }}
                            }}
                        }}
                    }}
                }}
            }}
        }}
    }}";
var r = await _graphService.PostAsync(query, ct);

var images = r["product"]["images"]["edges"]
    .Select(img =>
    {
        var node = img["node"];
        var id = node["id"].ToObject<string>();
        var src = node["src"].ToObject<string>();

        var fields = node["metafields"]["edges"]
            .Select(f =>
            {
                var node = f["node"];
                var id = node["id"].ToObject<string>();
                var ns = node["namespace"].ToObject<string>();
                var key = node["key"].ToObject<string>();
                var value = node["value"].ToObject<string>();

                return (id, ns, key, value);
            })
            .ToArray();

        return (id, src, fields);
    })
    .ToArray();

carldebilly avatar Oct 06 '20 11:10 carldebilly