Storefront createCart returned id is invalid global id for fetching the cart?

rnnyrk
Shopify Partner
1 0 0

Hi there,

 

I'm trying to build my own Shopify implementation using the Storefront API. I'm creating a cart, which returns the ID:

 

 

export async function addToCart(itemId: string, quantity: number) {
  const createCartMutation = gql`
    mutation createCart($cartInput: CartInput) {
      cartCreate(input: $cartInput) {
        cart {
          id
checkoutUrl } } } `; const variables = { cartInput: { lines: [ { quantity, merchandiseId: itemId, }, ], }, }; try { const card = await graphQLClient.request(createCartMutation, variables); console.log({ addToCartCard: card }); return card; } catch (error) { console.error(error); } }

 

This works perfectly fine. The cart seems to be created an when i visit the checkoutUrl I see a valid cart in the Shopify checkout flow with the product I've added.

 

Although when I try to fetch that cart with the returned ID from this query, let's say `gid:/shopify/Cart/c1-9e5bb45d10420f754f15389f15af54c2` I get the error stating: Invalid global id

 

The fetch call looks like:

export async function getCart(cartId: string) {
  const cartQuery = gql`
    query getCart($cartId: ID!) {
      cart(id: $cartId) {
        ${CartDetails}
      }
    }
  `;

  const variables = {
    cartId,
  };

  try {
    const data = (await graphQLClient.request(cartQuery, variables)) as any;
    console.log({ getCart: data });

    return data.cart;
  } catch (error) {
    console.error(error);
  }
}

I have no clue how or why this happens. All the IDs seem to match, but there is not a single hint to what I misunderstanding.. 

 

Screenshot 2023-05-26 at 10.24.59.png

 

Reply 1 (1)

ShopifyDevSup
Shopify Staff
1363 228 476

Hey @rnnyrk - thanks for getting in touch - this is odd. Generally, our team can't support language specific questions (we would usually ask for the raw GraphQL query), but if you're still encountering the issue I may have a potential fix here. 

I noticed that the GID is formatted like this in your example: gid:/shopify/Cart/c1-9e5bb45d10420f754f15389f15af54c2. Usually, we would expect the global ID to be formatted like this: gid://shopify/Cart/c1-9e5bb45d10420f754f15389f15af54c2 with an additional forward slash ("/"), 

I'm not sure if this would resolve the issue, but I ran a quick test in my dev environment and got a similar error that read "Invalid global id 'gid:/shopify/InventoryItem/[number-here]" when I attempted to replicate the issue using a query I had been working with earlier. If this doesn't resolve the issue - could you reach out to our Partner Support team through your Partner dashboard and provide an x-request-id from your response headers and link to this thread? If the error is within 7 days we can take a look in our logs and see if there is anything that would point to what the problem is if the formatting doesn't resolve the issue. 

Partner Support will also be able to get in touch with our technical teams so that they can facilitate more troubleshooting on your behalf if need be. Hope this helps!

 

Al | Shopfiy Developer Support 

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