Storefront graphql concurrent question

Topic summary

A developer is experiencing issues with Shopify’s Storefront GraphQL API when executing cartLinesAdd and cartLinesRemove mutations concurrently. The problem manifests as products being unexpectedly missing from the cart after simultaneous operations—for example, starting with two products and running both mutations at once results in only one product remaining instead of the expected two.

Root Cause:
The issue stems from race conditions where concurrent mutations may not process in the intended order, causing data inconsistency (e.g., items removed before being added).

Proposed Solutions:

  1. Sequential Execution: Implement logic to wait for cartLinesAdd to complete before executing cartLinesRemove, using promise chains or async/await patterns
  2. Optimistic Updates: Update the client-side cart state immediately after mutations, with proper rollback handling if server-side operations fail (requires careful testing)
  3. API Limitations: Consider contacting Shopify Support if the issue persists, as there may be inherent API constraints

The discussion remains open with one proposed solution but no confirmed resolution from the original poster.

Summarized with AI on November 2. AI used: claude-sonnet-4-5-20250929.

Hi!

I have a question about Shopify Graphql,If I make two graphql requests at the same time. One is **cartLinesAdd** and the other is **cartLinesRemove**,Then I found that the products I added were not in the final shopping cart.

For example, the original shopping cart has two products, and **cartLinesAdd**and **cartLinesRemove** are called at the same time. Normally, there should be two products in the cart, but in the end, only one product is left in the shopping cart.

I don’t know if the problem is caused by concurrency. Is there any solution?

Hi,
This is Richard at PageFly - Shopify Advanced Page Builder app.

You’re likely encountering concurrency issues with your Shopify GraphQL mutations. Here’s a breakdown of the problem and potential solutions:

Understanding the Issue

  • Concurrent Mutations: When you execute cartLinesAdd and cartLinesRemove simultaneously, Shopify’s GraphQL API might not process them in the order you intended.
  • Data Inconsistency: This can lead to unexpected results, such as products being removed before they are added, resulting in an incorrect final cart state.

Solutions

  1. Sequential Execution:

    • Implement Logic: Introduce logic to ensure sequential execution of mutations. For example:

      • Wait for the cartLinesAdd mutation to complete successfully before executing the cartLinesRemove mutation.
      • Use a mechanism like a promise chain or async/await (if using JavaScript) to handle the order of operations.
  2. Optimistic Updates (with Caution):

    • Optimistically Update Client-Side: After a successful cartLinesAdd mutation, update your local cart state (in your frontend application) immediately.

    • Handle Rollbacks: If the server-side mutation fails, roll back the optimistic update on the client-side.

    • Caution: Optimistic updates can lead to inconsistencies if not implemented carefully. Thoroughly test and handle potential race conditions.

  3. Shopify API Limitations:

    • Acknowledge Limitations: Be aware that there might be inherent limitations in how Shopify’s GraphQL API handles concurrent mutations.
    • Contact Shopify Support: If you suspect a bug or limitation in the API itself, reach out to Shopify Support for clarification.

Hoping my solution helps you solve your problem.
Best regards,
Richard | PageFly