A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Hi,
I am using vanilla JavaScript with GraphQL to update a line-item quantity on the Cart page. The details are below.
const endpoint = 'https://helloworld.myshopify.com/api/2024-07/graphql.json'; const headers = { 'Content-Type': 'application/json', 'X-Shopify-Storefront-Access-Token': '' }; const mutation_query = `mutation updateCartLines($cartId: ID!, $lines: [CartLineUpdateInput!]!) { cartLinesUpdate(cartId: $cartId, lines: $lines) { userErrors { code field message } cart { id lines(first: 5) { edges { node { id quantity attributes { key value } } } } } } }`; const options = { method: 'POST', headers, body: JSON.stringify({ query: mutation_query, variables: { cartId: `gid://shopify/Cart/${cart_token}`, lines: [ { id: `gid://shopify/CartLine/${line_id}`, quantity: 2 } ] } }) }; // Fetch fetch(endpoint, options) .then(response => { console.log('response:', response); return response.json(); }) .then(data => { console.log('data:', data) }) .catch(error => { console.error(error); });
{ ok: true, status: 200 }
{ "data": { "cartLinesUpdate": null }, "errors": [ { "message": "invalid id", "path": [ "cartLinesUpdate" ], "locations": [] } ] }
All help is appreciated! I have been stuck on this for hours and have tried everything I can think of and searched everything I can think of.
Thank you,
Michael
Hi Michael,
Are you sure these variables are correct:
variables: { cartId: `gid://shopify/Cart/${cart_token}`, lines: [ { id: `gid://shopify/CartLine/${line_id}`, quantity: 2 }
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Hey Liam,
I think they are but let me know if this looks correct to you.
cartID:
This came from the cart.js API. The variable is const cart_token = cart.token;
id attempt #1:
This came from the cart.js API. I noticed the ID is the same as the variant ID. The variables are const line_items = cart.items; and const line_id = line_item.id;
Thank you,
Michael
HI , there
It seems that the format "attempt#1" and "attempt#2" you mentioned follows the format of merchandise fields, rather than the cart line ID format.
see pic below
You d better use query cart Graphql to check the ids
query {
cart(id: "gid://shopify/Cart/xxxxxxxxx") {
# Cart fields
id
checkoutUrl
createdAt
lines(first:5){
nodes{
id
quantity
merchandise{
... on ProductVariant{
id
}
}
}
}
}
}
Hi there,
@anxiouspenguin, I am having the exact same use case as you, tried all the things you tried and am running into the same problem. Funny your post is only 3 days old!
Have you been able to fix it already? The problem still persists for me unfortunately.
I'm assuming you also used these docs here: https://shopify.dev/docs/storefronts/headless/building-with-the-storefront-api/cart/manage#step-3-in...
There it's clearly stated to do it this way, and if something isn't correct about this way of doing it, then Shopify should urgently update their documentation!
Regards,
Silvan
Hey @Tobi-van,
Unfortunately, I'm still stumped. If I ever figure this out I will reply and let you know! Please let me know if you figure it out!
Hey @Eric-HAN,
What is the CartLine Global ID format? Where and how did you get your CartLine Global ID? As @Tobi-van mentioned, there is no Line ID like this mentioned in any documentation or even on the internet that I can find. The Line ID is always mentioned as Variant ID or a Line Key but neither of those work and neither of those are formatted like your Line ID. Your help is much appreciated!
Thank you,
Michael
could query your ids first. what is your result? could you give the screenshot?
query {
cart(id: "gid://shopify/Cart/xxxxxxxxx") {
# Cart fields
id
checkoutUrl
createdAt
lines(first:5){
nodes{
id
quantity
merchandise{
... on ProductVariant{
id
}
}
}
}
}
}