Why am I getting an error when creating a json type metafield?

Solved

Why am I getting an error when creating a json type metafield?

wil_dev
Shopify Partner
12 0 2

Hi all!

I'v a problem when I try to create a metafield of type json:

const value = {"test":"test"}
      const response = await admin.graphql(
        `#graphql
          mutation CreateAppDataMetafield($metafieldsSetInput:[MetafieldsSetInput!]!) {
            metafieldsSet(metafields: $metafieldsSetInput) {
              metafields {
              id
              namespace
              key
            }
            userErrors {
              field
              message
            }
            }
          }
        `,
        {
          variables: {
            metafieldsSetInput: {
              namespace: "block_metafields",
              key: "test",
              type: "rich_text_field",
              value: JSON.stringify(value),
              ownerId: data.get('id')
            }
          }
        }
      )
    const metafield = await response.json()
    console.log(metafield.data.metafieldsSet.userErrors)

The console:

 

[
20:57:06 │ remix    │   {
20:57:06 │ remix    │     field: [ 'metafields', '0', 'value' ],
20:57:06 │ remix    │     message: 'Value is not the correct format: 
failed schema "test" is not a permitted key.'
20:57:06 │ remix    │   }
20:57:06 │ remix    │ ]

 

 I tried with other keys, it doesn't work... where is the error?

Thanks!

Accepted Solution (1)

SomeUsernameHe
Shopify Partner
519 57 110

This is an accepted solution.

If you are trying to create "a metafield of type json" then your

 type: "rich_text_field",

 should be

 type: "json",


But if it doesn't fix the error, the error message failed schema "test" is not a permitted key suggests that the key "test" may not be allowed under the schema of the metafield or that the API call doesn't have the correct permissions to set a metafield with this key. Ensure that the key is valid and permitted under your current API permissions and the metafield definitions in Shopify.

https://shopify.dev/docs/apps/custom-data/metafields/types

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee

View solution in original post

Replies 2 (2)

SomeUsernameHe
Shopify Partner
519 57 110

This is an accepted solution.

If you are trying to create "a metafield of type json" then your

 type: "rich_text_field",

 should be

 type: "json",


But if it doesn't fix the error, the error message failed schema "test" is not a permitted key suggests that the key "test" may not be allowed under the schema of the metafield or that the API call doesn't have the correct permissions to set a metafield with this key. Ensure that the key is valid and permitted under your current API permissions and the metafield definitions in Shopify.

https://shopify.dev/docs/apps/custom-data/metafields/types

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
wil_dev
Shopify Partner
12 0 2

Thank you so much!