Triamorph Systems

Architecture · 12 min read · Sep 08, 2024

Backend-for-Frontend (BFF) Pattern: GraphQL vs REST vs tRPC for Multi-Platform Enterprise Applications

Deep architectural comparison of the Backend-for-Frontend (BFF) pattern: when to use GraphQL, RESTful aggregators, or tRPC to power web and mobile applications.

By Hammad Haider

Core Engineering Takeaways

  • The BFF pattern decouples frontend development velocity from backend microservice release cycles, preventing API bloat.
  • Use tRPC for single-team TypeScript monorepos (Next.js + React Native) to achieve end-to-end compile-time type safety with zero code generation overhead.
  • Use GraphQL (Apollo Federation) for large multi-team organizations with diverse client teams requiring dynamic field querying across disparate domain services.

Serving desktop web dashboards and native mobile applications from a single monolithic "one-size-fits-all" REST API creates severe inefficiencies: mobile devices overfetch megabytes of unneeded JSON payloads over cellular connections, while web dashboards require dozens of sequential waterfall HTTP requests to assemble a single screen. The Backend-for-Frontend (BFF) pattern introduces lightweight, client-specific gateway services tailored to the exact data requirements of each user interface.

1. Why Generic One-Size-Fits-All APIs Fail Multi-Platform Apps

The mobile payload size and network round-trip bottleneck

A web dashboard displaying a user profile with 50 analytical widgets requires extensive nested relational data. A mobile notification widget only needs the user’s name and unread badge count. Forcing mobile devices to parse huge JSON payloads consumes battery and degrades render performance.

Mobile Payload Reduction
78%
Tailored BFF response filtering
Frontend Network Requests
1 Call
Single aggregated screen endpoint
Type Drift Errors
0
End-to-end shared TypeScript contracts

2. The 3 BFF Archetypes: REST Aggregators, GraphQL & tRPC

Architectural tradeoffs across team size and technology ecosystems

REST BFFs provide simplicity and excellent CDN edge caching. GraphQL provides unmatched flexibility for federated multi-team microservices. tRPC offers zero-build type safety across TypeScript full-stack monorepos.

src/server/routers/mobileBffRouter.ts (typescript)
import { router, publicProcedure } from '../trpc';
import { z } from 'zod';
import { userService, orderService } from '../microservices';

export const mobileBffRouter = router({
  getHomeScreenData: publicProcedure
    .input(z.object({ tenantId: z.string().uuid() }))
    .query(async ({ input }) => {
      // Parallel internal microservice orchestration
      const [userProfile, recentOrders] = await Promise.all([
        userService.getUserSummary(input.tenantId),
        orderService.getRecentOrders(input.tenantId, { limit: 5 })
      ]);

      // Return ONLY the minimal tailored payload required for mobile UI
      return {
        userName: userProfile.displayName,
        avatarUrl: userProfile.thumbnail,
        unreadAlertsCount: userProfile.notifications.filter(n => !n.read).length,
        orders: recentOrders.map(o => ({
          id: o.id,
          total: o.formattedTotal,
          status: o.status
        }))
      };
    })
});

3. End-to-End Type Safety with tRPC in Universal Monorepos

Refactoring backend endpoints with instant compile-time frontend warnings

In a Turborepo monorepo sharing code between Next.js web apps and React Native mobile apps, renaming a backend field in tRPC immediately flags TypeScript errors across all client repositories without requiring manual OpenAPI or GraphQL schema generation.

4. Edge Caching & Microservice Resiliency in BFF Layers

Deploying BFF logic to Cloudflare Workers for sub-10ms global edge responses

Deploying lightweight BFF aggregation functions to Cloudflare Workers allows caching personalized UI fragments close to the user while shielding core backend microservices from sudden traffic spikes.

Tags: backend for frontend bff patterngraphql vs rest vs trpc microservicesbff architecture mobile web enterprisetype safe api gateway typescriptapi orchestration layer design

Related Engineering Practice & Case Studies

Engineering Practice Blueprint

Custom Web Development & SaaS Platforms →

Next.js & React web platforms engineered for sub-1.5s load times and 98+ Lighthouse scores.

View Practice Blueprint & Capabilities →
Production Client System

Portfolix Developer Portfolio Builder →

Modern developer portfolio builder platform delivering sub-5 minute deployments with 98+ Lighthouse scores and 100% clean code export.

Read Deployed Case Study (Sub-5 minute edge deployment with 100% clean code export) →

Related Engineering Articles & Guides

Explore all technical guides in our Articles & Guides Index → or discuss your architecture with our engineers via Technical Discovery →