Dedicated to the Hydrogen framework, headless commerce, and building custom storefronts using the Storefront API.
I am creating a custom site for a friend of mine and this is my first time actually diving into GraphQL. I am super lost with how this mutation is working, but whenever it is ran, I get a very vague "invalid ID" error. It doesn't specify if it is the cartId or the itemId. I have been fighting with this for about 12 hours trying to find something that works but can't seem to get it. Is there something I am missing, or a better way to go about adding an item to a cart?
The mutation looks like this:
`
mutation AddToCart($cartId: ID!, $itemId: ID!) {
cartLinesAdd(cartId: $cartId, lines: [{ quantity: 1, merchandiseId: $itemId}]) {
cart {
id
lines(first: 100) {
edges {
node {
id
quantity
merchandise {
... on ProductVariant {
id
product {
title
}
}
}
}
}
}
}
}
}
`,
variables: { cartId, itemId },
Solved! Go to the solution
This is an accepted solution.
Hi @JustinReidy 👋
One way to determine the invalid ID, would be to to query each by ID:
{
cart(id: "gid://shopify/Cart/a0b1c2d3e4f5g6") {
id
}
node(id: "gid://shopify/ProductVariant/9876543210"){
... on ProductVariant {
id
}
}
}
It's a little unclear how you're passing the variables, but something like the below should work for you:
mutation ($cartId: ID!, $lines: [CartLineInput!]!) {
cartLinesAdd(cartId: $cartId, lines: $lines) {
cart {
id
lines (first:10){
nodes {
quantity
merchandise {
... on ProductVariant{
id
}
}
}
}
}
}
}
With input variables:
{
"cartId":"gid://shopify/Cart/a0b1c2d3e4f5g6",
"lines":[
{
"quantity": 1,
"merchandiseId": "gid://shopify/ProductVariant/9876543210"
}
]
}
The curl request would look like this:
curl -L -X POST 'https://shop-name.myshopify.com/api/2022-10/graphql.json' \
-H 'X-Shopify-Storefront-Access-Token: abcdefghijk0123456789' \
-H 'Content-Type: application/json' \
--data-raw '{"query":"mutation ($cartId: ID!, $lines: [CartLineInput!]!) {\n cartLinesAdd(cartId: $cartId, lines: $lines) {\n cart {\n id\n lines (first:10){\n nodes {\n quantity\n merchandise {\n ... on ProductVariant{\n id\n }\n }\n }\n }\n }\n }\n}\n", "variables":{"cartId":"gid://shopify/Cart/a0b1c2d3e4f5g6","lines":[{"quantity":1,"merchandiseId":"gid://shopify/ProductVariant/9876543210"}]}}'
Hope that helps!
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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
This is an accepted solution.
Hi @JustinReidy 👋
One way to determine the invalid ID, would be to to query each by ID:
{
cart(id: "gid://shopify/Cart/a0b1c2d3e4f5g6") {
id
}
node(id: "gid://shopify/ProductVariant/9876543210"){
... on ProductVariant {
id
}
}
}
It's a little unclear how you're passing the variables, but something like the below should work for you:
mutation ($cartId: ID!, $lines: [CartLineInput!]!) {
cartLinesAdd(cartId: $cartId, lines: $lines) {
cart {
id
lines (first:10){
nodes {
quantity
merchandise {
... on ProductVariant{
id
}
}
}
}
}
}
}
With input variables:
{
"cartId":"gid://shopify/Cart/a0b1c2d3e4f5g6",
"lines":[
{
"quantity": 1,
"merchandiseId": "gid://shopify/ProductVariant/9876543210"
}
]
}
The curl request would look like this:
curl -L -X POST 'https://shop-name.myshopify.com/api/2022-10/graphql.json' \
-H 'X-Shopify-Storefront-Access-Token: abcdefghijk0123456789' \
-H 'Content-Type: application/json' \
--data-raw '{"query":"mutation ($cartId: ID!, $lines: [CartLineInput!]!) {\n cartLinesAdd(cartId: $cartId, lines: $lines) {\n cart {\n id\n lines (first:10){\n nodes {\n quantity\n merchandise {\n ... on ProductVariant{\n id\n }\n }\n }\n }\n }\n }\n}\n", "variables":{"cartId":"gid://shopify/Cart/a0b1c2d3e4f5g6","lines":[{"quantity":1,"merchandiseId":"gid://shopify/ProductVariant/9876543210"}]}}'
Hope that helps!
Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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
That worked! The cartId was working as expected, but I needed to get the ProductVariant ID. Even though none of the items will have any variants other than the base