Block `QueryClient` API surface in UI

未關閉
#8,530 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
48/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
冷清
技術堆疊
typescript
領域
api, frontend

研究方向

從 packages/react-data-query/src/createUIQueryClient.ts 開始,閱讀現有的 invalidateQueries 和 defaultMutationOptions shadow 以及 wrapper hooks。決定每次呼叫的 queryFn 和 mutationFn override 應如何運作,然後為列出的 QueryClient 方法新增 guard,同時保留非資料服務的 key。為每個方法新增針對拋出和直通行為的單元測試,以及指定的 Option C 回歸測試。

由索引模型根據 Issue 內容生成。

描述

File: packages/react-data-query/src/createUIQueryClient.ts
Size: M | Hours: 6–10


Problem

Under ADR 0020 Option A, two messenger-routed proxies and the OmitKeyof wrapper hooks enforce that the background owns data-service-keyed cache entries: the proxy queryFn, and since #9324 (Update BaseDataService to accommodate mutations) the defaultMutationOptions shadow that injects a mutationFn. None of them covers the QueryClient instance surface. UI code with a handle on queryClient (via ui/contexts/query-client.ts or useQueryClient()) can call instance methods that bypass the proxy or cross the ownership boundary without warning.

No longer dormant. DATA_SERVICES on metamask-extension main holds MoneyAccountBalanceService and MoneyAccountApiDataService, so the guards below are on a live path rather than a preemptive one.

The options-resolution path is a second surface

Every method below is a QueryClient instance method. A shadow on the instance cannot reach a bypass supplied through per-call options, and both proxies can be overridden that way:

  • queryFn — the proxy is a client default, so an explicit queryFn on a useQuery call wins. OmitKeyof strips it from the wrapper's type, which is a compile-time strip with no runtime check.
  • mutationFn — defaultedOptions.mutationFn ??= is a nullish-assign, so a caller-supplied mutationFn on a data-service mutationKey wins and the write never reaches the service.

Whether that override is intended is an open design question rather than a settled hole: the query-side escape hatch was deliberate, and the mutation side mirrors it. Either way it is not closed by shadowing instance methods, so it needs its own decision and its own guard.


Solution

Mirror the existing invalidateQueries shadow. For each method below, shadow on the returned client and throw a descriptive error if the query key matches a registered data-service prefix. Non-data-service keys pass through unchanged. Note there are now two shadows on the returned client, invalidateQueries and defaultMutationOptions, so the guards compose with them rather than replacing them.

Methods to guard:

  • setQueryDefaults — per-key cache-policy override; ADR 0020 moves cache policy to the service layer
  • setMutationDefaults — data-service mutations exist as of #9324 (mutations in BaseDataService), so this is now live rather than preemptive
  • setDefaultOptions — block only when options.queries.queryFn or options.mutations.mutationFn is set
  • prefetchQuery / prefetchInfiniteQuery — pending policy decision on whether UI-side prefetch is in scope; block until decided
  • fetchQuery / fetchInfiniteQuery — same
  • setQueryData / setQueriesData — block; replacement implementation tracked separately (see Dependencies)
  • ensureQueryData / ensureInfiniteQueryData — block; replacement implementation tracked separately (see Dependencies)
  • getMutationCache / resumePausedMutations / isMutating — mutation-side twins, needed since #9324 (mutations in BaseDataService)

Acceptance Criteria

  • All methods above throw on data-service keys with a descriptive error citing the data service as cache owner and referencing ADR 0020
  • A caller-supplied queryFn or mutationFn on a data-service key is decided one way or the other, and whichever way it goes is enforced at runtime rather than only in the wrapper's types
  • setDefaultOptions only throws when options.queries.queryFn or options.mutations.mutationFn is set; other client-wide tuning unaffected
  • Non-data-service keys pass through for all methods (Option C hooks unaffected)
  • invalidateQueries and defaultMutationOptions shadows, wrapper hooks, and hydrate-based cache-sync continue to work
  • Unit tests per method: data-service key throws, non-data-service key passes, error message correct
  • Regression tests for Option C consumers (useHistoricalPrices, activity-v2/hooks, useMerklRewards) still pass

Dependencies

Depends on: MetaMask/metamask-extension#41183 (merged)
Dependents: MetaMask/metamask-extension#41977
Follow-up epic (Layer 2 + replacements): #8531 (Expose replacement implementations of QueryClient APIs in UI)
Widened by: #9324 (Update BaseDataService to accommodate mutations) — adds the mutation proxy and the mutation-side instance methods

主要語言
TypeScript
星號
422
分支
313
平均合併
1 天 8 小時
30 天內合併 PR
414

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 有 Pull Request 範本
  • 沒有貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

MetaMask/core 的其他 Issue

查看 MetaMask/core 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。