API/GraphQL and creating a Metaobject with a product reference list

API/GraphQL and creating a Metaobject with a product reference list

rdykes0715
New Member
8 0 0

Hi everyone, I would appreciate any help with my problem. I created a metaobject definition named Age Grouping. This definition has two fields:

 

age_grouping.group_name - A text value that simply names the metaobject, single line text

age_grouping.products - a list of products

 

I would like to get to the point where I am using a python script and the API to bulk create these metaobjects with a CSV. For now, I am hard coding the values into the code so that I can just create one test metaobject Age Group. The Age Group metaobject I created does have storefronts access turned on, which includes the Storefront API.

 

I am able to create the metaobject no problem when it only has the Group Name value filled in and I make the products value empty/optional. But I am very new to all this and cannot figure out how to pass in a list of products for the product list metafield.

 

Here is the meat of what I am trying (I've tried different things, this is the latest, it complained it's not a string) putting in fake product ID values for the purposes of this post:

 
def create_metaobject():
    # GraphQL mutation
    mutation = """
    mutation {
        metaobjectCreate(metaobject: {
            type: "age_grouping",
            fields: [
                {
                    key: "group_name",
                    value: "Test Age Group"
                },
                {
                    key: "products",
                    type: "list.product_reference",
                    references: [
                        { reference: "gid://shopify/Product/1111111111111111" },
                        { reference: "gid://shopify/Product/2222222222222" },
                        { reference: "gid://shopify/Product/333333333333" },
                        { reference: "gid://shopify/Product/444444444444" }
                    ]
                }
            ]
        }) {
            metaobject {
                id
                type
            }
            userErrors {
                field
                message
            }
        }
    }
    """
 
Thank you for any help and again I'm new to this area.

 

Replies 0 (0)