Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
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"
}
}
]
}
Solved! Go to the solution
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.
`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.
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.
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.
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
}}
}}
}}"""