# Coming from TanStack Query (https://effetch-query.monarcode.dev/docs/coming-from-tanstack-query)

You know TanStack Query, and you want to know what's the same here and what
isn't. Most of what you know carries over: the vocabulary is the same, and
so are many of the names. A few things work differently:

- **A result is an `AsyncResult`.** Reading a query gives Effect's
  `AsyncResult`: `Initial`, `Success` or `Failure`. A failure holds the
  full `Cause`, so its error keeps its type, such as `TodoNotFound`.
- **Nothing retries by default.** A failed fetch fails at once, unless you
  give `retry` a `Schedule`.
- **Queries are defined once, at module level.** A query is a value, like a
  schema: `todos` is defined once and shared, not written out with its key
  in each component.
- **Offline pauses nothing.** There's no `networkMode`: a fetch that runs
  while the device is offline fails, and the queries in use that are
  stale refetch when it reconnects.

Each of the following sections maps one part of TanStack Query's API to
Effetch Query's, in the order of the sidebar, and links each to the page
that explains it.

## Queries

Defining and fetching queries maps as follows:

| TanStack Query                                                      | Effetch Query                                                                                                                                   |
| ------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `queryOptions({ queryKey, queryFn })`                               | [`api.query(endpoint)`](https://effetch-query.monarcode.dev/docs/queries), or [`Query.make` / `Query.family({ key, run })`](https://effetch-query.monarcode.dev/docs/queries/without-effetch)                         |
| `query()`; `ensureQueryData`, `fetchQuery` (deprecated since 5.102) | [`Query.fetch`](https://effetch-query.monarcode.dev/docs/cache#read-and-write-from-effect); outside Effect, `client.fetch`                                                         |
| `prefetchQuery` (deprecated since 5.102)                            | [`Query.prefetch`](https://effetch-query.monarcode.dev/docs/cache#read-and-write-from-effect); outside Effect, `client.prefetch`                                                   |
| `staleTime`, `gcTime`                                               | [`staleTime`, `gcTime`](https://effetch-query.monarcode.dev/docs/queries/freshness), as Effect durations such as `"30 seconds"`; `gcTime` is set per query, with no client default |
| `refetchOnWindowFocus`                                              | [`refetchOnFocus`](https://effetch-query.monarcode.dev/docs/queries/freshness#focus-and-reconnect)                                                                                 |
| `retry: 3`                                                          | [`retry: Schedule`](https://effetch-query.monarcode.dev/docs/queries/options) (default none)                                                                                       |
| `placeholderData` from the cache                                    | [`placeholder`](https://effetch-query.monarcode.dev/docs/queries/placeholders) on `api.query`, `Query.make` / `Query.family`                                                       |
| `useInfiniteQuery`, `fetchNextPage`                                 | [`api.infiniteQuery`](https://effetch-query.monarcode.dev/docs/queries/infinite), `useInfiniteQuery`, `InfiniteQuery.fetchNextPage`                                                |
| `useInfiniteQuery`'s fields, `useSuspenseInfiniteQuery`'s           | [`useInfiniteQueryState`, `useSuspenseInfiniteQueryState`](https://effetch-query.monarcode.dev/docs/react/as-object#read-pages): the fields with the page controls                 |
| `getNextPageParam`, `getPreviousPageParam`                          | [`nextPageParam`, `previousPageParam`](https://effetch-query.monarcode.dev/docs/queries/infinite) (return `undefined` / `null` for none)                                           |
| `experimental_streamedQuery`                                        | [`Query.stream`, `Query.streamFamily`](https://effetch-query.monarcode.dev/docs/queries/streams); `api.query` of an SSE or NDJSON endpoint                                         |
| `setQueryData` from a WebSocket's messages                          | [`live`](https://effetch-query.monarcode.dev/docs/queries/pushes) on `api.query`, `Query.make` / `Query.family`                                                                    |

## React

Reading queries in components maps as follows:

| TanStack Query                                                     | Effetch Query                                                                                                                                                                            |
| ------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `QueryClientProvider`                                              | [`QueryProvider`](https://effetch-query.monarcode.dev/docs/react)                                                                                                                                                           |
| `useQueryClient`                                                   | [`useQueryClient`](https://effetch-query.monarcode.dev/docs/react), which returns the nearest provider's client                                                                                                             |
| `useQuery`, `useSuspenseQuery`                                     | [`useQuery`](https://effetch-query.monarcode.dev/docs/react) returns an `AsyncResult`; [`useSuspenseQuery`](https://effetch-query.monarcode.dev/docs/react/suspense) returns its `Success`                                                                     |
| `useQuery`'s `data`, `error`, `isPending`, `isFetching`, `refetch` | [`useQueryState`](https://effetch-query.monarcode.dev/docs/react/as-object): the same fields, with `status` narrowing `data` and `error`. `status` is `"pending"`, `"success"` or `"failure"`, and `isError` is `isFailure` |
| `useSuspenseQuery`'s fields                                        | [`useSuspenseQueryState`](https://effetch-query.monarcode.dev/docs/react/as-object#read-with-suspense): the same fields, with `data` always there                                                                           |
| `select`                                                           | [`select`](https://effetch-query.monarcode.dev/docs/react#select-part-of-a-value) on `useQuery`, `useSuspenseQuery` and `useQueryState`; `Atom.map` to share it                                                             |
| `enabled: false`                                                   | [`useQuery(null)`](https://effetch-query.monarcode.dev/docs/react#read-nothing-until-youre-ready)                                                                                                                           |
| `placeholderData: keepPreviousData`                                | [`useQuery(query, { keepPrevious: true })`](https://effetch-query.monarcode.dev/docs/react#keep-the-last-value-while-the-next-loads)                                                                                        |
| `isPlaceholderData`                                                | [`isPlaceholder`](https://effetch-query.monarcode.dev/docs/react/as-object) on `useQueryState`                                                                                                                              |
| `useQueries`                                                       | [`useQueries`](https://effetch-query.monarcode.dev/docs/react#read-several-queries)                                                                                                                                         |
| `useQueries`'s fields                                              | [`useQueriesState`](https://effetch-query.monarcode.dev/docs/react/as-object#read-several-queries): an object of fields per query                                                                                           |
| `subscribed: false`                                                | [`paused: true`](https://effetch-query.monarcode.dev/docs/react/pause) on `useQuery`, `useQueryState`, `useQueries` and `useInfiniteQuery`                                                                                  |
| `useQueryErrorResetBoundary` with an error boundary and `Suspense` | [`QueryBoundary`](https://effetch-query.monarcode.dev/docs/react/suspense); with a router's boundary, `useResetQueryFailure`                                                                                                |
| `useIsFetching`                                                    | [`useIsFetching`](https://effetch-query.monarcode.dev/docs/mutations/activity)                                                                                                                                              |

## Mutations

Defining and running mutations maps as follows:

| TanStack Query                                         | Effetch Query                                                                                                                                                                              |
| ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `useMutation`, `mutationOptions`                       | [`useMutation(api.mutation(endpoint, { … }))`](https://effetch-query.monarcode.dev/docs/mutations), or `Mutation.make`                                                                                                        |
| `mutate(variables, { onSuccess, onError })`            | [`run(variables, { onSuccess, onFailure })`](https://effetch-query.monarcode.dev/docs/mutations/run)                                                                                                                          |
| `useMutation`'s `status`, `data`, `error`, `isPending` | [The same fields](https://effetch-query.monarcode.dev/docs/mutations/run#run-from-a-component) on `useMutation`, beside `result`. `status` is `"idle"`, `"pending"`, `"success"` or `"failure"`, and `isError` is `isFailure` |
| `useMutation`'s `variables`; mutation `scope`          | [`variables`](https://effetch-query.monarcode.dev/docs/mutations/run); [`concurrent: "queue"`](https://effetch-query.monarcode.dev/docs/mutations/run#when-runs-overlap) (per `useMutation`)                                                                     |
| `invalidateQueries` in a mutation's `onSuccess`        | [`invalidates`](https://effetch-query.monarcode.dev/docs/mutations/invalidate) on the mutation; `fromClient(ApiClient, { invalidates: "route" })` for every endpoint                                                          |
| `onMutate` returning a rollback context                | [`optimistic`](https://effetch-query.monarcode.dev/docs/mutations/optimistic), undone automatically                                                                                                                           |
| `useIsMutating`, `useMutationState`                    | [Same names](https://effetch-query.monarcode.dev/docs/mutations/activity); `useMutationState(mutation)` lists one mutation's runs in progress                                                                                 |

## The cache

Reading and writing the cache maps as follows:

| TanStack Query                                            | Effetch Query                                                                                                                                                  |
| --------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `getQueryData`, `setQueryData`                            | [`Query.get`, `Query.set` / `Query.update`](https://effetch-query.monarcode.dev/docs/cache) (Effects); `QueryClient.get(client, query)`                                                           |
| `setQueriesData`                                          | [`Query.update(family, (value, args) => …)`](https://effetch-query.monarcode.dev/docs/cache)                                                                                                      |
| `invalidateQueries`, `refetchQueries`, `resetQueries`     | [`Query.invalidate`, `Query.refetch`, `Query.reset`](https://effetch-query.monarcode.dev/docs/cache): a query, a family, a key prefix or a group. `Query.refetch` reaches only the queries in use |
| `removeQueries`, `queryClient.clear()`                    | [`Query.clear`, `client.clear()`](https://effetch-query.monarcode.dev/docs/cache/accounts): drops the values without fetching                                                                     |
| A record in many queries (normalized caches, TanStack DB) | [`Entity.update`, `Entity.set`, `Entity.remove`, `Entity.get`](https://effetch-query.monarcode.dev/docs/cache/entities)                                                                           |
| `setQueryData` on other queries in a query's `onSuccess`  | [`entities: { sync: [Todos] }`](https://effetch-query.monarcode.dev/docs/cache/entity-sync) on the client                                                                                         |

## The client

Creating and configuring the client maps as follows:

| TanStack Query                                  | Effetch Query                                                                                                                                                        |
| ----------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `new QueryClient({ defaultOptions })`           | [`QueryClient.make({ queries: { defaults }, mutations: { defaults } })`](https://effetch-query.monarcode.dev/docs/client/defaults)                                                                      |
| `QueryCache({ onError, onSuccess, onSettled })` | [`queries.onFailure`, `queries.onSuccess`, `queries.onSettled`](https://effetch-query.monarcode.dev/docs/client/defaults)                                                                               |
| `MutationCache({ onMutate, onError, … })`       | [`mutations.onMutate`, `mutations.onFailure`, …](https://effetch-query.monarcode.dev/docs/client/defaults)                                                                                              |
| `focusManager`, `onlineManager`                 | [The client's `signals`](https://effetch-query.monarcode.dev/docs/client#focus-and-online-signals) (`Signals.browser`, `reactNativeSignals`). They trigger refetches only: being offline pauses nothing |

## Server rendering

Sending the cache from a server maps as follows:

| TanStack Query                              | Effetch Query                                                                                                                                                                               |
| ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `dehydrate`, `hydrate`, `HydrationBoundary` | [`QueryClient.dehydrate`, `QueryClient.hydrate`, `HydrationBoundary`](https://effetch-query.monarcode.dev/docs/server-rendering). A `Query.make` or `Query.family` query needs a `schema`; an endpoint query uses its `output` |
| `setupRouterSsrQueryIntegration`            | [`connectQueryClient`](https://effetch-query.monarcode.dev/docs/frameworks/tanstack-start) (`./react/tanstack-router`)                                                                                                         |

## Not in 0.1

These have no equivalent in 0.1:

- `cancelQueries`;
- `networkMode`, and the pausing of queries and mutations while offline;
- devtools.

[Limitations](https://effetch-query.monarcode.dev/docs/reference/limitations) lists everything 0.1 doesn't
do, and what to do instead.

## What's next

- [Getting started](https://effetch-query.monarcode.dev/docs/getting-started): build a small todo app, a file
  at a time.
- [Queries from endpoints](https://effetch-query.monarcode.dev/docs/queries): define the queries your app
  reads.
- [Limitations](https://effetch-query.monarcode.dev/docs/reference/limitations): what Effetch Query 0.1
  doesn't do.