createOptions - productOptionsCreate

Hi,
i am using the “createOptions - productOptionsCreate” to add a new variant to my product. My setup looks like in the attached picutures.

I am facing continuously the problem, that the creation of the new option with one variant is not working. I double checked the right metaobject-id several times and tried different ways. Can you confirm that this function is working as expected from shopify end? What can be a problem on my side?

“At least one value for the option linked to the ‘shopify.color-pattern’ metafield is invalid”


Code:

import streamlit as st
import json
import shopify
from dotenv import load_dotenv
from config.config import load_config

load_dotenv()
config = load_config()


# === Shopify mutation ===
def create_options(product_id, option_name, namespace, key, metaobject_ids):
    mutation = '''
    mutation createOptions($productId: ID!, $options: [OptionCreateInput!]!) {
      productOptionsCreate(productId: $productId, options: $options) {
        userErrors {
          field
          message
          code
        }
        product {
          options {
            name
            linkedMetafield {
              namespace
              key
            }
            optionValues {
              name
              linkedMetafieldValue
            }
          }
        }
      }
    }
    '''
    variables = {
        "productId": product_id,
        "options": [
            {
                "name": option_name,
                "linkedMetafield": {
                    "namespace": namespace,
                    "key": key,
                    "values": metaobject_ids
                }
            }
        ]
    }

    return json.loads(shopify.GraphQL().execute(mutation, variables))

# === Streamlit UI ===
st.title("🧪 Test: Create Product Option with Linked Metafield")

product_id = st.text_input("Product ID (GID)", value="gid://shopify/Product/xzy")
option_name = st.text_input("Option Name", value="Color")
namespace = st.text_input("Metafield Namespace", value="shopify")
key = st.text_input("Metafield Key", value="color-pattern")
metaobject_raw = st.text_area("Metaobject GIDs (one per line)", height=150, value="""gid://shopify/Metaobject/971662503
gid://shopify/Metaobject/971662504
gid://shopify/Metaobject/971662505""")

if st.button("Create Option"):
    metaobject_ids = [line.strip() for line in metaobject_raw.splitlines() if line.strip()]

    if not product_id or not option_name or not metaobject_ids:
        st.error("Please fill out all required fields.")
    else:
        try:
            config = load_config()
            with shopify.Session.temp(config["GRAPHQL_ENDPOINT"], config["GRAPHQL_VER"], config["API_TOKEN"]):
                result = create_options(product_id, option_name, namespace, key, metaobject_ids)

                if result.get("errors"):
                    st.error("❌ GraphQL Error")
                    st.json(result["errors"])
                else:
                    response_data = result.get("data", {}).get("productOptionsCreate", {})
                    if response_data.get("userErrors"):
                        st.error("❌ User Errors")
                        st.json(response_data["userErrors"])
                    else:
                        st.success("✅ Option Created Successfully")
                        st.json(response_data["product"]["options"])

        except Exception as e:
            st.error(f"❌ Exception: {e}")

Hey @user135 , would the solution offered here work?