What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Unable to create metafields through graphql

Solved

Unable to create metafields through graphql

monika-mni
New Member
5 0 0

I am trying to create metafields for one of my stores through Shopify GraphiQL App and Postman both, following the same syntax from given documentation: https://shopify.dev/tutorials/manage-metafields-with-graphql-admin-api but getting error. Can anyone please here.

Request:

 

mutation{
  productUpdate(input: {id: "gid://shopify/Product/<product_id>", metafields: [{
        namespace: "instructions",
        key: "wash",
        valueInput: {
          value: "cold wash",
          valueType: "STRING"
        }
        
      }
    ]} ) {
    product {
      metafields(first: 100) {
        edges {
          node {
            id
            namespace
            key
            value
          }
        }
      }
    }
  }
}

 

 

Response:

 

{
  "errors": [
    {
      "message": "InputObject 'MetafieldInput' doesn't accept argument 'valueInput'",
      "locations": [
        {
          "line": 5,
          "column": 9
        }
      ],
      "path": [
        "mutation",
        "productUpdate",
        "input",
        "metafields",
        0,
        "valueInput"
      ],
      "extensions": {
        "code": "argumentNotAccepted",
        "name": "MetafieldInput",
        "typeName": "InputObject",
        "argumentName": "valueInput"
      }
    }
  ]
}

 

 

Accepted Solution (1)
swalkinshaw
Shopify Staff
20 4 12

This is an accepted solution.

Your input variables have "STRING" as a string value, but the valueType input field has a type of MetafieldValueType which is an enum type. To specify an enum value, it's just STRING (the unquoted "bare" value) like this:

input: {id: "gid://shopify/Product/<product_id>", metafields: [{
    namespace: "instructions",
    key: "wash",
    value: "cold wash",
    valueType: STRING
  }
]}

That should fix the issue and get your mutation working properly. 

Note: Enum values in arguments/inputs are a bit confusing in GraphQL because whether you specify them as strings or unquoted bare values depends on how you're calling the mutation. If you're specifying the input inline (directly within the mutation document) as you've done in your example, then it's just STRING. However, if you're specifying input values separately as JSON variables, then they need to be quoted as strings (because it wouldn't be valid JSON otherwise). Our tutorial specifies the input in variables which is why they're quoted strings.

 

It runs successfully without any error but metafield haven't created and not getting in get list.

The mutation returned a validation error but because you didn't select the userErrors field, you aren't seeing the errors. See https://shopify.dev/concepts/graphql/mutations#return-fields 

Your mutation should look like this now:

mutation{
  productUpdate(input: {id: "gid://shopify/Product/<product_id>", metafields: [{
        namespace: "instructions",
        key: "wash",
        valueInput: {
          value: "cold wash",
          valueType: STRING
        }
      }
    ]} ) {
    userErrors {
      field
      message
    }
    product {
      metafields(first: 100) {
        edges {
          node {
            id
            namespace
            key
            value
          }
        }
      }
    }
  }
}

 

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 5 (5)

swalkinshaw
Shopify Staff
20 4 12

`valueInput` only exists for private metafields, not normal metafields which is a mistake in the tutorial. Your mutation (and input) should look like this:

 

mutation{
  productUpdate(input: {id: "gid://shopify/Product/<product_id>", metafields: [{
      namespace: "instructions",
      key: "wash",
      value: "cold wash",
      valueType: "STRING"   
    }
  ]} ) {
    product {
      metafields(first: 100) {
        edges {
          node {
            id
            namespace
            key
            value
          }
        }
      }
    }
  }
}

 

You can see the input type docs for more details:

Metafield input: https://shopify.dev/docs/admin-api/graphql/reference/metafields/metafieldinput

Private metafield input: https://shopify.dev/docs/admin-api/graphql/reference/metafields/privatemetafieldinput

 

Unfortunately these two aren't consistent which is annoying and unexpected. I'll make sure the tutorial gets updated, thanks for finding this bug.

To learn more visit the Shopify Help Center or the Community Blog.

monika-mni
New Member
5 0 0

Thanks @swalkinshaw  for the quick reply.

As you mentioned different syntax i tried that but still getting error.

First scenario, i added value and valueType in input like:

input: {id: "gid://shopify/Product/<product_id>", metafields: [{
         namespace: "instructions",
        key: "wash",
        value: "cold wash",
        valueType: "STRING" 
      }
    ]}

 but getting error:

"message": "Argument 'valueType' on InputObject 'MetafieldInput' has an invalid value (\"STRING\"). Expected type 'MetafieldValueType'.",

Second Scenario: i removed valueType and run it

input: {id: "gid://shopify/Product/<product_id>", metafields: [{
         namespace: "instructions",
        key: "wash",
        value: "cold wash"
      }
    ]}

It runs successfully without any error but metafield haven't created and not getting in get list.

 

swalkinshaw
Shopify Staff
20 4 12

This is an accepted solution.

Your input variables have "STRING" as a string value, but the valueType input field has a type of MetafieldValueType which is an enum type. To specify an enum value, it's just STRING (the unquoted "bare" value) like this:

input: {id: "gid://shopify/Product/<product_id>", metafields: [{
    namespace: "instructions",
    key: "wash",
    value: "cold wash",
    valueType: STRING
  }
]}

That should fix the issue and get your mutation working properly. 

Note: Enum values in arguments/inputs are a bit confusing in GraphQL because whether you specify them as strings or unquoted bare values depends on how you're calling the mutation. If you're specifying the input inline (directly within the mutation document) as you've done in your example, then it's just STRING. However, if you're specifying input values separately as JSON variables, then they need to be quoted as strings (because it wouldn't be valid JSON otherwise). Our tutorial specifies the input in variables which is why they're quoted strings.

 

It runs successfully without any error but metafield haven't created and not getting in get list.

The mutation returned a validation error but because you didn't select the userErrors field, you aren't seeing the errors. See https://shopify.dev/concepts/graphql/mutations#return-fields 

Your mutation should look like this now:

mutation{
  productUpdate(input: {id: "gid://shopify/Product/<product_id>", metafields: [{
        namespace: "instructions",
        key: "wash",
        valueInput: {
          value: "cold wash",
          valueType: STRING
        }
      }
    ]} ) {
    userErrors {
      field
      message
    }
    product {
      metafields(first: 100) {
        edges {
          node {
            id
            namespace
            key
            value
          }
        }
      }
    }
  }
}

 

To learn more visit the Shopify Help Center or the Community Blog.

monika-mni
New Member
5 0 0

Thank you @swalkinshaw 

It works perfectly fine for me. 

jam_chan
Shopify Partner
927 23 190

I'm trying to create a new product variant following your query. But it gave me an error:

result = {'errors': [{'message': "InputObject 'MetafieldInput' doesn't accept argument 'valueInput'", 'locations': [{'line': 16, 'column': 15}], 'path': ['mutation productVariantCreate', 'productVariantCreate', 'input', 'metafields', 0, 'valueInput'], 'extensions': {'code': 'argumentNotAccepted', 'name': 'MetafieldInput', 'typeName': 'InputObject', 'argumentName': 'valueInput'}}]}

 

Also, the API doc doesn't have this valueInput field. 

My query:

query = f"""mutation productVariantCreate {{
      productVariantCreate(input: {{
          options: [\"{get_variant_option_name(bundleID)}\", \"1\"],
          price: \"{price:.2f}\",
          inventoryPolicy: CONTINUE,
          weight: {weight},
          weightUnit: {weight_unit},
          productId: \"gid://shopify/Product/{productId}\",
          inventoryQuantities: [{{
              locationId: \"{locationId}\",
              availableQuantity: {quantity}
          }}],
          metafields: [{{
              key: \"selectedVariants\",
              namespace: \"{SHOPIFY_METAFIELD_NAMESPACE}\",
              valueInput: {{
                  value: \"{str([(dict.get('variantID'), dict.get('quantity')) for dict in variant_dicts])}\",
                  valueType: STRING
              }}
          }}]
      }}) {{
        product {{
          id
        }}
        productVariant {{
          id
        }}
        userErrors {{
          field
          message
        }}
      }}
    }}"""
BYOB - Build Your Own Bundles, SPO - SEO App to research keywords & edit social link preview