Why is my update_metafields.js action for gadget model not updating metafields in Shopify?

Topic summary

A developer is experiencing issues with a custom update_metafields.js action in their Gadget model for Shopify products. The action is designed to update product metafields using Shopify’s GraphQL API.

Key Problem:

  • The action fails to update metafields when triggered via a button on the Shopify storefront
  • No error messages are displayed, making debugging difficult

Technical Details:

  • The code includes a GraphQL mutation (metafieldsSet) to update metafields
  • Implementation uses connections.shopify.current?.graphql() to interact with Shopify
  • The metafield configuration includes: namespace “custom”, key “title_2”, type “single_line_text_field”, with the value derived from record.title_2

Current Status:
The issue remains unresolved with no responses or solutions provided yet. The reversed/garbled text in portions of the code snippet suggests potential encoding or formatting issues in the original post.

Summarized with AI on November 11. AI used: claude-sonnet-4-5-20250929.

I have a problem with updating shopify metafields through gadget model actions. I’ve created a update_metafields.js action for shopifyProduct model and it doesn’t work. Also doesn’t send any errors when used through button in shopify store front.

update_metafields.js :

/**

  • Action code for update on Shopify Product
  • @Anonymous { import(“gadget-server”).UpdateShopifyProductActionContext } context - Everything for running this action, like the api client, current record, params, etc.
    */
    export async function run({ api, record, params, connections }) {
    await connections.shopify.current?.graphql(
    mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) { metafieldsSet(metafields: $metafields) { metafields { id value } userErrors { field message } } },
    {
    metafields: [
    {
    key: “title_2”,
    namespace: “custom”,
    ownerId: gid://shopify/Product/${record.id},
    type: “single_line_text_field”,
    value: record.title_2 + " as a metafield!",
    },
    ],
    }
    );
    }

function used in button:
async function updateProduct() {
try {
await api.shopifyProduct.update_metafields(productId);
console.log(“Product updated successfully”);
} catch (error) {
console.error(“Error updating product:”, error);
}
}
updateProduct();