//native
/**
* Update Metaobject
* Update a metaobject entry (numeric ID or Metaobject GID). Only the field keys present in fields are changed (e.g. `{ "rating": 4 }`); non-string values are JSON-encoded. status applies only to definitions with the publishable capability. Needs the write_metaobjects scope.
*/
export async function main(
auth: RT.Shopify,
metaobject_id: string,
fields: { [key: string]: any } | undefined,
handle: string | undefined,
status: "ACTIVE" | "DRAFT" | undefined
) {
const id = metaobject_id.startsWith("gid://")
? metaobject_id
: `gid://shopify/Metaobject/${metaobject_id}`
const entries = Object.entries(fields ?? {})
const response = await fetch(
`https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
{
method: "POST",
headers: {
"X-Shopify-Access-Token": auth.token,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
query: `mutation ($id: ID!, $metaobject: MetaobjectUpdateInput!) { metaobjectUpdate(id: $id, metaobject: $metaobject) { metaobject { id handle type displayName updatedAt capabilities { publishable { status } } fields { key type value } } userErrors { field message } } }`,
variables: {
id,
metaobject: {
handle: handle || undefined,
fields: entries.length
? entries.map(([key, value]) => ({
key,
value:
typeof value === "string" ? value : JSON.stringify(value),
}))
: undefined,
capabilities: status ? { publishable: { status } } : undefined,
},
},
}),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { data, errors } = await response.json()
if (errors) {
throw new Error(JSON.stringify(errors))
}
if (data.metaobjectUpdate.userErrors.length) {
throw new Error(JSON.stringify(data.metaobjectUpdate.userErrors))
}
return data.metaobjectUpdate
}
Submitted by hugo989 3 hours ago