Metafield not returned in 'metafields' query

I’m using a demo development store to work on some discounting functions. I’d like to use shop metafields to hold discounting config that the functions can use.

The demo store has a pre-set metafield, with the key ‘alpine_sports’ - this is returned if I use the query

query testShopMetafields {
  shop {
    metafields(first:1) {
      nodes {
        type
        id
        key
        value
      }
    }
  }
}
{
  "data": {
    "shop": {
      "metafields": {
        "nodes": [
          {
            "type": "list.single_line_text_field",
            "id": "gid://shopify/Metafield/48202321953112",
            "key": "alpine_sports",
            "value": "[\"snowboarding\"]"
          }
        ]
      }
    }
  },

However, if I use a direct query using the key

query testShopMetafields {
  shop {
    metafield(key: "alpine_sports") {
        type
        id
        key
        value
    }
  }
}

It returns nothing:

{
  "data": {
    "shop": {
      "metafield": null
    }
  }

Is this expected behaviour? If so, why? I have found that it also applies in the reverse; that is, if I create a metafield on the shop I can directly query it, but it isn’t listed as one of the shop’s metafields in the ‘metafields’ collection query response.

I’m guessing there are perhaps some permissions and scope issues at play, but for these two queries to have such different results is confusing, to say the least.

Didn’t read correctly. The post here can be ignored. -.-

Further to this, the shop object API reference doesn’t actually list metafields as a query field

https://shopify.dev/docs/api/admin-graphql/2024-07/queries/shop

So what gives? Should this field even exist to query on the shop model? Is the metafield actually elsewhere? Is the field is connected to the wrong interface? Is this a bug?

The issue is the namespace of the metafield being queried and created.

If the namespace is omitted from the metafield query, it will default to the app-reserved namespace (as per the docs). So it will only look for metafields in that namespace when using the metafield query field. So this I realise is intended behaviour.

HOWEVER - when using the metafields connection field, it will return metafields in both the app-reserved namespace and other (in this case the ‘test_data’) namespaces. It is worth noting that it will NOT return metafields created in other app-reserved namespaces, even if you query them directly by key and namespace.

This still doesn’t really explain why the collection field is not described in the docs, but whatever.

HTH others with the same issue.