Metaobjects association

I’m trying the new metaobjects api. After creating a metaobject definition and creating a metaobject is it possible to relation it to a product for example?

F.

Hi @Meltin-Bit :waving_hand:

When creating the metaobjectDefinition, we can set one of the MetaobjectDefinition.fieldDefinitions to hold a product reference by setting the MetafieldDefinitionType.name to “product_reference”. A full list of supported types can be found here.

Below is an example:

mutation ($input: MetaobjectDefinitionCreateInput!) {
    metaobjectDefinitionCreate(definition: $input) {
        metaobjectDefinition {
            id
            type
            fieldDefinitions {
                key
                name
                type {
                    name
                }
            }
        }
    }
}

With variables:

{
    "input" : {
        "type": "$app:product_related_metaobject",
        "access": {
            "admin": "MERCHANT_READ_WRITE",
            "storefront": "PUBLIC_READ"
        },
        "capabilities": {
            "publishable": {
                "enabled": true
            }
        },
        "fieldDefinitions": { 
            "key": "product", 
            "name": "Related Product", 
            "type": "product_reference" 
        }
    }
}

Then to create this product-related metaobject, we can use the ](https://shopify.dev/api/admin-graphql/2023-01/mutations/metaobjectCreate)[metaobjectCreate mutation as follows:

mutation($input: MetaobjectCreateInput!) {
    metaobjectCreate(metaobject: $input) {
        metaobject {
            id
            type 
            field(key: "product") { value }
        }
    }
}

With variables:

{
    "input" : {
        "type": "$app:product_related_metaobject",
        "fields": {
            "key": "product",
            "value": "gid://shopify/Product/6832753737846"
        }
    }
}

Hope that helps!

3 Likes

Thanks, this make sense :slightly_smiling_face: Do you know if metaobjects are available in liquid like metafields? Something like app.metaobjects…? This would be nice inside a theme app extension.

1 Like

Hey Meltin-Bit, great question!

The short answer is yes, metaobjects are available in themes using the Admin API, Liquid, and the Storefront API. This is mentioned in our overview doc here, which is a good place to get an idea of the feature. The doc also links our developer resources for each method of access, and some links to other guides on access, capabilities and metaobject management.

Cheers!

1 Like