How do I create a private metafield on Shopify using Admin-graphql? In their docs, they have given example for retrieving a private metafield by its ID(ref: https://shopify.dev/api/admin-graphql/2022-01/queries/privateMetafield#section-examples), do you have any example for creating private metafield using Node.js?
1 Answer
I use https://www.npmjs.com/package/shopify-api-node, it has support for GraphQL calls. https://insomnia.rest/ has great GraphQL support, with field hinting, query eval etc.
An example of creating a product with a private metafield:
mutation productCreate($input: ProductInput!, $namespace: String!) {
productCreate(input: $input) {
product {
id
bufferProductId: privateMetafield(
key: "your_field_key"
namespace: $namespace
) {
value
}
tags
variants(first: 10) {
edges {
node {
id
sku
}
}
}
}
userErrors {
field
message
}
}
}
You need to pass these variables to the GraphQL query:
{
"input": {
"title": "Your product name",
"options": [
"Size",
"Colour"
],
"handle": "your-product-name",
"tags": [
"Product tag"
],
"vendor": "Lauri's sportin' goods",
"variants": [
{
"sku": "ABC123XL",
"options": [
"Large",
"Blue"
]
},
{
"sku": "ABC123L",
"options": [
"Large",
"Yellow"
]
}
],
"privateMetafields": [
{
"key": "your_field_key",
"namespace": "your_namespace",
"valueInput": {
"value": "Your field value",
"valueType": "STRING"
}
}
]
},
"namespace": "your_namespace"
}
This will return the private metafield value in the product creation response.
A few things that make private metafields different from public ones:
- you can add / update the private metafield over and over using ProductUpdate, whereas
- you can only access the private metafields through GraphQL. AFAIK, they’re not visible anywhere in the Shopify Admin, or using the REST API.
- even with GraphQL, those fields will be visible only to the API key that created them. So, you won’t see them with another Shopify App.
You can use these field types to set the private metafield values.