<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rahul Aluru on Medium]]></title>
        <description><![CDATA[Stories by Rahul Aluru on Medium]]></description>
        <link>https://medium.com/@rahulaluru3?source=rss-945444c86ac9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0jh7l9Nef4NcHtEKG6n2cA.jpeg</url>
            <title>Stories by Rahul Aluru on Medium</title>
            <link>https://medium.com/@rahulaluru3?source=rss-945444c86ac9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 03:21:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rahulaluru3/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Rest API vs GraphQL API]]></title>
            <link>https://medium.com/@rahulaluru3/rest-api-vs-graphql-api-86f1ac27bd31?source=rss-945444c86ac9------2</link>
            <guid isPermaLink="false">https://medium.com/p/86f1ac27bd31</guid>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Rahul Aluru]]></dc:creator>
            <pubDate>Wed, 01 Jul 2020 20:53:18 GMT</pubDate>
            <atom:updated>2020-07-02T10:29:09.953Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What is a Web API?</strong><br>A Web API is an API that backend developers expose on servers to which we can send HTTP requests from a client to fetch data or send data. <br>In other words, it’s a web service that exposes certain end-points where clients can fetch data or send data. So a Web Service or an API is the way our client and server talk.<br>Both Rest and GraphQL are two ways to build an API but the way how the client queries the data changes in these two scenarios.</p><p><strong>What is a Rest API?<br></strong>A Representational State Transfer API also called Rest API uses an architecture style where there are multiple endpoints that are exposed by your web service where each endpoint has its own “HTTP verb”, “path” and “body”. Then again there are constraints on how an endpoint is defined for instance there are 5+1 HTTP verbs namely, GET, POST, PUT, PATCH, DELETE, and OPTIONS. <br>HTTP Verbs:<br>1) GET:- Get a Resource(s) from the server.<br>2) POST:- Create or Append a Resource from the server.<br>3) PUT:- Create or OverWrite a Resource from the server.<br>4) PATCH:- Updating a Resource from the server.<br>5) DELETE:- Delete or Remove a Resource from the server.<br>We generally use PUT to edit an existing resource. Though, we can create a resource using PUT is not generally recommended as PUT is idempotent and POST is not.<br>An HTTP request is called Idempotent when you can make multiple identical requests on the same row and does not change the server state. So, PUT is not generally used for creation. But then again if you use PUT for the creation and if you PUT an object twice it has no effect on the database. So, this might be one of the properties you can consider while deciding between PUT and POST.</p><p>So typically an endpoint or a URL looks like this,<br><strong>GET: &lt;domainName&gt;/&lt;resourceName&gt;/:id</strong>, which fetches a resource having the specified “id”. A GET request can also be without an “id” in which case, querying the endpoint gives us all the resources of that &lt;resourceName&gt;.<br>So for a better understanding lets consider there is a resource called books, in this case, we can translate the query above to,<br><strong>GET: &lt;domainName&gt;/books/1</strong>, which fetches us a book having an id “1”.<br>So get doesn’t need a body. A body comes into the play when we want to send something over to the server in other words create or update a resource. In these cases, the body is just a JSON object containing the fields required for the action. <br>So a typical post request would look like this,<br><strong>POST: &lt;domainName&gt;/books {id:1, name: “Harry Potter”, author: “JK Rowling”}</strong>, this request creates a resource “Book” with the specified fields in the body. But in real-time you don’t want to hardcode the id in the body but instead, use auto-increment or let the DB server put in an Object ID.</p><h4><strong>What is a GraphQL API?</strong></h4><p>GraphQL API uses an architecture style where there is only one endpoint or URL from which the client fetches or sends data to or from the server. So there is only one HTTP verb i.e., “POST” and only one path i.e., “/graphql” following your domain name and the third part is “body” which contains a GraphQL query.<br>So for every request that you send out to a GraphQL API has fixed Http verb and path, unlike the Rest API. So the only thing that defines what action the server request performs is the third part which is the body that contains the GraphQL query. The query is parsed by the server and then server decides what to return</p><p>A GraphQL Query contains:<br>1) Operation “type”: {Query, Mutation, Subscription}<br>2) Operation “endpoint”: The resource that the client intends to talk to. (the resource that you specify should be mutatable on the server-side)<br>3) Requested fields: In simpler terms, this is to specify the fields of the resource that we are working on.</p><p>So, let&#39;s look at one example. For, instance lets take our <strong>books</strong> resource that we were talking about above. Let&#39;s just write a GraphQL query to fetch all the book names along with their author names,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/263/1*xBxlczwWHQPV9zZniSSv5w.png" /><figcaption>GraphQL query</figcaption></figure><p>So the operation type is “query” which means we are trying to read the data from API. This particular query fetches details of all the resources matching the endpoint i.e., “books”, the details of each book consists of its name and author’s name.<br>There are two other operation types namely, “mutation” and “subscription”.<br>Mutations are used when you want to create or add or update some data on the API. <br>Subscriptions are used when you want to subscribe to a resource that returns you live updates on the requested resource and these are done through web-sockets.<br>Also while writing queries in GraphQL you have to make sure that the endpoint you’re accessing is exposed by the API because some endpoints are not exposed as their owners want them to be immutable.</p><p>The major differences between these two ways of writing APIs are,<br>1) The requested field list: GraphQL allows you to specify the field list that you wanna fetch and it does not fetch any extra details about the resource. Whereas, Rest API doesn&#39;t give you an option and which takes a little more time.<br>2) As a developer, you need not know the structure of data that is stored on the backend when you are consuming Rest API whereas you have to know the structure if you are using GraphQL. You have to know the structure since not every model on the backend is available for you to fetch and sometimes you just make use of relationships between resources to fetch something if it isn&#39;t exposed.</p><p>These two styles of writing an API have their own pros and cons but when we are writing APIs how to decide to choose is the question that developers often think about. This might seem to be a big question but the answer to this question is simple. It depends on the situation and the type of application that you are developing. To explain this further let&#39;s analyze different scenarios,</p><ol><li>Let’s say your application needs a lot of data on every page, then choosing Restful service would be sensible because the fields to exclude from the request list in case of GraphQL query comes down and it eventually becomes as time-taking as Rest API.</li><li>Rest API is more clear than the GraphQL API and also Rest is easier to work with because you don’t have to know a separate query language, you just need to know how to access an endpoint. Also, in case of Rest you don’t need to worry about how the structure looks on the backend.</li><li>Using Rest API you don’t have to use any extra tools to work with endpoints whereas for GraphQL you need a special tool to work with endpoints. For example, we use ApolloCient for React and Angular.</li><li>If your application bandwidth is something that you really wanna optimize then GraphQL type of API should be used.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86f1ac27bd31" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>