To add a new validation list option to a metafield via GQL, you need to first retrieve the metafield definition using the metafieldDefinition query, then update the validation list using the metafieldDefinitionUpdate mutation.
Here’s an example query that demonstrates how to add a new validation list option to a metafield:
To add a new validation list option to a metafield via GQL, you need to first retrieve the metafield definition using the metafieldDefinition query, then update the validation list using the metafieldDefinitionUpdate mutation.
Here’s an example query that demonstrates how to add a new validation list option to a metafield:
This query retrieves the metafield definition for a metafield with namespace my_namespace and key my_key, and includes the validation rules for the metafield.
To add a new option to the validation list, you can update the oneOf array in the validation rules and then use the metafieldDefinitionUpdate mutation to update the metafield definition:
Hey @sandeepks23 - thanks for getting in touch. I wanted to reach out to share some clarifications and to share how the update mutation should be structured based on our dev docs for validation types. Since you’re wanting to add an additional colour choice as a validation option, we’d want to use the “choices” validator option type. Here’s the mutation:
mutation UpdateMetafieldDefinition($definition: MetafieldDefinitionUpdateInput!) {
metafieldDefinitionUpdate(definition: $definition) {
updatedDefinition {
id
name
validations {
name
type
value
}
}
userErrors {
field
message
}
}
}
And here are the variables:
The validations do require those specific name/value pairs and you’ll have to know some more information about the metafield definition you’d like to update validations for, but I can confirm the structure of the mutation should work if you follow the example above. If you haven’t checked it out yet, I’d also recommend taking a look here at our documentation on managing validations directly.
Hope this helps - please let me know if I can clarify anything on our end here.
I have created a metafield definition with choices [“Trivandrum”,“Kochi”]. I am successfully able to achieve this. But later I want to update this metafield definition. For example, add a new option “Alapuzha” to that array so I will be able to choose out of these three values. So this is my python code and I have used the query you mentioned.
import requests
import json
# Shopify store details
shopify_store = "burfis-shop.myshopify.com"
access_token = "token"
# GraphQL API endpoint
api_url = f"https://{shopify_store}/admin/api/2023-01/graphql.json"
# GraphQL mutation code
mutation_code = '''
mutation UpdateMetafieldDefinition($definition: MetafieldDefinitionUpdateInput!) {
metafieldDefinitionUpdate(definition: $definition) {
updatedDefinition {
id
name
validations {
name
type
value
}
}
userErrors {
field
message
}
}
}
'''
# Variables for the mutation
variables = {
"definition": {
"ownerType": "PRODUCT",
"namespace": "location",
"key": "product_store_locations",
"validations": [
{
"name": "choices",
"value": "[\"Red\", \"Green\", \"Blue\", \"Pink\",\"Purple\"]"
}
]
}
}
# Create the request payload
payload = {
"query": mutation_code,
"variables": variables
}
# Send the GraphQL request
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": access_token
}
response = requests.post(api_url, json=payload, headers=headers)
# Process the response
if response.status_code == 200:
data = response.json()
print(json.dumps(data, indent=2))
else:
print(f"Request failed with status code {response.status_code}: {response.text}")
and the error I am getting is
{
"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": "location",
"key": "product_store_locations",
"validations": [
{
"name": "choices",
"value": "[\"Red\", \"Green\", \"Blue\", \"Pink\",\"Purple\"]"
}
]
},
"problems": [
{
"path": [
"validations"
],
"explanation": "Field is not defined on MetafieldDefinitionUpdateInput"
}
]
}
}
]
}
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"
}
]
}
}
]
}