Storefront API cart query returns wrong number of items after mutation

Bug Description

Cart mutations return correct data, but cart queries return wrong data. Items
disappear from cart on page refresh.

Steps to Reproduce

  1. Add 4 items to cart using cartLinesAdd mutation
  2. Mutation response shows all 4 items ✓
  3. Query same cart using cart query
  4. Query returns only 1 item (wrong) ✗

Example

Cart ID: gid://shopify/Cart/hWN1gPfWjo9w284Q5RN4LlgX

Mutation Response: 4 items [Lemon Balm, Chili Pequin, Cenizo, American Beauty]
Query Response: 1 item [American Beauty only]

Additional Affected Cart IDs

  • gid://shopify/Cart/hWN1gFnkborlIyjaxTNhfpj7
  • gid://shopify/Cart/hWN1gEU9KHAGtzra5jt0jGYD
  • gid://shopify/Cart/hWN1g9tP70EgV0XxY5gB8ExF

Impact

  • Cart is unusable.

Environment

Related Issue

Similar to the BXGY cart bug: reported here
However, our bug affects ALL cart items, not just discounted ones.

Hey @vc-ca thanks for the detailed report and your patience here.

I can’t say for sure, but the behaviour you’re describing typically happens when the cart query only requests a single line due to pagination.

In the Storefront API, cart.lines is a connection and you must pass lines(first: N), otherwise a small default like first: 1 (or a variable that defaults to 1) will only return one item even though the cart contains more.

Can you share your cart query requests here and confirm if you’re querying for multiple lines (for example, lines(first: 50))?

Another thing that could potentially contribute to this if cart request isn’t using the full cart ID including the ?key=…; dropping the key or using an older token can lead to confusing results.

If you’re on Hydrogen, I’d also make sure you’re updating the cookie/header right after mutations. Another thing to check, on refresh, is to see that you don’t have any background logic that “replaces” lines (for example, a cartLinesUpdate that only sends a subset), which would make it look like a query issue.

If the issues persist after checking the above, no worries, if you can share the exact cart query (selection set and variables), your mutation and query sequence, and run the same cart in Storefront GraphiQL with lines(first: 50) and then share the x-request-id from the response headers we send back and timestamps, I’m more than happy to take a look at the issue in our logs and we can potentially escalate this to our product team.

Hi Alan,

Thank you for the detailed response. I’ve investigated all your
suggestions and from what I can tell, this is a genuine API bug, not a configuration issue.
Here’s what I’ve verified:

  1. Cart Query Configuration
    We ARE using lines(first: 100) in our query:
fragment cart on Cart {
    id
    checkoutUrl
    cost { ... }
    lines(first: 100) {  # <-- Requesting 100 lines
      edges {
        node { ... }
      }
    }
    totalQuantity
  }
  1. Cart ID with Full Key
    We ARE preserving the complete cart ID including ?key= parameter:
    Cart ID used: gid://shopify/Cart/hWN1gRXMwmskUxSyyDa71xzd?key=c9c4bea56291ba03ba383d099ece0e9f

  2. Reproducible Bug Evidence
    Here’s a complete sequence showing the bug:

  • Step 1: Add 4 items via cartLinesAdd
    Mutation Response:

    • Cart ID: gid://shopify/Cart/hWN1gRXMwmskUxSyyDa71xzd?key=c9c4bea56291ba03ba383d099ece0e9f
    • totalQuantity: 4
    • lines.edges.length: 4
    • Items: [Cenizo, Catnip, Calendula, American Beauty Berries]
  • Step 2: Query same cart (immediate refresh)
    Query Response (SAME cart ID with key):

  • Cart ID:gid://shopify/Cart/hWN1gRXMwmskUxSyyDa71xzd?key=c9c4bea56291ba03ba383d099ece0e9f
  • totalQuantity: 1 # ← WRONG (should be 4)
  • lines.edges.length: 1 # ← WRONG (should be 4)
  • Items: [American Beauty Berries only]
  1. Not a Pagination Issue
    The totalQuantity field itself returns 1 instead of 4, proving this isn’t a
    pagination problem. If it were pagination, totalQuantity would still show 4?

  2. No Background Updates
    We have no cartLinesUpdate or cartLinesRemove operations between the mutation and query. The sequence is:

  • cartLinesAdd (returns 4 items)
  • Page refresh
  • cart query (returns 1 item)
  1. Request for Investigation

Could you please investigate with these specifics:

  • Store: 21wzt0-vj.myshopify.com
  • API Version: 2024-01
  • Affected Cart:
    gid://shopify/Cart/hWN1gRXMwmskUxSyyDa71xzd?key=c9c4bea56291ba03ba383d099ece0e9f
  • Timestamp: August 13, 2025, ~9:45 AM CST
    I can capture x-request-id headers if you provide specific test steps to run in
    GraphiQL.

Thank you!

Thanks @vc-ca for the followup and the extra details (including the repro steps.), really appreciate this.

It does seem like something odd is happening between the line items being added and the query. I haven’t been able to replicate on my end though on my test shop. Here’s my current flow:

  1. I run a cartCreate mutation via the storefront API
  2. Then, I run a cartLinesAdd mutation like this:
Summary
mutation cartLinesAdd($cartId: ID!, $lines: [CartLineInput!]!) {
  cartLinesAdd(cartId: $cartId, lines: $lines) {
    cart {
      id
      totalQuantity
      cost {
        totalAmount {
          amount
          currencyCode
        }
      }
      lines(first: 10) {
        edges {
          node {
            id
            quantity
            merchandise {
              ... on ProductVariant {
                id
                title
                product {
                  title
                }
              }
            }
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

Variables:

Summary

‘’’
{
“cartId”: “gid://shopify/Cart/id-here”,
“lines”: [
{
“merchandiseId”: “gid://shopify/ProductVariant/variant-id-here”, ## This is adding a new variant, not just adjusting the quantity, to clarify!
“quantity”: 5
}
]

In the returned response, I am able to see the newly added line items. If I just query the cart as well with a cart (id:) query, the new lines show up, so it is strange that it’s seemingly not taking.

One thing I just wanted to check, are those items part of any bundles that might appear as a single line item? Just trying to narrow down the issue. If not, happy to keep looking into this for sure.

If you’re still seeing the issue pop up, if you could share the x-request-ids from when you make an example cartLinesAdd mutation and then one for when you make the standard cart query, I can trace that exact flow in our logs and get in touch with our product team to investigate further.

Hope to hear from you soon, thanks again for your patience on this as well, really appreciated!

I’ve captured x-request-ids showing the cart bug. The query is
returning previously deleted items instead of current cart contents

Store:21wzt0-vj.myshopify.com
API Version: 2024-01
Cart ID: gid://shopify/Cart/hWN1m2A6pxbiEcQo2Gj7Hd8j?key=a112e37c60ab34a78821060051c5630d

Sequence of events:

  1. Added Calendula to cart (earlier)
  2. Deleted Calendula via cartLinesRemove
  3. Added 4 new items (Echinacea, Fig Leaf, Lemon Balm, Holy Basil)

Mutation (4th item added):

  • x-request-id: 3076a9af-9131-48bd-9bd8-8c9d5a643d71-1755137903
  • Timestamp: 2025-08-14T02:18:24.316Z
  • Result: Correctly shows 4 current items
  • totalQuantity: 4

Query (immediately before):

  • x-request-id: 78ac35fc-2431-4735-8c52-fa228bf5d14d-1755137782
  • Timestamp: 2025-08-14T02:18:23.771Z
  • Result: Shows deleted Calendula item
  • totalQuantity: 1

The cart query is returning a stale/cached state showing previously
deleted items. This suggests the query endpoint isn’t reading from the
same data source as mutations, possibly hitting a stale cache or read
replica.

Thanks

Thanks @vc-ca appreciate you sending these my way. I’m going to reach out to our product team for you now so that we can investigate this further internally and I’ll reach out as soon as I can confirm a fix/expected behaviour or share next steps.

We’ll get this looked into!

Hey @vc-ca, I was able to work with some folks internally on this, and we may need some more info from you. Would you be willing to work on this with me in direct message on the forums here? I can set that up on my end (we may need some more shop-specific info that we don’t necessarily want to share publicly!).

One other thing we noticed in our troubleshooting is that it looks like you’re using API version 2024-01. Could you try replicating this issue using API version 2025-07 (the latest API version) and letting me know if the issue persists? I’m just wondering if the issue pertains to that older API version being used there.

Hope to hear from you soon, thanks again for the patience on this, it’s really appreciated.

Hi @Alan_G,

I resolved the bug. It was my code, apologies!

After your suggestion to test with API version 2025-07, I did more digging and found the root cause was a caching conflict in my implementation.

The Shopify API is working perfectly.

The Problem:
My shopifyFetch function had a hardcoded revalidate: 600 (10-minute cache) in the Next.js fetch options, but I was also passing cache: 'no-store' for cart operations. This conflict caused Next.js to serve stale cached responses.

The Fix:

// Before (buggy):
next: {
  revalidate: 600,  // Always set, even with cache: 'no-store'
  ...(tags && { tags })
}

// After (fixed):
next: {
  ...(cache === 'no-store' ? {} : { revalidate: 600 }),
  ...(tags && { tags })
}

Why it looked like an API bug:

  • Mutations would return fresh data (they always bypass cache)
  • Queries would return stale cached data from 10 minutes ago
  • This made it appear that the query endpoint was returning wrong data

The cart now works perfectly on both API versions.
Thanks, your suggestions helped me find the issue!

Hey @vc-ca, glad everything is working for you now and thanks for sharing your solutions here with the community. Let me know if I can help with anything else as always! :smiley: