<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[heyCoders]]></title><description><![CDATA[heyCoders]]></description><link>https://heycoders.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 22 Nov 2026 17:22:38 GMT</lastBuildDate><atom:link href="https://heycoders.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[REST vs GraphQL: Why Does GraphQL Exist?]]></title><description><![CDATA[Modern applications need to communicate with servers to fetch, create, update and delete data. Imagine building a developer community application where…]]></description><link>https://heycoders.hashnode.dev/rest-vs-graphql-why-does-graphql-exist</link><guid isPermaLink="true">https://heycoders.hashnode.dev/rest-vs-graphql-why-does-graphql-exist</guid><dc:creator><![CDATA[Vaishali Sirimalla]]></dc:creator><pubDate>Sat, 21 Nov 2026 13:44:30 GMT</pubDate><content:encoded><![CDATA[<p>Modern applications need to communicate with servers to fetch, create, update and delete data.</p>
<p>Imagine building a developer community application where users can explore events, register for meetups, view speaker profiles and interact with other developers.</p>
<p>The frontend needs data from different resources, such as users, events, speakers and registrations.</p>
<p>REST APIs have traditionally been a popular way to handle this communication. However, as applications became more complex, developers started facing a few challenges when fetching data.</p>
<p>Two common problems are:</p>
<ul>
<li><strong>Over-fetching:</strong> Receiving more data than the application actually needs.</li>
<li><strong>Under-fetching:</strong> Having to make multiple API requests to collect all the data required by a screen.</li>
</ul>
<p>This is where GraphQL comes into the picture.</p>
<p>GraphQL was developed internally at Facebook in 2012 and publicly open-sourced in 2015. It introduced a different approach to fetching data by allowing clients to specify exactly which fields they need.</p>
<p>But before understanding GraphQL, let's look at how REST works.</p>
<h2>How REST APIs Work</h2>
<p>REST stands for Representational State Transfer. It is an architectural style used for designing APIs that communicate over HTTP.</p>
<p>In REST, data is generally organised around resources, and each resource is accessed through a URL (endpoint).</p>
<p>For our developer community application, we might have endpoints like these:</p>
<table>
<thead>
<tr>
<th>HTTP Method</th>
<th>Endpoint</th>
<th>Purpose</th>
</tr>
</thead>
<tbody><tr>
<td>GET</td>
<td><code>/users/1</code></td>
<td>Fetch user details</td>
</tr>
<tr>
<td>GET</td>
<td><code>/users/1/events</code></td>
<td>Fetch a user's registered events</td>
</tr>
<tr>
<td>GET</td>
<td><code>/events/101</code></td>
<td>Fetch a particular event</td>
</tr>
<tr>
<td>POST</td>
<td><code>/events</code></td>
<td>Create a new event</td>
</tr>
<tr>
<td>PUT</td>
<td><code>/events/101</code></td>
<td>Update an event</td>
</tr>
<tr>
<td>DELETE</td>
<td><code>/events/101</code></td>
<td>Delete an event</td>
</tr>
</tbody></table>
<p>Let's say our frontend needs to display a user's profile.</p>
<p>It sends a request:</p>
<pre><code class="language-http">GET /users/1
</code></pre>
<p>The server might return:</p>
<pre><code class="language-json">{
  "id": 1,
  "name": "Vaishali",
  "email": "vaishali@example.com",
  "phone": "9876543210",
  "address": "Mumbai"
}
</code></pre>
<p>The frontend receives this response and uses the required information to display the profile.</p>
<p>This approach is straightforward and works well for many applications.</p>
<p>However, things can get a little complicated when the frontend needs different combinations of related data.</p>
<h2>What Are Over-fetching and Under-fetching?</h2>
<p>Let's understand these two problems using the same example.</p>
<h3>Over-fetching</h3>
<p>Over-fetching happens when an API returns more data than the client actually needs.</p>
<p>Imagine our profile card only displays the user's name.</p>
<p>However, the REST API returns:</p>
<pre><code class="language-json">{
  "id": 1,
  "name": "Vaishali",
  "email": "vaishali@example.com",
  "phone": "9876543210",
  "address": "Mumbai",
  "createdAt": "2026-01-10"
}
</code></pre>
<p>Our UI only needs:</p>
<pre><code class="language-json">{
  "name": "Vaishali"
}
</code></pre>
<p>We receive several additional fields that our UI doesn't even use.</p>
<p>This is called over-fetching.</p>
<h3>Under-fetching</h3>
<p>Under-fetching happens when a single API request doesn't provide all the data required by the frontend.</p>
<p>Imagine our profile page needs:</p>
<ul>
<li>User name</li>
<li>User email</li>
<li>Registered event names</li>
<li>Event dates</li>
</ul>
<p>We first request:</p>
<pre><code class="language-http">GET /users/1
</code></pre>
<p>The response contains:</p>
<pre><code class="language-json">{
  "id": 1,
  "name": "Vaishali",
  "email": "vaishali@example.com"
}
</code></pre>
<p>But where are the registered events?</p>
<p>We need another request:</p>
<pre><code class="language-http">GET /users/1/events
</code></pre>
<p>Now we're making multiple requests just to collect the information needed for one screen.</p>
<p>This is an example of under-fetching.</p>
<p><strong>Note:</strong> These are common possibilities with REST APIs, not unavoidable limitations. REST endpoints can also be designed to return related data together or support field selection.</p>
<h2>How Does GraphQL Approach Data Fetching Differently?</h2>
<p>GraphQL is a query language for APIs and a runtime for fulfilling those queries with data.</p>
<p>Unlike traditional REST API designs, GraphQL allows the client to specify exactly which fields it needs.</p>
<p>Let's take our previous example.</p>
<p>We want to display:</p>
<ul>
<li>User name</li>
<li>User email</li>
<li>Registered event names</li>
<li>Event dates</li>
</ul>
<p>With GraphQL, we can write a single query:</p>
<pre><code class="language-graphql">query {
  user(id: "1") {
    name
    email
    events {
      eventName
      date
    }
  }
}
</code></pre>
<p>The server can return:</p>
<pre><code class="language-json">{
  "data": {
    "user": {
      "name": "Vaishali",
      "email": "vaishali@example.com",
      "events": [
        {
          "eventName": "React Meetup",
          "date": "2026-12-10"
        },
        {
          "eventName": "GraphQL Workshop",
          "date": "2026-12-15"
        }
      ]
    }
  }
}
</code></pre>
<p>Notice something interesting?</p>
<p>We requested only the fields we needed, and the response follows the structure of our query.</p>
<p>We also didn't need to make separate frontend requests for the user and their events.</p>
<p>That's one of the main advantages of GraphQL: <strong>the client gets more control over the shape of the response.</strong></p>
<p>One important thing to understand is that a single GraphQL request doesn't necessarily mean a single database query.</p>
<p>Behind the scenes, the server might still fetch information from multiple tables, databases or other data sources to prepare the response.</p>
<p>GraphQL doesn't require a completely different database structure either. It works as a layer between the client and the underlying data sources.</p>
<p>We'll explore how this actually works in the next blog.</p>
<h2>REST vs GraphQL: A Practical Comparison</h2>
<p>Let's quickly compare both approaches.</p>
<table>
<thead>
<tr>
<th>Feature</th>
<th>REST</th>
<th>GraphQL</th>
</tr>
</thead>
<tbody><tr>
<td>API structure</td>
<td>Resource-based endpoints</td>
<td>Commonly a single endpoint</td>
</tr>
<tr>
<td>Data fetching</td>
<td>Response generally defined by the endpoint</td>
<td>Client specifies requested fields</td>
</tr>
<tr>
<td>Over-fetching</td>
<td>Can occur</td>
<td>Reduced through field selection</td>
</tr>
<tr>
<td>Under-fetching</td>
<td>May require multiple requests</td>
<td>Related data can be requested together</td>
</tr>
<tr>
<td>Response structure</td>
<td>Defined by the server endpoint</td>
<td>Follows the requested query structure</td>
</tr>
<tr>
<td>Caching</td>
<td>Well-supported HTTP caching</td>
<td>Requires additional caching strategies</td>
</tr>
<tr>
<td>Versioning</td>
<td>Often uses URL versioning</td>
<td>Can evolve through schema changes</td>
</tr>
</tbody></table>
<p>Neither approach is perfect for every situation.</p>
<p>GraphQL also introduces challenges, such as query complexity, caching, authorization and the N+1 problem.</p>
<p>Choosing between them depends on the application's requirements.</p>
<h2>When Should You Use GraphQL or REST?</h2>
<p><strong>GraphQL can be useful when:</strong></p>
<ul>
<li>An application has complex relationships between data.</li>
<li>Different screens require different combinations of fields.</li>
<li>Multiple clients, such as web and mobile applications, consume the same API.</li>
<li>Developers need more flexibility when fetching data.</li>
</ul>
<p>For example, our developer community platform might have different screens for users, event organisers and speakers. Each screen needs a different combination of related data.</p>
<p>GraphQL can make handling these requirements easier.</p>
<p><strong>REST can be a better choice when:</strong></p>
<ul>
<li>The application has simple CRUD operations.</li>
<li>Resources have straightforward endpoint structures.</li>
<li>Standard HTTP caching is important.</li>
<li>The team already has a well-established REST architecture.</li>
</ul>
<p>For example, a simple blog application with endpoints to retrieve posts, create posts and delete posts might not need the additional complexity of GraphQL.</p>
<p>Sometimes, keeping things simple is the best architectural decision.</p>
<h2>What I Learned</h2>
<p>While exploring REST and GraphQL, one thing that stood out to me was that GraphQL isn't about replacing REST everywhere.</p>
<p>Both approaches solve the problem of communication between applications and servers, but they approach data fetching differently.</p>
<p>REST structures API access around resources and endpoints, whereas GraphQL allows clients to describe the data they need through queries.</p>
<p>I also learned that making one GraphQL request doesn't necessarily mean executing just one database query.</p>
<p>This was my first step in exploring GraphQL beyond just writing queries and mutations.</p>
<p>There's still a lot more to learn, and I'm excited to experiment with implementations and contribute to the open-source ecosystem.</p>
<p>In the next blog, I'll explore the building blocks of GraphQL, including:</p>
<ul>
<li>Schema design and GraphQL types</li>
<li>Queries and mutations</li>
<li>Resolvers and context</li>
<li>The N+1 problem and why it matters for GraphQL performance</li>
</ul>
<hr />
<p><strong>Part of my Exploring GraphQL learning journey.</strong></p>
]]></content:encoded></item></channel></rss>