Updating Metafiled Validation Options via API?

Topic summary

Goal: Add new allowed values to a product metafield’s predefined list via Shopify API without triggering validation errors.

Key points:

  • The correct approach is a GraphQL mutation metafieldDefinitionUpdate using the validations field with the “choices” validator. The value must be a JSON-encoded array of allowed options (e.g., [“Red”,“Green”,“Blue”,“Pink”]).
  • Earlier examples using validation.rules.oneOf and querying by namespace/key led to errors: metafieldDefinition requires id, and the schema does not expose rules on MetafieldDefinitionValidation; the correct field is validations.

Version/API scope:

  • The validations field on MetafieldDefinitionUpdateInput is available in GraphQL Admin API version 2023-04 and later. It is not present in 2023-01, which caused the “Field is not defined” error.
  • This capability is GraphQL-only (not REST). The Shopify GraphiQL app worked because it targeted 2023-04.

Resolution/Outcome:

  • Switching the endpoint to GraphQL Admin API 2023-04 resolved the issue, and updating choices via validations works as expected.

Terms:

  • Metafield: custom data field on resources like products.
  • Choices validator: restricts a metafield’s value to a specified list.
Summarized with AI on January 25. AI used: gpt-5.

@ShopifyDevSup For GraphSQL Explorer in Shopify the above query+variable combination is working.

Ie, over here - https://admin.shopify.com/store/{{store-name}}/apps/shopify-graphiql-app.

In the network console, the request is reaching to https://shopify-graphiql-app.shopifycloud.com/admin/api/2023-04/graphql. And the update is happening correctly.

But when we use the Rest endpoint - https://{{store_name}}.myshopify.com//admin/api/2023-01/graphql.json to send the query and variables. it returns below error

{
  "errors": [
    {
      "message": "Variable $definition of type MetafieldDefinitionUpdateInput! was provided invalid value for validations (Field is not defined on MetafieldDefinitionUpdateInput)",
      "locations": [
        {
          "line": 2,
          "column": 36
        }
      ],
      "extensions": {
        "value": {
          "ownerType": "PRODUCT",
          "namespace": "custom",
          "key": "product_store_locations",
          "validations": [
            {
              "name": "choices",
              "value": "[\"Thiruvananthapuram\"]"
            }
          ]
        },
        "problems": [
          {
            "path": [
              "validations"
            ],
            "explanation": "Field is not defined on MetafieldDefinitionUpdateInput"
          }
        ]
      }
    }
  ]
}

The same error when we using shopify_python_api

import shopify
session = shopify.Session(SHOP_URL, api_version, access_token)
shopify.ShopifyResource.activate_session(session)

query = """
mutation UpdateMetafieldDefinition($definition: MetafieldDefinitionUpdateInput!) {
 metafieldDefinitionUpdate(definition: $definition) {
   updatedDefinition {
     id
     name
     validations {
       name
       type
       value
     }
   }
   userErrors {
     field
     message
   }
 }
}
"""

variables = {
    "definition": {
        "ownerType": "PRODUCT",
        "namespace": "custom",
        "key": "product_store_locations",
        "validations": [
            {
                "name": "choices",
                "value": "[\"Thiruvananthapuram\"]"
            }
        ]
    }
}

print(shopify.GraphQL().execute(query=query, variables=variables))
1 Like