Course contentsShow
← TypeScript
Lesson 1590 of 1,79636. tRPC and End-to-End Type SafetyFree lesson

Hybrid Strategies: Mixing Approaches

Using multiple type-safe communication patterns in the same codebase.

Hybrid Strategies: Mixing Approaches

You'll learn how to combine multiple end-to-end type-safe communication patterns within the same codebase to meet different architectural needs.

Why Mix Approaches?

Real-world applications rarely fit perfectly into a single communication pattern. You might have:

  • Internal microservices that benefit from tRPC's zero-config inference
  • Public REST APIs requiring OpenAPI documentation (ts-rest/zodios)
  • Third-party integrations using GraphQL (graphql-codegen)
  • Legacy endpoints with hand-written types

Rather than forcing everything through one pattern, you can strategically combine approaches where each shines.

The Practical Reality

Think of type-safe communication tools like specialized vehicles: tRPC is a high-speed train (fast, efficient, specific route), REST contracts are buses (documented stops, public access), GraphQL is a flexible rideshare (query exactly what you need). You don't use one vehicle for every trip—you choose based on the journey.

In your codebase, this means:

// Internal admin operations → tRPC
const userList = await trpc.admin.users.list.query();

// Public API → ts-rest with OpenAPI
const apiClient = initContract(publicApiContract);
const products = await apiClient.products.list();

// Third-party CMS → GraphQL Codegen
const posts = await gqlClient.query({ query: GetPostsDocument });

Each pattern shares TypeScript types but serves different architectural boundaries.

Strategic Boundaries

The key is intentional boundaries: use tRPC for services you control, REST contracts for documented public APIs, and GraphQL codegen for external integrations. Shared domain types can flow through all approaches while preserving end-to-end type safety across different transport mechanisms.

Key Takeaway: Mixing tRPC, ts-rest, zodios, and GraphQL codegen in one codebase is not only valid—it's often the most pragmatic way to maintain type safety while meeting diverse architectural requirements.