Error using graphQL in Shopify app

I have the following function to add tags to an order: // GraphQL function to update order tags using the correct query async function updateOrderTags(shopify, orderId, tags) { const mutation = mutation addTags($id: ID!, $tags: [String!]!) { tagsAdd(id: $id, tags: $tags) { node { id } userErrors { message } } } ; const variables = { id: gid://shopify/Order/${orderId}, // Ensure the ID is properly formatted as a global ID tags: tags // Tags should be a valid array of strings }; try { const result = await shopify.graphql(mutation, { variables }); if (result.tagsAdd.userErrors.length > 0) { const errorMessages = result.tagsAdd.userErrors.map(error => error.message).join(", "); throw new Error(Failed to update tags: ${errorMessages}); } return result; } catch (error) { throw new Error(GraphQL Error: ${error.message}); } } And it is being used in the code as follows: ... const shopify = connections.shopify.current; ... await updateOrderTags(shopify, orderId, newTags); ... However, it continues t throw the following error:
No description
No description
No description
No description
2 Replies
[Gadget] Riley
[Gadget] Riley12mo ago
Hello, it looks like the id variable is getting set to null. If you spread ...variables or just pass in variables without the {} does that fix the issue? It seems like id and tags are the expected params for the mutation
const result = await shopify.graphql(mutation, variables);
const result = await shopify.graphql(mutation, variables);
safwan
safwanOP12mo ago
okay it works now thank you so much I wasted an entire day and all my gpt 4o prompts on this

Did you find this page helpful?