How do I retrieve a shop metafield?

Topic summary

Issue: Difficulty retrieving a shop metafield via Shopify’s Admin GraphQL after successfully writing it.

Context:

  • Author can create a shop-level metafield using the metafieldsSet mutation with variables: key, namespace, ownerId (shopId), type: single_line_text_field, value (apiKey).
  • Wants to read the same metafield but is unsure of the correct GraphQL query syntax.

Attempted read:

  • Query tries: shop(ownerId: “”) { metafield(namespace: “…”, key: “…”) { id value } }
  • Unclear on whether shop(ownerId: …) is valid and how to properly access the metafield by namespace/key.

Additional note:

  • Considered storing against the App Installation instead of Shop but couldn’t find documentation.

Status:

  • Request for guidance on correct read syntax; no solution or responses yet.

Artifacts central to understanding:

  • Code snippets for the mutation (metafieldsSet) and the attempted query (shop + metafield by namespace/key).
Summarized with AI on December 28. AI used: gpt-5.

I’m new to using GraphQL and was hoping to get some guidnance.

I’ve managed to write to the shop metafields using this query (I’m storing it against the shop as it’s unique to that url, although I did read you can install agains the app installation but couldn’t find any docs on that):

const metaResponse = await admin.graphql(
  `#graphql
    mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {
      metafieldsSet(metafields: $metafields) {
        metafields {
          key
          namespace
          value
          createdAt
          updatedAt
        }
        userErrors {
          field
          message
          code
        }
      }
    }`,
    {
      variables: {
        "metafields": [
          {
            "key": metafieldKey,
            "namespace": metafieldNamespace,
            "ownerId": shopId,
            "type": "single_line_text_field",
            "value": apiKey
          }
        ]
      },
    },
  );

Where OwnerID is the shop you’re interacting with in the admin interface.

But then trying to read it back, I can’t figure out the syntax:

const queryData = `
      {
        shop(ownerId: "` + shopId + `") {
          metafield(namespace: "` + metafieldNamespace + `", key: "` + metafieldKey + `") {
            id
            value
          }
        }
      }
    `;

Can anyone help untangle me please.

Thanks

Greg