Request metafields using GraphQL

Hi,

Can someone check this dotnet fiddle and let me know what is wrong ?

https://dotnetfiddle.net/ZTA2J0

I receive this :
Failed to retrieve product metafields. Status code: Unauthorized

I have all the access imaginable

Thanx

Hi CubiGear,

I notice a couple of things in that fiddle. I am sure you noticed that the prompts at the top needed to be replaced:

string shopifyUrl = “your_store”;
string apiKey = “your_api_key”;
string apiSecret = “your_api_secret”;

I /think/ that could be the cause of the “Unauthorized”

I forked the fiddle, filled those in for your store and it made the request and it returned with the metafields. However the JsonSerializer.Deserialize does not seem to be handing back an object you can navigate by indices in that way, so I added a Console.WriteLine(json); right after the json string was assigned and I did see a valid response in the console.

I hope this helps,

-Rick

Hi,

When I gave you the fiddle, I did change the credentials so I don’t transmit it !

Wow that is rather strange. Suddenly I tried and it works ???

Well, I hve no clue why I was getting Unauthorized and now I don’t.

Thank you…

I find the result a little bit strange or maybe I’m not querying the right thing.

I was expecting the metafields of 4 variants of the product but I get values that I don’t know where they come from

{
    "data": {
        "product": {
            "metafields": {
                "edges": [{
                        "node": {
                            "key": "dimensions",
                            "value": "{\"length\":10,\"height\":10,\"width\":10,\"no_packing\":true}"
                        }
                    }
                ]
            }
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 13,
            "actualQueryCost": 4,
            "throttleStatus": {
                "maximumAvailable": 1000.0,
                "currentlyAvailable": 996,
                "restoreRate": 50.0
            }
        }
    }
}

Hi CubiGear,

In the fiddle query, you are only requesting the product’s metafields. In our last discussion I mentioned looping through Variants to get their metafields like this:

query ProductVariantMetafields {
  product(id:"gid://shopify/Product/

You can request product AND variant metafields by combining the two queries and having `metafields` at both levels. They will be at the product level in the result while the variant metafields will be at the variant level.

Requesting at both levels /may/ cause the query to be too expensive, if so that will be indicated in the error message.

Also note that I am requesting `nodes` while you are requesting `edges.node`, both work fine and will return the same node data, I just was not querying any "edge" level" information.

Thanks,

-Rick

Hi there,

I get this :

{
    "errors": [{
            "message": "Field 'nodes' doesn't exist on type 'ProductVariantConnection'",
            "locations": [{
                    "line": 5,
                    "column": 7
                }
            ],
            "path": ["query", "product", "variants", "nodes"],
            "extensions": {
                "code": "undefinedField",
                "typeName": "ProductVariantConnection",
                "fieldName": "nodes"
            }
        }
    ]
}

Are you still in the same fiddle? I see a couple of issues there, but none that would result in that error message. Can you share your query here?

The nodes path is a bit shorter, but edges -> nodes is probably better, particularly since you will likely want to move to cursors in the future.

I would also recommend not having your access tokens in a shared fiddle, even for a scrap or test stores.

Same fiddle and same error when you run it.

https://dotnetfiddle.net/ZTA2J0

What you say about edges is a complete mystery to me. I know absolutely nothing about query like this. I’m a complete newbie

Your suggestion is noted for the token. It’s a private fiddle

I see what is wrong, I do not know what the default api version is, but I would suggest changing it to 2023-01.

So change the one line to this client.BaseAddress = new Uri($"https://{shopifyUrl}/admin/api/2023-01/graphql.json"); and nodes will work.

-Rick

1 Like

Bingo. Big thanx !