I’m currently trying to use the GraphQL API to add a metafield to a product image, but I’m pretty sure I’m running into a bug, since the error message I’m getting doesn’t look right.
All queries and mutations were executed using the Shopify GraphiQL App with API version 2023-04 (latest).
I previously created a new product image by using the productCreateMedia mutation which works fine and I’m able to query all images/media of a product, so I also have the required id(s) to create the metafield.
Query:
query imagesByProductId($id: ID!) {
product(id: $id) {
media(first: 3) {
edges {
node {
... on MediaImage {
id
status
image {
id
altText
url
metafield(key: "somekey", namespace: "image") {
namespace
key
value
}
}
}
}
}
}
images(first: 3) {
nodes {
id
metafield(key: "somekey", namespace: "image") {
id
}
}
}
}
}
Response:
{
"data": {
"product": {
"media": {
"edges": [
{
"node": {
"id": "gid://shopify/MediaImage/33642354213165",
"status": "READY",
"image": {
"id": "gid://shopify/ImageSource/33652035027245",
"altText": "",
"url": "https://cdn.shopify.com/[pathToFile]",
"metafield": null
}
}
},
{
"node": {
"id": "gid://shopify/MediaImage/33642354245933",
"status": "READY",
"image": {
"id": "gid://shopify/ImageSource/33652035060013",
"altText": "",
"url": "url": "https://cdn.shopify.com/[pathToFile]",
"metafield": null
}
}
},
{
"node": {
"id": "gid://shopify/MediaImage/33642354278701",
"status": "READY",
"image": {
"id": "gid://shopify/ImageSource/33652035092781",
"altText": "",
"url": "url": "https://cdn.shopify.com/[pathToFile]",
"metafield": null
}
}
}
]
},
"images": {
"nodes": [
{
"id": "gid://shopify/ProductImage/41289660956973",
"metafield": null
},
{
"id": "gid://shopify/ProductImage/41289661055277",
"metafield": null
},
{
"id": "gid://shopify/ProductImage/41289661251885",
"metafield": null
}
]
}
}
},
"extensions": {
"cost": {
"requestedQueryCost": 20,
"actualQueryCost": 18,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 982,
"restoreRate": 50
}
}
}
}
(Although I have to admit it’s a little confusing to have multiple ids for a single item, but only being allowed to use one of them for metafield.)
So for this example I’m using an image represented by the following ids:
- gid://shopify/MediaImage/33642354213165
- gid://shopify/ImageSource/33652035027245
- gid://shopify/ProductImage/41289660956973
Now I want to use the metafieldsSet mutation with the variables provided below, but no matter which ownerId I use I always get the same error in the response.I was able to figure out that only the ProductImage id is the correct one to use here, but I still run into this issue.
Mutation:
mutation addMetafieldToImage($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
id
owner {
... on Image {
id
url
}
}
ownerType
type
value
}
userErrors {
code
elementIndex
field
message
}
}
}
Variables:
{
"metafields": [
{
"ownerId": "gid://shopify/ProductImage/41289660956973",
"type": "single_line_text_field",
"namespace": "image",
"key": "somekey",
"value": "testme"
}
]
}
Response:
{
"data": {
"metafieldsSet": null
},
"errors": [
{
"message": "invalid id",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"metafieldsSet"
]
}
],
"extensions": {
"cost": {
"requestedQueryCost": 11,
"actualQueryCost": 1,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 999,
"restoreRate": 50
}
}
}
}
So this is really a bug with the GraphQL API, right?