Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Request metafields using GraphQL

Solved

Request metafields using GraphQL

CubiGear
Shopify Partner
31 3 7

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

Accepted Solution (1)
rick-shopify
Shopify Staff
9 2 3

This is an accepted solution.

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

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 9 (9)

rick-shopify
Shopify Staff
9 2 3

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

To learn more visit the Shopify Help Center or the Community Blog.

CubiGear
Shopify Partner
31 3 7

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...

CubiGear
Shopify Partner
31 3 7

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
            }
        }
    }
}
rick-shopify
Shopify Staff
9 2 3

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/<redacted product id>") {
    variants(first:10) {
      nodes {
        id
        metafields(first:10) {
          nodes {
            id
            key
            value
          }
        }
      }
    }
  }
}

 

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

To learn more visit the Shopify Help Center or the Community Blog.

CubiGear
Shopify Partner
31 3 7

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"
            }
        }
    ]
}
rick-shopify
Shopify Staff
9 2 3

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.

To learn more visit the Shopify Help Center or the Community Blog.

CubiGear
Shopify Partner
31 3 7

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

rick-shopify
Shopify Staff
9 2 3

This is an accepted solution.

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

To learn more visit the Shopify Help Center or the Community Blog.

CubiGear
Shopify Partner
31 3 7

Bingo.  Big thanx !