# safe-action-tanstack-query

Connects next-safe-action mutations to TanStack Query with typed error handling, cache updates, and invalidation. Use it when React Query already handles your client data.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 438 installs · ★ 1
- Author: next-safe-action
- License: not stated · policy=link-only · description only; the file stays with the author
- Source: https://github.com/next-safe-action/skills/tree/HEAD/skills/safe-action-tanstack-query/SKILL.md
- Updated: 2026-09-03
- HTML: https://affpapa.org/en/nevibecode/skills/safe-action-tanstack-query

## What it does

This skill explains how `mutationOptions()` fits into `useMutation`. It maps the safe action result into TanStack Query’s mutation model: returned server or validation failures become `ActionMutationError`, while successful results arrive as mutation data. The guide also covers `isActionMutationError`, `hasServerError`, and `hasValidationErrors`.

It describes cache-based optimistic changes with `onMutate`, query invalidation through `queryClient.invalidateQueries()`, retry backoff, DevTools, and paused mutation persistence. The integration requires `@next-safe-action/adapter-tanstack-query` and `@tanstack/react-query`.

There is a firm boundary: wrapped actions must be stateless `.action()` functions that return errors instead of throwing them. `stateAction`, `throwValidationErrors: true`, and `throwServerError: true` do not fit this adapter. It handles mutations, not data fetching queries.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-tanstack-query -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/safe-action-tanstack-query/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/safe-action-tanstack-query/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-tanstack-query -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/safe-action-tanstack-query/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-tanstack-query -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/safe-action-tanstack-query/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install next-safe-action/skills safe-action-tanstack-query
```

2. **Manually: put the files in place**
- File: `.github/skills/safe-action-tanstack-query/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-tanstack-query -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/next-safe-action/skills --path skills/safe-action-tanstack-query
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/safe-action-tanstack-query/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add next-safe-action/skills --skill safe-action-tanstack-query -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/next-safe-action/skills/tree/HEAD/skills/safe-action-tanstack-query/SKILL.md

## When to use it
- connect a safe action to `useMutation`
- map server and validation failures to typed errors
- invalidate the client query cache after a write
- apply an optimistic cache update with `onMutate`
- configure retries and paused mutation persistence

## Example prompts
- Connect `createUser` to `useMutation` with `mutationOptions()` and handle its returned data.
- Show how to distinguish `serverError` from `validationErrors` with `ActionMutationError` and the type guards.
- Add an optimistic cache update in `onMutate`, then invalidate the related queries with `queryClient.invalidateQueries()`.

## Similar
- [frontend-design](https://affpapa.org/en/nevibecode/skills/frontend-design.md) — skill · 1025790 installs
- [design-taste-frontend](https://affpapa.org/en/nevibecode/skills/design-taste-frontend.md) — skill · 678700 installs
- [prototype](https://affpapa.org/en/nevibecode/skills/prototype.md) — skill · 986201 installs
- [hyperframes](https://affpapa.org/en/nevibecode/skills/heygen-com-hyperframes.md) — skill · 1200000 installs
- [hyperframes-cli](https://affpapa.org/en/nevibecode/skills/heygen-com-hyperframes-cli.md) — skill · 1300000 installs
- [webapp-testing](https://affpapa.org/en/nevibecode/skills/webapp-testing.md) — skill · 186758 installs

From the same set: [next-safe-action/skills](https://affpapa.org/en/nevibecode/by/next-safe-action/skills.md) — 9 cards

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
