Unable to create metafields through graphql

Solved
monika-mni
New Member
4 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"
      }
    }
  ]
}

 

 

0 Likes
swalkinshaw
Shopify Staff
Shopify Staff
8 2 2

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

0 Likes
monika-mni
New Member
4 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.

 

0 Likes
swalkinshaw
Shopify Staff
Shopify Staff
8 2 2

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
          }
        }
      }
    }
  }
}

 

monika-mni
New Member
4 0 0

Thank you @swalkinshaw 

It works perfectly fine for me. 

0 Likes