Repository navigation
Conversation
Currently, `BaseDataService` has a `fetchQuery` method which is best
used for making read-only requests ("queries" in TanStack Query
parlance), but does not work as well for requests that change state on
the server side ("mutations"). For instance, queries are cacheable, but
mutations are not.
This commit adds a separate method, `executeMutation`, which
accommodates mutations better. Its implementation is different from
`fetchQuery` as it uses the mutation cache instead of the query cache.
Also, retries are disabled.
The UI query client deduplicated mutations by mutation key alone, which could misroute service :cacheUpdated state onto the wrong UI mutation when multiple mutations shared a key, orphan concurrent mutations, and clobber mutations that used a custom mutationFn. Introduce a UI-generated globalId that uniquely correlates one UI mutation with the service-side mutation it triggers. The UI stores the globalId on the mutation's meta, passes it as the trailing action argument, and matches incoming cache updates strictly by it. executeMutation accepts an optional globalId and echoes it back on the service-side mutation's meta so it rides along in dehydrated payloads.
hydrateMutations, deriveMutationAction, and readGlobalId lived inside createUIQueryClient and were only exercised indirectly through integration tests. Extract them into hydrateMutations.ts and add a dedicated unit test that covers every deriveMutationAction branch, including the previously-untested pending and idle (continue) cases. Remove the integration tests from createUIQueryClient.test.ts that only existed to reach the derive-action logic, keeping one that verifies the full round-trip synchronization between clients.
Collaborator
Author
|
@metamaskbot publish-previews |
Contributor
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
3 of 4 tasks
pull Bot
pushed a commit
to Reality2byte/core
that referenced
this pull request
Sep 23, 2026
…taMask#9324) ## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> Currently, `BaseDataService` has a `fetchQuery` method which is best used for making read-only requests ("queries" in TanStack Query parlance), but does not work as well for requests that change state on the server side ("mutations"). For instance, it makes sense for queries to be cached and retried, but not so much for mutations. This commit adds a separate method, `executeMutation`, which accommodates mutations better. Besides the differences mentioned above, it also uses the mutation cache instead of the query cache. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> Closes https://consensyssoftware.atlassian.net/browse/WPC-1118. ## Manual testing I've created MetaMask#10176, which converts `AuthenticatedUserStorageService.setAssetsWatchlist` to use `executeMutation` under the hood, and MetaMask/metamask-mobile#36083, which loads those changes into the mobile app. You can check out the mobile branch and run through the manual testing steps there to confirm that `executeMutation` still exhibits the same basic behavior as `fetchQuery`. ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md) - [ ] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Changes shared cache/event contracts (`objectType`, mutation sync) and introduces server-mutating paths; incorrect retry or hydration behavior could cause duplicate writes or stale UI state, though mutations are explicitly non-retried. > > **Overview** > Adds **mutation support** alongside existing query flows in `BaseDataService` and `@metamask/react-data-query`, so write requests no longer go through query-style caching and retries. > > **`BaseDataService`** gains a protected **`executeMutation`** API (plus exported **`MutationKey`**) that runs through TanStack’s mutation cache, optional Superstruct validation via **`processMutationResponse`**, and **no retry policy** (only the circuit breaker runs the request). Mutations can carry a **`globalId`** (default UUID) in `meta` to correlate with the UI client. **`:cacheUpdated`** payloads now include **`objectType: 'query' | 'mutation'`**, and mutation cache lifecycle is wired into persistence, dehydration, and **`destroy()`** (including clearing mutation GC timers). > > **`react-data-query`** exposes **`useMutation`** (retries off by default), routes data-service mutations through the messenger with the UI’s **`globalId`**, and syncs service-side mutation state via **`hydrateMutations`** on **`updated`** cache events only (so idle `added` events do not clobber settled UI results). > > **`@tanstack/query-core`** (and **`@tanstack/react-query`**) are bumped to **`^5.89.0`** across dependent packages; **`uuid`** is added where mutation IDs are minted. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 2bc6446. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a PR which allows us to smoke-test the changes being introduced in #9324 — specifically the introduction of
executeMutationin BaseDataService — within clients.It will be removed once this smoke test is complete.