Create custom metafield that references a metaobject

Hi,

I am trying to create a custom meta field that references a meta object.

Mutation

mutation metafieldDefinitionCreate($definition: MetafieldDefinitionInput!) {
  metafieldDefinitionCreate(definition: $definition) {
    createdDefinition {
      id
      name
      namespace
      validations {
        name
        value
      }
      type {
        name
      }
    }

    userErrors {
      field
      message
    }
  }
}

Variables

{
  "definition": {
    "description": "Test",
    "key": "product_bundle2",
    "name": "product_bundle2",
    "namespace": "custom",
    "ownerType": "PAGE",
    "pin": true,
    "type": "metaobject_reference",
    "useAsCollectionCondition": false,
    "visibleToStorefrontApi": true,
    "validations": {
      "name": "metaobject_definition_id",
      "value": "gid://shopify/Metaobject/13335560507"
    }
  }
}

Error

A validation for setting the metaobject definition a reference must point to.

Example use case:

A product refers to a “Designer” metaobject.

"userErrors": [
        {
          "field": [
            "definition",
            "validations"
          ],
          "message": "Validations require that you select a metaobject."
        }
      ]

The documentation for validation https://shopify.dev/docs/apps/custom-data/metafields/definitions/validation says that:

A validation for setting the metaobject definition a reference must point to.

Example use case:

A product refers to a “Designer” metaobject.










<br>validations: <br> name: metaobject_definition_id,<br> value: "gid://shopify/MetaobjectDefinition/123"<br>









I am confused because I am referencing the object in the validation, the object exists. What field is supposed to be passed to reference the metaObject.

Any chance that anyone has a solution to this problem? I am still stuck.

Just had this issue – it seemed to be because there were no entries within the definition I was trying to reference. Once I added a dummy entry, creating the definition worked (though I was using the admin interface and not the GraphQL API).

Hey @Giltee !

Just taking a look here, I see in your example you’re using a metaobject GID in your validations. What you need here instead is the MetaobjectDefinition id.

To find that for a specific metaobject, you can query the MetafieldDefinitionValidation and the definition ID will be returned in the value field.

Using that ID instead will get you past that validations error!

Hope it helps.

  • Kyle G.
1 Like

I also got stuck with the same issue and I used very similar inputs as yours. The main issue is that we’re using a MetaObject id in the validation when the API is looking for a MetaobjectDefinition (easy to mix the two up when reading the docs).

So instead of gid://shopify/Metaobject/13335560507 use something like gid://shopify/MetaobjectDefinition/xxxxxxxxx where the Metaobject definition id can be fetched using the GraphQL API. It’s also mentioned in /settings/custom_data in the store settings if you inspect the row created for the metaobject definition in question (screenshot below).

I was able to create a metafield in this exact fashion without having a metaobject created in advance. But I used the GraphQL API.