Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
Hi All,
Has anyone got a working example of how to add new Validation list options to a metafield via GQL?
e.g.
I have a defined metafield for a product that has pre-defined colours options Red, Green, Blue.
If via GQL I try to add a product with the colour Pink, I get a validation error:
Value does not exist in provided choices: ["Red", "Green","Blue"].
However I can't find a GQL query to add the value to the validation list prior to the product meta creation.
I've tried the query:
mutation metafieldDefinitionUpdate(\$definition: MetafieldDefinitionUpdateInput!) {
metafieldDefinitionUpdate(definition: \$definition) {
updatedDefinition {
id
name
}
userErrors {
field
message
}
}
}
But the `MetafieldDefinitionUpdateInput` doesn't seem to accept the validation list?
Any help would be appreciated.
Andy
hello there
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:
query {
metafieldDefinition(namespace: "my_namespace", key: "my_key") {
id
namespace
key
valueType
validation {
rules {
... on MetafieldValidationRulesEnumOneOf {
oneOf
}
}
}
}
}
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:
query { metafieldDefinition(namespace: "my_namespace", key: "my_key") { id namespace key valueType validation { rules { ... on MetafieldValidationRulesEnumOneOf { oneOf } } } } }
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:
mutation {
metafieldDefinitionUpdate(
definition: {
namespace: "my_namespace"
key: "my_key"
validation: {
rules: [
{ oneOf: ["Red", "Green", "Blue", "Pink"] }
]
}
}
) {
updatedDefinition {
id
namespace
key
valueType
validation {
rules {
... on MetafieldValidationRulesEnumOneOf {
oneOf
}
}
}
}
}
}
If this fixed your issue, likes and accepting as a solution are highly appreciated
| Build an online presence with our custom-built Shopify Theme: EcomifyTheme
| Check out our reviews: Trustpilot Reviews
| We are Shopify Partners: EcomGraduates Shopify Partner
I tried your method but when I tried the query
query {
metafieldDefinition(namespace: "my_namespace", key: "my_key") {
id
namespace
key
valueType
validation {
rules {
... on MetafieldValidationRulesEnumOneOf {
oneOf
}
}
}
}
}
I got an error like this "message":"Field 'metafieldDefinition' is missing required arguments: id". So I updated like this:
query = """
query {
metafieldDefinition(id: "gid://shopify/MetafieldDefinition/{metafield_id}") {
id
namespace
key
validations {
rules {
oneOf
}
}
}
}
"""
getting error like this:
"message":"Field 'rules' doesn't exist on type 'MetafieldDefinitionValidation'"
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:
{
"definition": {
"ownerType": "PRODUCT",
"namespace": "testing",
"key": "test",
"validations": [
{
"name": "choices",
"value": "[\"Red\", \"Green\", \"Blue\", \"Pink\",\"Purple\"]"
}
]
}
}
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.
Al | Shopify Developer Support
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
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"
}
]
}
}
]
}
I am new to GraphQL and Thank you for your help.
Hi all, Just linking this here https://shopify.dev/changelog/update-validations-of-a-metafield-definition it can be the case that not all features available in GraphQL API are available in REST API , (and vice versa). It appears that this addition to the API is just in GraphQL for now.
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
I got an error as Page Not Found when I checked your link.
Sorry for the confusion @sandeepks23,
There was some trailing punctuation that broke the link, but it should work now.
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Got it thanks.
@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))
The above issue got resolved when we used the API version 2023-04, initially, I was using the 2023-01 version.