Skip to content

[DON'T MERGE] Apply executeMutation pattern across some data services for testing - #10176

Draft
mcmire wants to merge 28 commits into
mainfrom
test-apply-mutation-pattern
Draft

mcmire wants to merge 28 commits into
mainfrom
test-apply-mutation-pattern

Conversation

@mcmire

@mcmire mcmire commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

This is a PR which allows us to smoke-test the changes being introduced in #9324 — specifically the introduction of executeMutation in BaseDataService — within clients.

It will be removed once this smoke test is complete.

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.
@mcmire mcmire changed the title Use mutation for assets watchlist writes [DON'T MERGE] Apply executeMutation pattern across some data services for testing Sep 10, 2026
@mcmire

mcmire commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator Author

@metamaskbot publish-previews

@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/account-tree-controller@8.0.0-preview-8bfa290fb
@metamask-previews/accounts-controller@39.1.1-preview-8bfa290fb
@metamask-previews/address-book-controller@7.1.2-preview-8bfa290fb
@metamask-previews/ai-controllers@1.0.0-preview-8bfa290fb
@metamask-previews/analytics-controller@2.0.0-preview-8bfa290fb
@metamask-previews/analytics-data-regulation-controller@0.0.0-preview-8bfa290fb
@metamask-previews/announcement-controller@8.1.0-preview-8bfa290fb
@metamask-previews/app-metadata-controller@2.0.1-preview-8bfa290fb
@metamask-previews/approval-controller@9.0.2-preview-8bfa290fb
@metamask-previews/assets-controller@14.0.3-preview-8bfa290fb
@metamask-previews/assets-controllers@111.1.3-preview-8bfa290fb
@metamask-previews/authenticated-user-storage@3.0.2-preview-8bfa290fb
@metamask-previews/base-controller@9.1.0-preview-8bfa290fb
@metamask-previews/base-data-service@1.0.0-preview-8bfa290fb
@metamask-previews/bitcoin-regtest-up@1.0.0-preview-8bfa290fb
@metamask-previews/bridge-controller@80.1.1-preview-8bfa290fb
@metamask-previews/bridge-status-controller@75.4.0-preview-8bfa290fb
@metamask-previews/build-utils@3.0.4-preview-8bfa290fb
@metamask-previews/chain-agnostic-permission@1.7.0-preview-8bfa290fb
@metamask-previews/chomp-api-service@4.0.1-preview-8bfa290fb
@metamask-previews/claims-controller@0.6.1-preview-8bfa290fb
@metamask-previews/client-controller@1.0.1-preview-8bfa290fb
@metamask-previews/client-utils@2.1.1-preview-8bfa290fb
@metamask-previews/compliance-controller@2.1.0-preview-8bfa290fb
@metamask-previews/composable-controller@12.0.1-preview-8bfa290fb
@metamask-previews/config-registry-controller@3.1.0-preview-8bfa290fb
@metamask-previews/connectivity-controller@0.3.0-preview-8bfa290fb
@metamask-previews/controller-utils@12.3.0-preview-8bfa290fb
@metamask-previews/core-backend@9.0.0-preview-8bfa290fb
@metamask-previews/cryptography@0.0.0-preview-8bfa290fb
@metamask-previews/delegation-controller@3.0.2-preview-8bfa290fb
@metamask-previews/earn-controller@12.2.6-preview-8bfa290fb
@metamask-previews/eip-5792-middleware@3.0.5-preview-8bfa290fb
@metamask-previews/eip-7702-internal-rpc-middleware@0.1.1-preview-8bfa290fb
@metamask-previews/eip1193-permission-middleware@2.0.1-preview-8bfa290fb
@metamask-previews/eth-block-tracker@15.0.1-preview-8bfa290fb
@metamask-previews/eth-json-rpc-middleware@24.0.2-preview-8bfa290fb
@metamask-previews/eth-json-rpc-provider@6.0.1-preview-8bfa290fb
@metamask-previews/foundryup@1.0.1-preview-8bfa290fb
@metamask-previews/gas-fee-controller@26.3.2-preview-8bfa290fb
@metamask-previews/gator-permissions-controller@5.0.2-preview-8bfa290fb
@metamask-previews/geolocation-controller@1.0.0-preview-8bfa290fb
@metamask-previews/java-tron-up@1.0.0-preview-8bfa290fb
@metamask-previews/json-rpc-engine@10.5.0-preview-8bfa290fb
@metamask-previews/json-rpc-middleware-stream@8.0.8-preview-8bfa290fb
@metamask-previews/keyring-controller@27.1.1-preview-8bfa290fb
@metamask-previews/kyc-controller@0.0.0-preview-8bfa290fb
@metamask-previews/local-node-utils@1.0.0-preview-8bfa290fb
@metamask-previews/logging-controller@9.0.0-preview-8bfa290fb
@metamask-previews/message-manager@14.1.2-preview-8bfa290fb
@metamask-previews/messenger@2.0.0-preview-8bfa290fb
@metamask-previews/messenger-cli@0.2.0-preview-8bfa290fb
@metamask-previews/money-account-api-data-service@0.4.1-preview-8bfa290fb
@metamask-previews/money-account-balance-service@2.4.3-preview-8bfa290fb
@metamask-previews/money-account-controller@1.0.0-preview-8bfa290fb
@metamask-previews/money-account-upgrade-controller@3.0.2-preview-8bfa290fb
@metamask-previews/money-account-utils@1.1.0-preview-8bfa290fb
@metamask-previews/multichain-account-service@13.0.2-preview-8bfa290fb
@metamask-previews/multichain-api-middleware@4.0.3-preview-8bfa290fb
@metamask-previews/multichain-network-controller@3.2.4-preview-8bfa290fb
@metamask-previews/multichain-transactions-controller@7.1.2-preview-8bfa290fb
@metamask-previews/name-controller@9.1.2-preview-8bfa290fb
@metamask-previews/network-connection-banner-controller@0.2.1-preview-8bfa290fb
@metamask-previews/network-controller@36.0.0-preview-8bfa290fb
@metamask-previews/network-enablement-controller@6.0.5-preview-8bfa290fb
@metamask-previews/notification-services-controller@26.0.1-preview-8bfa290fb
@metamask-previews/passkey-controller@3.1.0-preview-8bfa290fb
@metamask-previews/permission-controller@13.1.1-preview-8bfa290fb
@metamask-previews/permission-log-controller@5.1.0-preview-8bfa290fb
@metamask-previews/perps-controller@15.1.0-preview-8bfa290fb
@metamask-previews/phishing-controller@17.4.0-preview-8bfa290fb
@metamask-previews/platform-api-docs@0.1.0-preview-8bfa290fb
@metamask-previews/polling-controller@16.0.9-preview-8bfa290fb
@metamask-previews/preferences-controller@23.1.0-preview-8bfa290fb
@metamask-previews/profile-metrics-controller@4.0.3-preview-8bfa290fb
@metamask-previews/profile-sync-controller@29.0.0-preview-8bfa290fb
@metamask-previews/ramps-controller@20.2.0-preview-8bfa290fb
@metamask-previews/rate-limit-controller@7.0.1-preview-8bfa290fb
@metamask-previews/react-data-query@1.0.0-preview-8bfa290fb
@metamask-previews/remote-feature-flag-controller@6.1.0-preview-8bfa290fb
@metamask-previews/sample-controllers@5.0.6-preview-8bfa290fb
@metamask-previews/seedless-onboarding-controller@10.1.1-preview-8bfa290fb
@metamask-previews/selected-network-controller@26.1.7-preview-8bfa290fb
@metamask-previews/sentinel-api-service@1.0.1-preview-8bfa290fb
@metamask-previews/shield-controller@6.0.1-preview-8bfa290fb
@metamask-previews/signature-controller@39.2.10-preview-8bfa290fb
@metamask-previews/smart-transactions-controller@26.0.0-preview-8bfa290fb
@metamask-previews/snap-account-service@2.1.2-preview-8bfa290fb
@metamask-previews/social-controllers@2.8.0-preview-8bfa290fb
@metamask-previews/solana-test-validator-up@1.0.0-preview-8bfa290fb
@metamask-previews/stellar-quickstart-up@0.0.0-preview-8bfa290fb
@metamask-previews/storage-service@1.0.2-preview-8bfa290fb
@metamask-previews/subscription-controller@8.0.1-preview-8bfa290fb
@metamask-previews/transaction-controller@69.7.0-preview-8bfa290fb
@metamask-previews/transaction-pay-controller@27.1.1-preview-8bfa290fb
@metamask-previews/user-operation-controller@41.2.9-preview-8bfa290fb
@metamask-previews/wallet@12.0.2-preview-8bfa290fb
@metamask-previews/wallet-cli@0.0.0-preview-8bfa290fb

Base automatically changed from add-execute-mutation to main September 23, 2026 16:25
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant