Solved

Get private metafield for shop

matiasturunen
Tourist
8 2 3

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)

danloomer
Shopify Staff
11 5 4

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`

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

View solution in original post

Replies 3 (3)

danloomer
Shopify Staff
11 5 4

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`

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

matiasturunen
Tourist
8 2 3

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

danloomer
Shopify Staff
11 5 4

Hi @matiasturunen 

 

There is a top level query `privateMetafield` which lets you get a single private metafield by ID:
https://shopify.dev/api/admin-graphql/2021-10/queries/privateMetafield

 

but if you want to get a specific namespace and key you have to go through the resource (Product, Shop, Customer, etc). That query is documented here:

https://shopify.dev/api/admin-graphql/2021-10/objects/PrivateMetafield#top

You can see what types support it, and what arguments you can pass.

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