MetafieldsSet mutation returns "invalid id" for product images

MetafieldsSet mutation returns "invalid id" for product images

jeliebig-tudock
Shopify Partner
3 0 3

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?

Replies 5 (5)

Richard_Monette
Shopify Staff
19 2 7

metafieldsSet should work now if you use GID such as gid://shopify/MediaImage/33642354213165

 

Please let me know if that works, or if you are still having issues, thanks!

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

jeliebig-tudock
Shopify Partner
3 0 3

Sorry for the late reply, the issue still occurs and I receive the same error message as before.

I used a MediaImage GID as you said, but it the API still doesn't accept it.

Richard_Monette
Shopify Staff
19 2 7

On 2023-07 here is what I am doing:

 

To get the GID:

 

query products {
  products(first: 10) {
    edges {
      node {
        media(first: 10) {
          edges {
            node {
              id
            }
          }
        }
      }
    }
  }
}

 

once I've got the GID, I am setting it using:

 

mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
  metafieldsSet(metafields: $metafields) {
    metafields {
      id
    }
    userErrors {
      field
      message
    }
  }
}

{
  "metafields": [
    {
      "key": "foo",
      "namespace": "bar",
      "ownerId": "gid://shopify/MediaImage/29500990455864",
      "type": "string",
      "value": "testing123"
    }
  ]
}

 

What error specifically are you seeing when trying this?

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

jeliebig-tudock
Shopify Partner
3 0 3

API version is 2023-04 (latest)

 

First I use this query to get MediaImage ids:

 

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

 

 I pick the first result which happens to be "gid://shopify/MediaImage/33666068480301" and use that for the next mutation.

 

mutation addMetafieldToImage($metafields: [MetafieldsSetInput!]!) {
    metafieldsSet(metafields: $metafields) {
    metafields {
      id
      owner {
        ... on Image {
          id
          url
        }
      }
      ownerType
      type
      value
    }
    userErrors {
      code
      elementIndex
      field
      message
    }
  }
}

 

The variables look like this:

 

{
  "metafields": [
    {
      "ownerId": "gid://shopify/MediaImage/33666068480301",
      "type": "single_line_text_field",
      "namespace": "image",
      "key": "somekey",
      "value": "testme"
    }
  ]
}

 

The response I get is this one:

 

{
  "data": {
    "metafieldsSet": null
  },
  "errors": [
    {
      "message": "invalid id",
      "locations": [
        {
          "line": 2,
          "column": 5
        }
      ],
      "path": [
        "metafieldsSet"
      ]
    }
  ],
  "extensions": {
    "cost": {
      "requestedQueryCost": 11,
      "actualQueryCost": 1,
      "throttleStatus": {
        "maximumAvailable": 1000,
        "currentlyAvailable": 999,
        "restoreRate": 50
      }
    }
  }
}

 

GrantDB
Shopify Partner
74 3 14

Don't suppose you got anywhere with this did id you?  I want to add metafields to media.  I can add no problem, but I can't seem to ever retrieve the the metafields

 

https://community.shopify.com/c/metafields-and-custom-data/accessing-mediaimages-and-metafields/td-p...

 

I have been using metafieldsSet to set the metafield

 
Thanks
 
Grant