> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cfosilvia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 02 QUERY HOOK MIGRATION

# Query Hook Migration

## Objective

Move all read-side data fetching behind query hooks that use the shared key factory.

## Rules

* all reads belong in `hooks/queries/`
* no page or component should call raw `fetch` for app data reads
* every `useQuery` must use `globalKeys` or `userKeys(userId)`
* user-scoped queries must be disabled when `userId` or other required auth context is missing

## Migration Pattern

Before:

```ts theme={null}
return useQuery({
  queryKey: ['accounts', 'list'],
  queryFn: fetchAccounts,
})
```

After:

```ts theme={null}
const userId = user?.id ?? ''

return useQuery({
  queryKey: userKeys(userId).accounts.list(),
  queryFn: () => fetchAccounts(session, userId),
  enabled: !!user?.id && !!session,
})
```

## Required Review Work

For each query hook:

1. confirm whether the data is user-scoped or global
2. replace inline keys with factory keys
3. confirm `enabled` guards still prevent unauthenticated fetches
4. update any in-file invalidation to use the same factory
5. remove local key builder helpers

## Domains To Migrate

Suggested grouping:

* core: accounts, manual accounts, holdings, net worth, daily snapshots
* transactions: unified, investment, manual, account-specific variants
* assets/liabilities: properties, vehicles, physical assets, liabilities, other assets
* crypto: wallets, crypto holdings, plaid crypto holdings, coinbase data
* vault/chat/settings: vault folders/documents, chat sessions, user info, personal info, settings
* remaining app domains: subscriptions, earnings, sync status, alerts, research, news, market data

## Fetch Extraction Rule

If a page/component currently does this:

```ts theme={null}
const response = await fetch('/api/foo')
```

and the call is a read, move that logic into a new or existing file under `hooks/queries/`.

Example naming:

* `hooks/queries/use-sync-status.ts`
* `hooks/queries/use-research-sessions.ts`
* `hooks/queries/use-account-refresh-status.ts`

## Acceptance Criteria

* no raw read-side `fetch` calls remain in pages/components for app data
* every query hook uses the shared factory
* every user-scoped query is isolated by `userId`


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.