Re: Metaobjects association

Solved

Metaobjects association

Meltin-Bit
Shopify Partner
27 0 10

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.

Meltin Bit
Accepted Solutions (2)

ShopifyDevSup
Shopify Staff
1453 238 511

This is an accepted solution.

Hi @Meltin-Bit 👋

 

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 `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!

 

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

View solution in original post

ShopifyDevSup
Shopify Staff
1453 238 511

This is an accepted solution.

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!

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

View solution in original post

Replies 3 (3)

ShopifyDevSup
Shopify Staff
1453 238 511

This is an accepted solution.

Hi @Meltin-Bit 👋

 

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 `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!

 

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

Meltin-Bit
Shopify Partner
27 0 10

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

Meltin Bit
ShopifyDevSup
Shopify Staff
1453 238 511

This is an accepted solution.

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!

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