trpc-patterns

Standardize tRPC integration with TanStack Query in TSX files.

1.2k|116|Updated Mar 22, 2025
One-click install
npx skills add https://github.com/FranciscoMoretti/sparka --skill trpc-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: trpc-patterns
Source: https://github.com/FranciscoMoretti/sparka/tree/main/.skillz/trpc-patterns
Command: npx skills add https://github.com/FranciscoMoretti/sparka --skill trpc-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides clear, standardized patterns for integrating TRPC with Tanstack Query, eliminating guesswork and ensuring type-safe, efficient API interactions. It streamlines both backend and frontend development, reducing common errors and accelerating feature delivery.

Core Features & Use Cases

  • Type-Safe API Development: Guides the creation of TRPC routers and procedures with Zod validation for robust, error-resistant APIs.
  • Efficient Data Fetching: Demonstrates optimal patterns for useQuery and useMutation with Tanstack Query, ensuring performant data handling.
  • Automated Cache Invalidation: Ensures data consistency across your application by automatically invalidating relevant queries after mutations.
  • Use Case: Generate a new TRPC mutation for updating a user profile, including input validation, secure database interaction, and automatic cache invalidation for related user data queries, saving development time and ensuring correctness.

Quick Start

Using the trpc-patterns skill, create a new TRPC query for fetching a list of products, ensuring it uses protectedProcedure and proper database function calls.

Frequently Asked Questions about trpc-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I integrate tRPC with TanStack Query in React?

Integrate tRPC with TanStack Query by configuring tRPC hooks and TanStack Query's useQuery and useMutation to handle API calls type-safely. This combination standardizes data fetching across your React application, ensuring consistent error handling and automatic cache management for tRPC procedures.

What's the best way to structure tRPC procedures with Zod validation?

Structure tRPC procedures by defining input validation with Zod schemas and using protectedProcedure for secure operations. This pattern ensures type-safe, validated API endpoints that catch errors at compile time and runtime, reducing bugs in both backend and frontend code.

How does automatic cache invalidation work with tRPC mutations?

Automatic cache invalidation in tRPC mutations uses TanStack Query to clear stale query data after mutation completion. Configuring invalidateQueries ensures related data refetches automatically, keeping your UI synchronized without manual cache management.

Can I use tRPC with TanStack Query for both queries and mutations?

Yes, tRPC works seamlessly with TanStack Query's useQuery for fetching data and useMutation for creating and updating data. This pairing provides type-safe, efficient bidirectional communication between frontend and backend with built-in caching and error handling.

What validation patterns does tRPC require for API procedures?

tRPC requires Zod schemas to validate procedure inputs before execution. These schemas enforce type safety and input constraints at the API boundary, preventing invalid data from reaching your database and ensuring consistent validation across all endpoints.

When should I use protectedProcedure versus public procedures in tRPC?

Use protectedProcedure for operations requiring authentication and authorization checks, and public procedures for unrestricted endpoints. This pattern secures sensitive operations like profile updates while allowing open access where appropriate, standardizing security across your API.