Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: cartLinesAdd mutation returning invalid ID

Solved

cartLinesAdd mutation returning invalid ID

JustinReidy
Shopify Partner
2 0 0

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 },
Accepted Solution (1)

ShopifyDevSup
Shopify Staff
1453 238 524

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

View solution in original post

Replies 2 (2)

ShopifyDevSup
Shopify Staff
1453 238 524

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

JustinReidy
Shopify Partner
2 0 0

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