Create/update metafields on the Shop object

Solved
Richard687
Tourist
8 0 1

I'm trying (and failing) to use the GraphQL API to add/update a private metafield on the Shop object using the privateMetafieldUpsert mutation.

I can get this to work fine on a Product object, but when it is given the ID of a Shop object it fails with the error "invalid id".

For example...

Mutation:

mutation ($input: PrivateMetafieldInput!) {
  privateMetafieldUpsert(input: $input) {
    privateMetafield {
      namespace
      key
      value
    }
    userErrors {
      field
      message
    }
  }
}

 

Variables: (shop ID has been changed)

{
  "input": {
    "owner": "gid://shopify/Shop/99999999",
    "namespace": "abc",
    "key": "xyz",
    "valueInput": {
      "value": "{}",
      "valueType": "JSON_STRING"
    }
  }
}

 

Result:

{
  "data": {
    "privateMetafieldUpsert": null
  },
  "errors": [
    {
      "message": "invalid id",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "privateMetafieldUpsert"
      ]
    }
  ],
  "extensions": {
    "cost": {
      "requestedQueryCost": 10,
      "actualQueryCost": 1,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 999,
        "restoreRate": 50
      }
    }
  }
}

 

Does anyone know why this is failing?

0 Likes
mkcny
Shopify Staff
Shopify Staff
1 1 2

This is an accepted solution.

Hi, Richard687.

This is not the most obvious thing, but you can create a private metafield on a shop simply by omitting the owner. It will default to the current shop as the owner.

Hope that helps.

Richard687
Tourist
8 0 1

Many thanks. That worked nicely.

0 Likes