Skip to main content
← Guides

Guide

tRPC vs GraphQL (2026)

Updated PkgPulse Team9 min read
0

Compare how tRPC shares TypeScript router types and GraphQL exposes a schema. Choose an API contract that fits your client languages and release schedules.

Contents

TL;DR

Choose tRPC when your TypeScript client can import the server router's type. Choose GraphQL when separately maintained clients or clients in other languages need a shared schema. A TypeScript monorepo can use either. The choice turns on how consumers get the contract. tRPC quickstart · GraphQL schemas and types

Key Takeaways

  • tRPC shares the router type with TypeScript clients. The router still defines procedures and can validate inputs at runtime. tRPC quickstart
  • GraphQL clients share a schema. A TypeScript client can generate types for its operations. GraphQL schemas and types · GraphQL Code Generator quickstart
  • Measure the dependencies you ship. For browser bundle size, compare equivalent queries in your production client builds. Account for the schema runtime and code generation separately in the server and development toolchains.

How Each Solves the API Contract Problem

Every API has a contract: "this endpoint accepts X and returns Y." The challenge is keeping that contract in sync between server and client as your application evolves. Before tRPC and modern GraphQL tooling, the disconnect was invisible until production:

// The old world: REST without contract enforcement
// Server returns this shape — client doesn't know
app.get('/users/:id', async (req, res) => {
  res.json({ id, name, email, role, createdAt });
});

// Client assumes the shape — drift happens silently
const user = await fetch('/users/123').then(r => r.json());
user.nome; // Typo — no compile-time error, fails only at runtime

Both tRPC and GraphQL solve this contract problem, but through fundamentally different mechanisms:

GraphQL uses a shared schema. The examples define it in SDL, which the server uses to validate client operations. If you want generated TypeScript types, run code generation with the schema and client operations as inputs. GraphQL schemas and types · GraphQL Code Generator quickstart

tRPC uses the router definition as the contract. The TypeScript client imports the router type and infers procedure inputs and outputs from it. A procedure can still use an input schema for runtime validation. tRPC quickstart

These illustrative snippets show the same getUser operation as a tRPC procedure and a GraphQL schema field.

// tRPC — procedure definition IS the contract
export const usersRouter = router({
  getById: publicProcedure
    .input(z.object({ id: z.string() }))
    .query(async ({ input, ctx }) => {
      return ctx.db.user.findUnique({ where: { id: input.id } });
      // Return type: User | null — automatically inferred
    }),
});
# GraphQL — separate schema file defines the contract
type Query {
  user(id: ID!): User
}

type User {
  id: ID!
  name: String!
  email: String!
}

With tRPC, there's one place to look. With GraphQL, the schema and the resolver are separate — you must keep them in sync manually or use a code-first approach (like Pothos or TypeGraphQL) that generates the SDL from TypeScript.


tRPC in Practice

A complete Next.js + tRPC + Zod setup illustrates how little boilerplate the approach requires:

// server/routers/users.ts — define procedures
import { router, publicProcedure, protectedProcedure } from '../trpc';
import { z } from 'zod';
import { TRPCError } from '@trpc/server';

export const usersRouter = router({
  getById: publicProcedure
    .input(z.object({ id: z.string() }))
    .query(async ({ input, ctx }) => {
      const user = await ctx.db.user.findUnique({ where: { id: input.id } });
      if (!user) throw new TRPCError({ code: 'NOT_FOUND' });
      return user; // Prisma infers the return type
    }),

  create: protectedProcedure
    .input(
      z.object({
        name: z.string().min(1),
        email: z.string().email(),
      })
    )
    .mutation(async ({ input, ctx }) => {
      return ctx.db.user.create({ data: input });
    }),
});

// server/root.ts — combine routers
export const appRouter = router({
  users: usersRouter,
  posts: postsRouter,
});
export type AppRouter = typeof appRouter; // This is the only export the client needs
// client/app.tsx — full type inference, zero codegen
import { trpc } from '../utils/trpc';

function UserCard({ userId }: { userId: string }) {
  const { data: user, isLoading } = trpc.users.getById.useQuery({ id: userId });
  // user: { id: string; name: string; email: string; ... } | undefined
  // TypeScript knows every field — rename on server breaks build on client

  if (isLoading) return <Spinner />;
  return <div>{user?.name}</div>;
}

const createUser = trpc.users.create.useMutation();
createUser.mutate({ name: 'Alice', email: 'alice@example.com' });
// Type error if you pass wrong fields or miss required ones ✅

The magic: AppRouter is just a TypeScript type. Import it on the client without importing any server code. The tRPC client uses this type to infer input and output shapes for every procedure at compile time.


GraphQL's Multi-Client Advantage

GraphQL gives client teams a language-neutral schema. A web app and a Python data pipeline can inspect the same types and select different fields in their queries.

Consider a product API consumed by multiple client types:

React web app (TypeScript)   ─┐
React Native mobile app      ─┤→ GraphQL API ← Single schema
Python data pipeline          ─┤
Third-party partner apps     ─┘

Teams that want static types can generate them from the GraphQL schema and their operations:

  • Web and mobile: graphql-codegen generates TypeScript types
  • Python: ariadne-codegen or manual introspection
  • Partner apps: introspect the schema and generate clients in any language

Any HTTP client can call a tRPC procedure by following its HTTP protocol. A Python client, for example, needs the procedure path and input format. A TypeScript client that imports the server's router type also gets end-to-end inference for procedure inputs and results. Choose GraphQL when teams need a shared schema they can inspect and use across languages. tRPC client documentation · tRPC HTTP specification

# GraphQL query — identical structure works from any client
query GetUser($id: ID!) {
  user(id: $id) {
    id
    name
    email
    posts {
      title
      createdAt
    }
  }
}

The field-level selection capability of GraphQL also matters at scale. A mobile client can fetch only the fields that fit on a small screen. A dashboard can fetch everything. A background job can fetch only IDs. With tRPC, you'd need separate procedures or optional fields to achieve the same flexibility.


GraphQL Federation and Microservices

GraphQL Federation (via Apollo Federation or Hive) lets you compose multiple GraphQL schemas into one unified graph. This is essential for large organizations where different teams own different parts of the data model:

# Products service — owns Product type
type Product @key(fields: "id") {
  id: ID!
  name: String!
  price: Float!
}

# Reviews service — extends Product with reviews
extend type Product @key(fields: "id") {
  id: ID! @external
  reviews: [Review!]!
}

# Clients query through a single composed gateway
query ProductWithReviews($id: ID!) {
  product(id: $id) {
    name
    price
    reviews {
      rating
      comment
    }
  }
}

The gateway composes these into a single schema and routes subqueries to the appropriate service. Clients see one coherent API without knowing how many services exist underneath.

tRPC has no equivalent Federation story. It is designed for monorepos or single-service deployments where all procedures live in one place. You can run multiple tRPC routers and merge them, but there is no automatic schema composition across team or service boundaries at the governance level that Federation provides.


GraphQL in Practice: Schema-First with Codegen

Understanding GraphQL's actual workflow helps clarify why teams choose it despite the additional tooling overhead. The schema-first approach with graphql-codegen produces a developer experience that is strong, even if it requires more ceremony:

# schema.graphql — the source of truth
type User {
  id: ID!
  name: String!
  email: String!
  role: UserRole!
  posts: [Post!]!
}

enum UserRole {
  USER
  ADMIN
}

type Query {
  user(id: ID!): User
}

type Mutation {
  createUser(input: CreateUserInput!): User!
}

input CreateUserInput {
  name: String!
  email: String!
}
# codegen.yml — run once to generate TypeScript types
overwrite: true
schema: "schema.graphql"
documents: "src/**/*.graphql"
generates:
  src/generated/graphql.ts:
    plugins:
      - typescript
      - typescript-operations
      - typescript-react-apollo

After running graphql-codegen, every operation you define in .graphql files gets typed React hooks:

// src/queries/user.graphql
query GetUser($id: ID!) {
  user(id: $id) {
    id
    name
    email
    role
  }
}

// After codegen: useGetUserQuery is fully typed
import { useGetUserQuery } from '../generated/graphql';

function UserProfile({ userId }: { userId: string }) {
  const { data, loading } = useGetUserQuery({ variables: { id: userId } });
  // data.user: { id: string, name: string, email: string, role: UserRole } | undefined
  return loading ? <Spinner /> : <div>{data?.user?.name}</div>;
}

The codegen step runs during development (typically via graphql-codegen --watch) and in CI before type checking. It adds a few seconds to the development loop but provides the same end-to-end type safety as tRPC — just with an explicit intermediate artifact.

Choosing an Approach for Next.js Applications

The most common question is whether to use tRPC or GraphQL for a Next.js application. The answer almost always favors tRPC for internal APIs:

A Next.js monorepo with one web client, one engineering team, and a TypeScript codebase is the ideal tRPC scenario. The AppRouter type lives in the api package, the web client imports it, and TypeScript enforces the contract with zero additional tooling. Adding GraphQL here means writing a schema, setting up a GraphQL server (Apollo or Yoga), configuring codegen, and running the codegen in your development workflow — all to solve a problem that tRPC solves with a single type export.

A React Native app and a web app can both import the same tRPC router type and get the same inference. If the mobile team needs to select different fields for a smaller screen, GraphQL lets it write a separate query against the shared schema.

The calculation changes further when you have non-TypeScript consumers. A data analytics pipeline in Python, a partner integration in Ruby, or a webhook consumer in Go — none of these can use tRPC's type sharing mechanism. GraphQL's language-agnostic schema and introspection API serve these use cases cleanly.

Key Technical Differences

tRPCGraphQL
SchemaNo separate schemaRequired (SDL or code-first)
Type safetyEnd-to-end inference in TypeScript with the router typeSchema validation on the server and optional generated operation types on the client
Multiple clientsAny HTTP client can call proceduresClients in any language can query the shared schema
Network protocolHTTP (REST-like)HTTP (queries/mutations), WebSocket (subscriptions)
IntrospectionVia TypeScriptBuilt-in SDL introspection
N+1 problemManual (DataLoader)Manual (DataLoader)
Learning curveLow (feels like function calls)Medium (new query language)
Tooling ecosystemGrowingMature (Apollo, Relay, urql)
Federation / compositionNot supportedYes (Apollo Federation, Hive)

Package Health

Packagenpm downloads, Nov 23–29, 2026Role
@trpc/server7,654,036tRPC routers and server adapters
@trpc/client6,538,146tRPC client
graphql67,154,492GraphQL.js implementation
@apollo/client7,988,818Apollo's GraphQL client
@graphql-codegen/cli9,618,285The Guild's code generation CLI

Each row counts downloads for one npm package. An app can install several of these packages, so the rows cannot be added to estimate how many apps use either approach. tRPC quickstart · GraphQL.js documentation · Apollo Client documentation · GraphQL Code Generator quickstart


When to Choose

Choose tRPC when:

  • Building a TypeScript monorepo where both server and client are in the same repository
  • Your application is a Next.js full-stack app with a single client
  • You want zero-overhead type safety without a codegen step or schema file
  • Your API is internal and not intended to be consumed by external parties or non-TypeScript code
  • Your team wants the simplest possible API layer with the best TypeScript DX

Choose GraphQL when:

  • Your API is consumed by multiple client types (web, mobile, third-party partners)
  • You have non-TypeScript consumers — Python, Swift, Kotlin, or any other language
  • Your organization needs GraphQL Federation to compose schemas across teams and services
  • You're exposing a public API where third parties need to introspect and discover capabilities
  • Your existing codebase already has a GraphQL schema and you want to leverage it