All things Shopify and commerce
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
Solved! Go to the solution
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)
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
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
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)
Discover how to increase customer engagement on your store with articles from Shopify A...
By Jacqui Apr 23, 2025Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025