Skip to content

useLazyQuery cache overwrites results when multiple queries with different variables are executed before completion #12706

Closed
@TienThinh-fe

Description

@TienThinh-fe

Issue Description

When using useLazyQuery to execute the same query with different variables in quick succession (before the first query completes), Apollo Client's InMemoryCache only stores the result of the last query. Previous query results are lost and require re-fetching, even though they should be cached independently based on their different variable values.

This issue affects production applications where users interact rapidly with filter components, causing:

  • Data loss in both Apollo cache and application state
  • Unnecessary network requests for previously fetched data
  • Poor user experience with inconsistent loading states

Expected Behavior
Both queries should complete and cache their results independently based on their variable values. Subsequent calls with the same variables should use cached data without making network requests.

Actual Behavior
Only the last query's result is cached. Previous query results are lost from the cache, causing unnecessary re-fetching when those queries are triggered again.

Link to Reproduction

https://github.com/TienThinh-fe/apollo-lazy-issue

Reproduction Steps

Clone the repo

Backend

cd backend && npm install && npm start

Frontend

cd frontend && npm install && npm run dev

Execute the bug reproduction:

  • Open the application in browser
  • Open browser console to observe network requests
  • Click "Load Tags" button (observe loading state)
  • Immediately click "Load Persons" button before Tags completes loading
  • Observe that only Persons data appears in the UI
  • Click "Load Tags" again - notice it makes a new network request instead of using cached data

@apollo/client version

3.13.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions