Re: Storefront Cart Api: Has random error, response data from the mutation is inconsistent.

Topic summary

A developer encountered intermittent issues where Shopify Storefront API cart mutations (add/remove items) returned success responses indicating updated cart data, but the actual server-side cart remained unchanged. This caused data synchronization problems between mutation responses and subsequent cart queries.

Initial troubleshooting attempts:

  • Another user suggested Apollo Client caching solutions: manually updating cache after mutations, using refetchQueries, adding delays before fetching, or setting fetchPolicy: 'network-only'
  • The original poster clarified this wasn’t a client-side caching issue, as the mutation response itself showed updated data while the server hadn’t actually updated

Resolution:
The issue was identified and resolved. The problem stemmed from using Shopify’s checkout-sheet-kit-react-native library—specifically, its preload function was randomly causing cart data to revert to previous values after mutations were executed.

Related resources:

  • A similar issue was documented in Shopify’s iOS SDK GitHub repository
  • A separate user shared a JavaScript solution for displaying cart error messages on Shopify cart pages
Summarized with AI on October 28. AI used: claude-sonnet-4-5-20250929.

When use storeFront API to manipulate cart, and use Apollo on client side. There is a problem, sometimes there will be an error, that mutations for cart, like add to cart, remove item from cart… It returns the data as expected in response, but actually the actual data of cart has not been updated. So after call query get cart, the data is not synchronized with the response from mutation, so it leads to asynchrony in application.

Can anyone help me in this error ?

Shopify document: https://shopify.dev/docs/storefronts/headless/building-with-the-storefront-api/cart

Hey @Aiden19

Thank you for reaching out regarding the issue with cart mutations not immediately reflecting in the queried cart data. This behavior is likely due to a delay in Shopify’s Storefront API updating the cart state or Apollo Client caching outdated data.

To resolve this issue, please consider the following solutions:

  1. Manually Update Apollo Cache After Mutations

By default, Apollo does not automatically update the cache after a mutation. To ensure immediate synchronization, update the cache manually:

const [addToCart] = useMutation(ADD_TO_CART, {
  update(cache, { data }) {
    if (data?.cartLinesAdd?.cart) {
      cache.writeQuery({
        query: GET_CART,
        data: { cart: data.cartLinesAdd.cart }
      });
    }
  }
});
  1. Use refetchQueries to Retrieve Updated Cart Data

If the cache update does not work as expected, force a fresh fetch after a mutation:

const [addToCart] = useMutation(ADD_TO_CART, {
  refetchQueries: [{ query: GET_CART }]
});
  1. Introduce a Short Delay Before Fetching Updated Cart

Shopify’s backend may take a brief moment to update the cart state. Adding a short delay before querying the cart again can improve synchronization:

await addToCart();
setTimeout(() => {
  refetchCart(); // Fetch cart data after a slight delay
}, 500);
  1. Set Apollo Fetch Policy to network-only

To ensure that cart queries always return the latest data from Shopify rather than relying on Apollo’s cache, configure your query as follows:

const { data, refetch } = useQuery(GET_CART, {
  fetchPolicy: 'network-only' // Ensures fresh data from Shopify API
});

These steps should help maintain cart data consistency and prevent any asynchrony issues. If the problem persists, please let us know, and we can investigate further.

Best regards,

Rajat

Thanks for your response, i appreciate it!
I don’t think it’s related to Apollo’s cache issue, because when the mutation succeeds, the response is that the cart data has been updated. But in reality, the cart data on the server has not been updated.

i checked on github and found someone else having a similar problem to mine, as they were using an sdk for ios: https://github.com/Shopify/mobile-buy-sdk-ios/issues/1243

I think I fixed it myself, I used https://github.com/Shopify/checkout-sheet-kit-react-native to implement the checkout flow, and the preload function of this library resulted in asynchronization of the cart data when the cart data was updated by mutation, the preload caused the cart data to revert back to the previous value (this happened randomly)

Hey all,
Just wanted to share a quick fix I found for missing error messages on Shopify cart pages. I had the same issue where customers couldn’t see error messages when they tried to add too many items to their cart. Turns out, a simple JavaScript snippet can intercept API responses and display the error messages properly. I added it to my cart template file and it worked like a charm. Check out my blog for the details: Fixing Missing Error Messages on Shopify Cart Pages. Let me know if you have any questions!
Best, Zayd