Get private metafield for shop

Solved
matiasturunen
Tourist
4 1 2

I'm developing an app that needs to store some shop specific keys somewhere. I figured that Shopify's private metafields wold be optimal for the job. I can successfully create private metafield (which *should* got to shop's metafields as I'm not providing owner property, docs: https://shopify.dev/apps/metafields/private ) I assume that it is a success as I don't get any error messages in return. My question is how to get the values of the metafields I just created? I have tested getting one and all metafields, by providing param for shop and also without it. Also there seems to be some mismatch between docs and api spec, as in api spec the query for private metafield is 

 

{
  privateMetafield(id) {
    # PrivateMetafield fields
  }
}

 

which is different from the one at the docs

 

{
  product(id: "gid://shopify/Product/1") {
    privateMetafield(namespace: "wholesale", key: "wholesale_price") {
      value
    }
  }
}

 

And I want to clarify that I have tested the query above with and without 'product(...)' and by repalcing it with shop.

If it actually is like above, but with shop id, I then would like to know where I can get the ID

Accepted Solution (1)

Accepted Solutions
danloomer
Shopify Staff
5 3 2

This is an accepted solution.

Hi @matiasturunen ,

 

To get the ID of the shop you could use the following query:

query GetShopId {
  shop {
    id
  }
}

 

You might not need to provide the shop ID though, the following query may work:

query GetShopPrivateMetafield {
  shop {
    privateMetafield(namespace:"the_ns", key:"the_key") {
      id
      value
    }
  }
}

 

Notice that I'm not passing an ID to `shop`

View solution in original post

Replies 2 (2)
danloomer
Shopify Staff
5 3 2

This is an accepted solution.

Hi @matiasturunen ,

 

To get the ID of the shop you could use the following query:

query GetShopId {
  shop {
    id
  }
}

 

You might not need to provide the shop ID though, the following query may work:

query GetShopPrivateMetafield {
  shop {
    privateMetafield(namespace:"the_ns", key:"the_key") {
      id
      value
    }
  }
}

 

Notice that I'm not passing an ID to `shop`

View solution in original post

matiasturunen
Tourist
4 1 2

That worked nicely. But it left me to wonder why this query is not mentioned anywhere in documentation?