Happening now | Office Hours: Customizing Your Theme With Dan-From-Ryviu | Ask your questions now!

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

Solved

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

Aiden19
Shopify Partner
4 1 0

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

Accepted Solution (1)

Aiden19
Shopify Partner
4 1 0

This is an accepted solution.

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)

View solution in original post

Replies 4 (4)

rajweb
Shopify Partner
781 66 142

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 }
      });
    }
  }
});

2. 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 }]
});

3. 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);

4. 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

 

Rajat | Shopify Expert Developer
Need a reliable Shopify developer for your next project?
Dash Drop App: https://apps.shopify.com/dash-drop-delivery
Email: rajat.shopify@gmail.com
Portfolio: https://rajatweb.dev

ProductifyGroups App:  Shopify App - Product Variants
Aiden19
Shopify Partner
4 1 0

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. 

Aiden19
Shopify Partner
4 1 0

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

Aiden19
Shopify Partner
4 1 0

This is an accepted solution.

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)