<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kunal Ninawe</title>
    <description>The latest articles on DEV Community by Kunal Ninawe (@ninawekunal3).</description>
    <link>https://dev.to/ninawekunal3</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3900851%2F096e554f-6519-4674-aebe-ba7981679800.jpg</url>
      <title>DEV Community: Kunal Ninawe</title>
      <link>https://dev.to/ninawekunal3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ninawekunal3"/>
    <language>en</language>
    <item>
      <title>Added a BFF layer before reaching for GraphQL. What happened next?</title>
      <dc:creator>Kunal Ninawe</dc:creator>
      <pubDate>Tue, 15 Sep 2026 17:50:27 +0000</pubDate>
      <link>https://dev.to/ninawekunal3/added-a-bff-layer-before-reaching-for-graphql-what-happened-next-c0f</link>
      <guid>https://dev.to/ninawekunal3/added-a-bff-layer-before-reaching-for-graphql-what-happened-next-c0f</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; One table page was making 1 + 1 + N network calls and pulling every bill into the browser. A thin server layer that speaks "one screen at a time" fixed it. Three months later it runs 80 routes across 9 features, and it caught a cross-user data leak before it shipped.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before: a React app talking straight to the back-end.
&lt;/h2&gt;

&lt;p&gt;Standard setup. React, React Query, generated API client. Every page called the back-end directly.&lt;/p&gt;

&lt;p&gt;The bills worklist table needed three things per row: the bill, its summary, and its pending approvers. &lt;br&gt;
Those lived behind three different endpoints.&lt;/p&gt;

&lt;p&gt;So the page did this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 1 call: every bill, no paging&lt;/span&gt;
&lt;span class="nf"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;queryFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getBills&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;page_size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9999&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// 1 call: the summary strip&lt;/span&gt;
&lt;span class="nf"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;queryFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getWorklistSummary&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// N calls: one per bill on screen, to get approvers&lt;/span&gt;
&lt;span class="nf"&gt;useQueries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;bills&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;queryFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getBillApprovals&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;bill_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;})),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then TanStack Table filtered, sorted, searched, and paginated all 9,999 rows in the browser.&lt;/p&gt;

&lt;p&gt;Three problems, in order of pain:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;N+1.&lt;/strong&gt; 50 rows on screen meant 52 requests before the table was usable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fetch everything.&lt;/strong&gt; The browser downloaded the whole bill corpus to show page 1 of 10.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stitching in the UI.&lt;/strong&gt; Every screen re-wrote its own "join bill to approver" logic, and they drifted.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why not GraphQL?
&lt;/h2&gt;

&lt;p&gt;GraphQL solves the shape problem, not the "where does the join and the auth check live" problem. &lt;br&gt;
A BFF answers both, on the server you already deploy, with zero new infra. &lt;br&gt;
If we ever need GraphQL, it plugs in behind the BFF, not instead of it.&lt;/p&gt;
&lt;h2&gt;
  
  
  After: one call per screen
&lt;/h2&gt;

&lt;p&gt;The BFF is a set of resource routes on the same Express server that already does our SSR. &lt;br&gt;
Each route serves exactly one screen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/routes/bff-payables-worklist-route.tsx&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;loader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;requirePermission&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Permission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;BILL_READ&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1. guard first&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createServerApiClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2. user's token, per request&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bills&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getCached&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nf"&gt;getBills&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;approvals,vendor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3. one backend call&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;paginate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bills&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 4. compute on the server&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;respond&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;worklistSchema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 5. validated contract&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser now sends one request: &lt;code&gt;GET /bff/worklist?tab=review&amp;amp;q=acme&amp;amp;page=1.&lt;/code&gt;&lt;br&gt;
It gets back 10 rows, a total, and per-tab counts. Nothing else.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjeeqw7ir88bvzgeuelzh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjeeqw7ir88bvzgeuelzh.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Before: the browser makes 52 requests and filters 9,999 rows in JavaScript. After: one call to the BFF, which checks permission, caches per user, makes one backend call, computes the page on the server, and returns 10 rows.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The bug you will hit in week one
&lt;/h2&gt;

&lt;p&gt;If you add a BFF, you now own two caches you did not own before. Both bit us.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The browser HTTP cache.&lt;/strong&gt; We set &lt;code&gt;Cache-Control: private, max-age=10&lt;/code&gt; on BFF responses. Reasonable. 
Then user A logged out, user B logged in on the same laptop, and the browser replayed A's worklist JSON to B for 10 seconds. 
The fix was one line: &lt;code&gt;Vary: Cookie&lt;/code&gt;. The lesson was bigger: every BFF response is per-user, so treat every cache header as a security setting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The in-memory server cache.&lt;/strong&gt; A BFF loader that caches "the bills list" is a cross-tenant leak waiting to happen. Ours is keyed by &lt;code&gt;userId&lt;/code&gt;, and a review agent now blocks any PR where a cache key is anything else.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The guard pattern that came out of it: &lt;code&gt;requirePermission()&lt;/code&gt; is the first statement in every loader, tenant scope comes from the token and never from a query param, and a fetch error fails loud instead of returning an empty list.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the BFF grew into?
&lt;/h2&gt;

&lt;p&gt;Once the layer existed, things kept landing on it because it was the obvious place.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server-side filter, sort, search, paginate.&lt;/strong&gt; The table emits state, the BFF does the work. Search is debounced at 600ms and adjacent tabs are prefetched.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One contract per screen.&lt;/strong&gt; Each route returns exactly what its page renders. That is the GraphQL benefit, without the GraphQL.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A shared toolkit.&lt;/strong&gt; app/lib/bff holds cache, compute, CSV, and response helpers. Written once, used by 9 features.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The route triple.&lt;/strong&gt; Every table page ships list + CSV export + filter options as three sibling routes. New pages copy the shape instead of designing one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A notification system.&lt;/strong&gt; The BFF owns a durable inbox (SQLite), a connected-tab presence map, and catch-up delivery after a closed tab. None of that needed a new service.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Plus two I did not expect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Upload proxying.&lt;/strong&gt; File uploads go through the BFF as an async job with a status endpoint and streamed progress.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Attachment safety.&lt;/strong&gt; The BFF refuses to hand out a URL for a quarantined or unscanned file. That check lives in one place now.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to add one?
&lt;/h2&gt;

&lt;p&gt;Add a BFF when you can say yes to two of these:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A screen needs data from more than one endpoint to render.&lt;/li&gt;
&lt;li&gt;The browser is doing joins, filters, or pagination on the full dataset.&lt;/li&gt;
&lt;li&gt;You already run a Node server for SSR.&lt;/li&gt;
&lt;li&gt;Auth, tenant scoping, or cache rules are copy-pasted across pages.
Skip it if you have one backend, one client, and the backend already returns page-shaped data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Numbers:&lt;/strong&gt; 80 BFF routes, 9 features, 213 files, one leak caught before production.&lt;/p&gt;

&lt;p&gt;What did I miss? &lt;br&gt;
If you have run a BFF for longer than three months, I want to hear what broke. &lt;br&gt;
Find me on &lt;a href="https://www.linkedin.com/in/ninawekunal/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>bff</category>
    </item>
  </channel>
</rss>
