<?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 Shriram Balakrishnan on Medium]]></title>
        <description><![CDATA[Stories by Shriram Balakrishnan on Medium]]></description>
        <link>https://medium.com/@shriramrbk?source=rss-9effcf63c5bc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iayRjEBwJoKms6XSLVO0ew.jpeg</url>
            <title>Stories by Shriram Balakrishnan on Medium</title>
            <link>https://medium.com/@shriramrbk?source=rss-9effcf63c5bc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 20:36:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shriramrbk/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[Authorization in GraphQL — Schema Directive vs GraphQL Shield]]></title>
            <link>https://levelup.gitconnected.com/authorization-in-graphql-schema-directive-vs-graphql-shield-17085f346488?source=rss-9effcf63c5bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/17085f346488</guid>
            <category><![CDATA[authorization]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[apollo-server]]></category>
            <dc:creator><![CDATA[Shriram Balakrishnan]]></dc:creator>
            <pubDate>Sun, 18 Oct 2020 21:27:08 GMT</pubDate>
            <atom:updated>2020-10-19T02:47:40.112Z</atom:updated>
            <content:encoded><![CDATA[<h3>Authorization in GraphQL — Schema Directive vs GraphQL Shield</h3><blockquote>This blog also assumes that you are familiar with GraphQL Schema Directives. If not, I recommend reading this <a href="https://www.apollographql.com/docs/apollo-server/schema/directives/">Apollo documentation</a> on Schema Directive.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*mhMF50PSG7XY9YWqlp-Wuw.png" /></figure><p>Let’s say we have a basic <strong>Blogs</strong> application in which an User hasMany Posts and each Post in turn hasMany Comments.</p><p>Below is the gist containing the basic GraphQL schema of this application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ce0346c9f2a3b9ec3ceb8b73476bbcf/href">https://medium.com/media/9ce0346c9f2a3b9ec3ceb8b73476bbcf/href</a></iframe><h3>Problem Statement</h3><p>Let’s say that we want only the logged in users with USER role to access the comments. How can we implement this?</p><h3>Approach 1 — Using Schema Directive</h3><p>Authorization for queries, mutations and types can be implemented using Schema Directives. In fact, <a href="https://www.apollographql.com/docs/apollo-server/schema/creating-directives/#enforcing-access-permissions">this section</a> of Apollo documentation takes us through an example of implementing authorization using directives.</p><p>Let’s implement an auth directive in the same manner as described in Apollo docs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bd92ec97946e6b4fe050d7a5943160f/href">https://medium.com/media/1bd92ec97946e6b4fe050d7a5943160f/href</a></iframe><p>In the above code, note that the presence of requiredRole has been checked. requiredRole has to be passed as parameter wherever we use the auth directive.</p><blockquote>Also, logging of isUser text has been done so that we will know exactly how many times this function is getting executed.</blockquote><p>Changes to the baseTypeDefs.js will be as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd18c77d94c7b10028065cda73dbd6cd/href">https://medium.com/media/cd18c77d94c7b10028065cda73dbd6cd/href</a></iframe><p>Note the usage of @auth directive for CommentType. For a full working example you can refer <a href="https://github.com/shrirambalakrishnan/blog-graphql-optimizations/tree/auth-using-directive">this repository</a>.</p><p>Here is the screenshot of server log when the users query is run.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4LKGCYGYM0_lUaq_Ov7r9A.png" /></figure><p>Now, as you can see, <strong>@auth</strong> directive is getting executed once for every CommentType record.</p><p>Is this repeated execution even needed? How can this be optimized?</p><h3>Approach 2: graphql-shield — A Better Alternative</h3><p>The creation of authorization rules are very intuitive with <a href="https://github.com/maticzav/graphql-shield">GraphQL Shield</a>.</p><p>In the code given below, I have created a isUser permission for CommentType</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cc97337fc94a3fdd5bab04fa3b0d35b/href">https://medium.com/media/4cc97337fc94a3fdd5bab04fa3b0d35b/href</a></iframe><p>In the isUser rule, note the use of <strong>contextual cache</strong> which ensures that the result of this rule is cached for this specific context and <strong>thus it prevents unnecessary data loading</strong>.</p><p>For full implementation you can refer <a href="https://github.com/shrirambalakrishnan/blog-graphql-optimizations/tree/auth-using-shield">this repository</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xNzfQsOYWGoPk1trTZqKgQ.png" /></figure><p>Upon executing the same query, you can see that `isUser` is getting executed only once.</p><p>Here are a few resources to read more on this topic.<br>1. GraphQL Summit 2020 Talk— <a href="https://youtu.be/dBuU61ABEDs">How to Auth</a><br>2. Apollo’s Official <a href="https://www.apollographql.com/blog/setting-up-authentication-and-authorization-with-apollo-federation/">Blog</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17085f346488" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/authorization-in-graphql-schema-directive-vs-graphql-shield-17085f346488">Authorization in GraphQL — Schema Directive vs GraphQL Shield</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solve N+1 query problem in GraphQL with Dataloaders]]></title>
            <link>https://levelup.gitconnected.com/solve-n-1-query-problem-in-graphql-with-dataloaders-18e16ac17b21?source=rss-9effcf63c5bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/18e16ac17b21</guid>
            <category><![CDATA[apollo-server]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[dataloader]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Shriram Balakrishnan]]></dc:creator>
            <pubDate>Sat, 01 Aug 2020 19:37:08 GMT</pubDate>
            <atom:updated>2020-08-02T14:41:24.444Z</atom:updated>
            <content:encoded><![CDATA[<h3>Solve N+1 query problem in GraphQL with Dataloader</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*mhMF50PSG7XY9YWqlp-Wuw.png" /></figure><p><em>This is a continuation of my </em><a href="https://levelup.gitconnected.com/do-not-resolve-your-graphql-fields-like-a-rest-endpoint-ac43242f269"><em>previous blog</em></a><em> that explained a better way to resolve the fields of a GraphQL query. That blog ended with a note on N+1 query problem faced while resolving the fields.</em></p><p>In this blog post, we will see how to solve that N+1 query problem using <a href="https://github.com/graphql/dataloader">Dataloader</a>.</p><h3>What is Dataloader?</h3><p>Dataloader is a library that batches consecutive requests and makes a single data request under the hood. This request can be made to any data source, like a database or a web service.</p><blockquote>A Dataloader takes in an array as argument, processes data using that argument and returns an array of objects.</blockquote><p>The element at the nth index of the returned array will be considered by the dataloader as the data for the nth element in the input argument.</p><p>Let’s implement postsLoader now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd4f1ea2b7ce085416e258c1367d73ad/href">https://medium.com/media/cd4f1ea2b7ce085416e258c1367d73ad/href</a></iframe><p>We will now use this postsLoader to resolve the posts. The updated resolvers will be as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a62b793f306f50e0c00c5beae6df58d/href">https://medium.com/media/4a62b793f306f50e0c00c5beae6df58d/href</a></iframe><p>When the client queries for users along with the posts fields, then for every user resolved in the `users` query, this `posts` resolver will be called with the parent argument being equal to the user object. Using this user, we can find the posts.</p><p>As you can see from the resolver of `posts` field, we are still querying for a single post using the dataloader’s `load` API.</p><blockquote>Multiple postsLoader.load() calls will be batched and then `postsLoader` will be called only once.</blockquote><p>Here is the screenshot showing the DB queries hit when client requests users data along with their posts data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m6l4runqUtMT6j1CzvHvXA.png" /><figcaption>This ensures that we hit the database only once to fetch our data and hence solving our N+1 query problem.</figcaption></figure><h3>One caveat with this implementation of dataloader though.</h3><p>Dataloader not just batches requests, but it also caches the responses. It does caching to ensure that another query is not made to fetch the same data.<br> <br>The problem with this caching is that if another request’s resolver hits this dataloader too (when first request is being processed), then the <strong>cached result will be returned to the second request too</strong>. As you can see, returning the cached data to a completely different request is a serious problem.<br> <br><strong><em>Hence the dataloader should always be defined per request.</em></strong></p><p>This way, different instances of the dataloader will be used by different requests. The best way is to define them in the context of the GraphQL request.</p><p>Thus, the new implementation of the dataloader will be as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/873383e9b0bd1d433b7abb0f6b646037/href">https://medium.com/media/873383e9b0bd1d433b7abb0f6b646037/href</a></iframe><p>Hope this was helpful. Check out this <a href="https://github.com/shrirambalakrishnan/blog-graphql-optimizations">repo</a> that has the implementation of above-mentioned users resolver using Apollo GraphQL server.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18e16ac17b21" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/solve-n-1-query-problem-in-graphql-with-dataloaders-18e16ac17b21">Solve N+1 query problem in GraphQL with Dataloaders</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Do not resolve your GraphQL fields like a REST endpoint]]></title>
            <link>https://levelup.gitconnected.com/do-not-resolve-your-graphql-fields-like-a-rest-endpoint-ac43242f269?source=rss-9effcf63c5bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac43242f269</guid>
            <category><![CDATA[apollo-server]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[graphql-resolver]]></category>
            <dc:creator><![CDATA[Shriram Balakrishnan]]></dc:creator>
            <pubDate>Sun, 21 Jun 2020 19:23:16 GMT</pubDate>
            <atom:updated>2020-06-23T21:07:13.460Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*mhMF50PSG7XY9YWqlp-Wuw.png" /></figure><p>The advantages of using GraphQL are enormous. Below are a few of them.</p><p>1. Same API can be used by multiple clients as we can conditionally query the necessary data from an API.<br>2. Reduced data usage by the client as only required data is transferred from the server.<br>3. Backend API is not tightly coupled with where it is used.</p><p>But one of the most important advantages from a backend perspective is the following:</p><blockquote><strong>Reduced processing by the backend server based on the client’s query.</strong></blockquote><p>Note that this is not something that is provided out of the box by GraphQL. If you do not code it the right way, then the load on your backend server will always be the same irrespective of the client’s query.</p><p>That is, you should not write code in the same way you would write a REST endpoint.</p><h3>Scenario</h3><p>Let’s look at an example consisting of users and their posts. Below are the structures of two tables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hwJcg2I15W6rx3DQUjP8Aw.png" /><figcaption>Users table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D_jqzkXUJyC8sS_Sf_S4nw.png" /><figcaption>Posts table</figcaption></figure><p>Let’s say I need a resolver to list the users along with their posts. How would I write this resolver?</p><blockquote>Note: I will be writing a NodeJS app with Apollo GraphQL server and Sequelize to explain this problem and solution.</blockquote><h3>One possible solution</h3><p>You can define the schema as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c89c8f238c4dc63b5f21ca48214f6ed9/href">https://medium.com/media/c89c8f238c4dc63b5f21ca48214f6ed9/href</a></iframe><p>And your user&#39;s resolver can be as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1449f529c5f029e0f4e4af144951781/href">https://medium.com/media/a1449f529c5f029e0f4e4af144951781/href</a></iframe><p>Sure this solves our requirement. <strong><em>But is this efficient?</em></strong></p><p>Let’s have a look at the logs under 2 cases.</p><blockquote>Note: The Left Hand Side (LHS) of the following screenshots contains the query executed and it’s result. The Right Hand Side (RHS) contains the application logs executed while executing the GraphQL query.</blockquote><h4>Case 1 — Query users with posts data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LXcVq1iwLc604tnpFsDmhQ.png" /><figcaption>Query users data with their posts data</figcaption></figure><h4>Case 2 — Query users without posts data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B3uEdiMQXGR5gHnMr-qIaw.png" /><figcaption>Query users without posts data</figcaption></figure><blockquote><strong>As you can see from the logs, the query to fetch posts is always being executed irrespective of the client’s query. That is, even when client do not request the posts data.</strong></blockquote><p>So how can we optimize this?</p><h3>A Better Solution</h3><p>GraphQL allows us to resolve every field present inside a field. This is what we need to do.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7cc048dead3c91015b8586fb46a4a73/href">https://medium.com/media/e7cc048dead3c91015b8586fb46a4a73/href</a></iframe><p>As you can see, we are resolving the field posts of UserType (which is the type returned by the users resolver). This allows the GraphQL server to resolve this field only when it is queried explicitly by the client.</p><h4>Case 1 — Query users with posts data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WyxcxXERDkbT8L3WZHJtTQ.png" /></figure><h4>Case 2— Query users without posts data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nEGg51ILEYBkQWSCYXjLdg.png" /></figure><h3>And as it is evident in the logs, when posts field is not queried, the query to fetch posts is also not getting executed.</h3><p>Now you might also have noticed that <strong>multiple queries are being executed to fetch posts. <em>That is, one post query per user is being fired</em>.</strong> This is the problem we will be solving in the next blog. For the curious few, here is a small hint — you might have to take a look at <a href="https://github.com/graphql/dataloader">Dataloaders</a>.</p><p>I hope this is useful. Stay tuned for the next blog!</p><p><a href="https://skilled.dev/">Coding Interview Questions + Land Your Dev Job | Skilled.dev</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac43242f269" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/do-not-resolve-your-graphql-fields-like-a-rest-endpoint-ac43242f269">Do not resolve your GraphQL fields like a REST endpoint</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Don’t make happiness your quest]]></title>
            <link>https://medium.com/@shriramrbk/dont-make-happiness-your-quest-34918a43549f?source=rss-9effcf63c5bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/34918a43549f</guid>
            <dc:creator><![CDATA[Shriram Balakrishnan]]></dc:creator>
            <pubDate>Tue, 13 Aug 2019 20:36:47 GMT</pubDate>
            <atom:updated>2020-03-15T05:44:40.629Z</atom:updated>
            <content:encoded><![CDATA[<p>When Captain America lifted the Mjolnir in the Avengers Endgame or when Thor entered the Battle of Wakanda in the Avengers Infinity War, the audience in every screen went berserk. Those are the moments every fan lives for. To see their favourite character break boundaries and achieve the impossible.</p><p>Each of us would have felt the rush of excitement when those scenes unfolded.</p><p>This happiness is what everyone of us try to experience in our life. To get that, some go on an adventure, some try out different foods, some try to get the same from their work. We feel that by doing something or the other we will achieve this state of happiness.</p><p>This means that you are literally making your own happiness a consequence of what happens in the outside.</p><p>But being happy is the single most important thing that could change the whole way in which you experience everything in the world.</p><p>The very way in which you interact with people around you will improve. As you are happy, you naturally have the best interest for people around you instead of being stuck in your own little world.</p><p>The way in which you work will really improve too. You no longer work to achieve happiness or this or that. As you are already happy, you work just to experience the work to it’s fullest.</p><p>And being happy is really easy. Just be in the present moment and experience things the way they are.</p><p>And when someone asks you “How’s life?”, you will be able to reply from the bottom of your heart, “Beautiful”.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34918a43549f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rails ActionCable — Bare Minimum Implementation]]></title>
            <link>https://medium.com/reportbee/rails-actioncable-bare-minimum-implementation-b168f7456723?source=rss-9effcf63c5bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/b168f7456723</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[report-bee]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[implementation]]></category>
            <category><![CDATA[actioncable]]></category>
            <dc:creator><![CDATA[Shriram Balakrishnan]]></dc:creator>
            <pubDate>Thu, 22 Nov 2018 10:57:01 GMT</pubDate>
            <atom:updated>2018-11-22T10:57:01.304Z</atom:updated>
            <content:encoded><![CDATA[<h3>Rails ActionCable — Bare Minimum Implementation</h3><blockquote>Before the final blog of the GraphQL Series, it is important to understand one of the major Rails 5 feature —<strong> ActionCable</strong>.</blockquote><h4>What is ActionCable</h4><p>ActionCable was introduced in Rails 5. Rails founder, DHH, explains in this <a href="https://www.youtube.com/watch?v=KJVTM7mE1Cc">video</a> about the way in which Basecamp used polling to display notifications and how ActionCable solved this problem.</p><p>ActionCable uses websockets, which means that once connection is established, data can be sent from server to client right away (as long as socket connection exists) instead of a time-consuming request-response cycle (if HTTP was used).</p><h4>The Publisher/Subscriber Pattern</h4><p>ActionCable works on the basis of publisher-subscriber design pattern where the server publishes the data to channel. All the consumers subscribed to this data will receive this data. It has both server and client side components.</p><ul><li>At the server end, a channel needs to be created.</li><li>Once connection is established between consumer and server (through websocket), the consumer needs to subscribe to channels to which it wants to listen. At this point, it becomes the subscriber of the channel.</li><li>When an event happens, the server needs to publish data to this channel.</li><li>All the subscribers will then receive the data published to this channel.</li></ul><p>ActionCable makes this entire flow possible easily. The connection part will already be created through the code in cable.js file. The channel creation, publishing to channel and creating a subscriber needs to be done.</p><h4>What’s in this blog?</h4><ul><li>We will implement ActionCable in the <a href="https://github.com/shrirambalakrishnan/movies_app">movies_app</a> repository and publish and display any newly created movie to user.</li><li>You can work on top of the repo’s master branch to implement the code samples given in this blog.</li></ul><h4>Initial Steps</h4><p>Create Movies controller and create index action which displays list of all movies.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0767de144be38e68fd2717660b3657ba/href">https://medium.com/media/0767de144be38e68fd2717660b3657ba/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4058db6e294734f9d1503271a88a1807/href">https://medium.com/media/4058db6e294734f9d1503271a88a1807/href</a></iframe><h4>Channel Creation</h4><p>It’s time to create our channel which will be used to publish a newly created movie.</p><p>Create a file app/channels/movies_channel.rb and paste the following code. The subscribed method will be invoked whenever a consumer subscribes to this channel. Upon successful subscription, the data from movies channel will be streamed to that subscriber too.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a25fd1e027610bdb6aca819d40227fb4/href">https://medium.com/media/a25fd1e027610bdb6aca819d40227fb4/href</a></iframe><h4>Subscription From Client Side</h4><p>Create a file app/assets/javascripts/cable/subscriptions/movies.coffee and paste the following coffeescript code. The code subscribes to the MoviesChannel. When server sends new movie data, it adds the new movie data to the list of movies. Thus, user will be able to view newly added movies without reloading the page.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c85f5223ebb580bf2e6847087a92f62f/href">https://medium.com/media/c85f5223ebb580bf2e6847087a92f62f/href</a></iframe><h4>Publishing New Movie Data</h4><p>Upon new movie creation using the create_movie mutation, publish the movie data to the movies channel. Update the code in app/graphql/resolvers/create_movie.rb to the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39d06b127c16324aa27d80afb2b57f8d/href">https://medium.com/media/39d06b127c16324aa27d80afb2b57f8d/href</a></iframe><p>Note the ActionCable.server.broadcast to movies line.</p><p>It’s time to run the app and see the ActionCable’s magic.</p><p>Below image displays the initial data that displays movies GraphQL query data on left and MoviesController#index action’s view on right.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MMEwYKgkPR3tMcQhV8oHCg.png" /><figcaption>Initial Data</figcaption></figure><p>Now, as soon as, new movie is created using the create_movie GraphQL mutation, the newly created movie will be published and it will be received by the subscriber. This data will be instantaneously displayed on the MoviesController#index action’s view on the right side.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XRG9jhpFdlfhDCANj9_ftQ.png" /><figcaption>New Movie Creation</figcaption></figure><h4>Conclusion</h4><p>This basic implementation of ActionCable will help in understanding its core concept. You can try out authentication and authorisation on top of this to get to know this better. The detailed explanation on ActionCable can be found in its <a href="https://guides.rubyonrails.org/action_cable_overview.html">Rails doc</a>. This understanding is also necessary for understanding the final blog of the <a href="https://medium.com/reportbee/graphql-blog1-bbdf25ceda39">GraphQL series</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b168f7456723" width="1" height="1" alt=""><hr><p><a href="https://medium.com/reportbee/rails-actioncable-bare-minimum-implementation-b168f7456723">Rails ActionCable — Bare Minimum Implementation</a> was originally published in <a href="https://medium.com/reportbee">reportbee</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL — Core Concepts (Blog #2 of 3)]]></title>
            <link>https://medium.com/reportbee/graphql-blog2-core-concepts-5d2b0beb1b6?source=rss-9effcf63c5bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d2b0beb1b6</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[report-bee]]></category>
            <category><![CDATA[query]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Shriram Balakrishnan]]></dc:creator>
            <pubDate>Thu, 27 Sep 2018 07:21:49 GMT</pubDate>
            <atom:updated>2018-09-28T11:59:19.143Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This is the second blog in the GraphQL series. The </em><a href="https://medium.com/reportbee/graphql-blog1-bbdf25ceda39"><em>first blog</em></a><em> gives a basic introduction of GraphQL.</em></p><p>Now that we are aware of the upsides of using GraphQL, we will dive right into implementing a GraphQL API in Rails app. We will learn necessary concepts along the way.</p><h3>App setup</h3><p>We will need an app to implement and try out GraphQL APIs. I have created an app that:</p><ol><li>uses graphql and graphiql-rails gems</li><li>contains the basic models (Movie, Actor and Cast) and seed data that will be helpful for us in this learning process.</li></ol><p>On top of this app, you can try out the code given in this tutorial.</p><h4><strong>To set up this app</strong></h4><ol><li>Clone the app from <a href="https://github.com/shrirambalakrishnan/movies_app">this</a> repo.</li><li>Switch to basic_setup branch.</li><li>Setup database.yml.</li><li>Run rails db:setup.</li><li>Run rails db:seed.</li></ol><h3><strong>Let’s get started</strong></h3><p>Run the installer for graphql gem.</p><p>rails generate graphql:install</p><p>This will create GraphqlController and expose the graphql endpoint (which will be pointing to GraphqlController#execute action). In addition, files for defining queries, mutation and schema will be generated.</p><p>Now we will start with implementing GraphQL queries.</p><h3><strong>Queries</strong></h3><p>Queries are used to fetch data from the server.</p><h4><em>First Query</em></h4><p>Now, to keep things simple, let’s try to implement a <strong>GraphQL API that returns the details of first movie</strong>.</p><p>To understand how query works, we need to know the following two important points.</p><ol><li>The app/graphql/types/query_type.rb will hold the end point for all the root level fields. This means that if you have to query first_movie, then you will have to define it here.</li><li>GraphQL APIs are also strongly typed. This means that the fields and its data-types needs to be defined. Only the defined field’s data will be returned by the server (if it is requested by the query).</li></ol><p>So let’s define the movie type here. Create movie_type.rb file under graphql/types. Include the below following code in it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a4ca463752ce8b86552afedab185607/href">https://medium.com/media/3a4ca463752ce8b86552afedab185607/href</a></iframe><p>Let’s breakdown this code now. You are creating a new type, MovieType and specifying the fields that can be returned upon a query’s request. You are also mentioning the data-type of each and every field. The ! before the data-type means that it can’t be of null value.</p><p>The next step is to define the first_movie root field. As mentioned earlier, query_type.rb will hold all the end points for root queries. Include the following code in the file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad3631d9a8e4c691a4181e1ad7a7e5bf/href">https://medium.com/media/ad3631d9a8e4c691a4181e1ad7a7e5bf/href</a></iframe><p>The above code defines first_movie field which can be queried.</p><ol><li>It is of MovieType which means that only the 4 fields (id, name, genre and year) mentioned in movie_type.rb is query-able.</li><li>The resolve block returns the object(s) for which the fields mentioned in MovieType can be returned by the server. In this case, it returns a single Movie object.</li><li>The parameters to resolve block are object, arguments and context. We will see more on this, in the later parts of this series.</li></ol><p>Lets try out our first GraphQL API to fetch the first movie from database using the Grahiql’s UI. In Graphiql, you can edit the queries on the left side and the response from server appears on the right side. It’s a great tool for development.</p><p>You construct the GraphQL query within query {} . This tells the server that it is a GraphQL query. Inside first_movie query, we have given all the required attributes namely, id, name, genre and year. Hence all the fields will be returned. If your page needs only the name and genre, then you can remove it from the query and only the queried fields will be returned by the server.</p><p>Here is the input and output from Graphiql.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w4MPq0G0MjV80i3cU0CM8w.jpeg" /><figcaption>First Query — first_movie</figcaption></figure><h4><em>Second Query</em></h4><p>The first query is only for learning purpose. There is no actual use in fetching the first movie alone. So, let’s try to implement an API that returns all the movies present in the database - something that can be used in movies listing page.</p><p>For this, we can make use of the existing type, MovieType. In the query_type.rb, define the movies field as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20a4ade339aba575639f4378e54d39e8/href">https://medium.com/media/20a4ade339aba575639f4378e54d39e8/href</a></iframe><p>The data-type of movies allows multiple movies to be returned by specifying array of MovieType as its type. The Graphiql output for this query will be as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nUs78ZtLHSnMF4Qj9e_kbg.png" /><figcaption>Second Query — movies</figcaption></figure><h4><em>Second Query — Improved</em></h4><p>The above API can be used in the movies listing page. But what if the you want also want the actors’ names returned by the same API. We can improve the existing API for this requirement.</p><p>In order to add actors details, as always, we need to add ActorType and add a field in movies root query type named actors .</p><p>Create app/graphql/types/actor_type.rb and add the following code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de060895b7f02d87a0f860fd38cd6a34/href">https://medium.com/media/de060895b7f02d87a0f860fd38cd6a34/href</a></iframe><p>Update the graphql/types/movie_type.rb with this code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b91870ce1ff2e3c2f3211931dca50079/href">https://medium.com/media/b91870ce1ff2e3c2f3211931dca50079/href</a></iframe><p>This allows us to query movies with actors data. Following is the graphiql’s result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vaos9RI_S7S39k17fjrUZQ.png" /><figcaption>Second Query Improved — movies</figcaption></figure><p>Now we can move on to mutations.</p><p><strong>Mutations:</strong></p><p>Mutations are used for creating/updating database records.</p><p>Let’s write a mutation to create a movie.</p><p>The app/graphql/mutation_type.rb will hold the root fields for mutations (just like app/graphql/query_type.rb was handling the queries).</p><p>Place the following code in `mutation_type.rb’.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/764e1bf4fcb35563f3f1a20a4a8f07cc/href">https://medium.com/media/764e1bf4fcb35563f3f1a20a4a8f07cc/href</a></iframe><p>This exposes create_movie mutation. Note the resolver function that’s been used. This is the function that:</p><ol><li>Defines the arguments that can be passed to the mutation</li><li>Does the actual CRUD operation</li><li>Specifies the type that can be returned once the mutation is complete</li></ol><p>In order to define this resolver function, create a folder resolvers under app/graphql and create a file named create_movie.rb. Place the following code in it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00f90671e6c0aaec883c4bea71874fea/href">https://medium.com/media/00f90671e6c0aaec883c4bea71874fea/href</a></iframe><p>This resolver (as mentioned in the three points above):</p><ol><li>Defines that 3 arguments (name, year and genre) can be passed.</li><li>Creates movie object with the passed parameters.</li><li>Returns an object of type MovieType</li></ol><p>Lets try out this mutation using Graphiql.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DL-gkq_gnOAUWMaPTXofLA.png" /><figcaption>Mutation — create_movie</figcaption></figure><p><strong>Conclusion:</strong></p><p>We have learnt and implemented two of the main concepts in GraphQL — queries and mutations. I am sure that you will be able to use graphql gem in your projects. The next step is the jump on to the next concept — subscriptions. In the next blog of the series, we will implement this. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d2b0beb1b6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/reportbee/graphql-blog2-core-concepts-5d2b0beb1b6">GraphQL — Core Concepts (Blog #2 of 3)</a> was originally published in <a href="https://medium.com/reportbee">reportbee</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL — An Introduction (Blog #1 of 3)]]></title>
            <link>https://medium.com/reportbee/graphql-blog1-bbdf25ceda39?source=rss-9effcf63c5bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbdf25ceda39</guid>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[report-bee]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[code]]></category>
            <dc:creator><![CDATA[Shriram Balakrishnan]]></dc:creator>
            <pubDate>Thu, 30 Aug 2018 10:16:37 GMT</pubDate>
            <atom:updated>2018-09-27T07:30:34.835Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*PKgygyihDjd0RnzfmH_z5Q.png" /></figure><p>Welcome to the introductory blog on GraphQL.</p><h3>What is GraphQL?</h3><p>GraphQL is an alternative to the widely used REST API specification. Its a standard/specification invented by Facebook that specifies how APIs should behave. The two important aspects of this standard are:</p><ol><li>Declarative data fetching — Client (API consumer) specifies the data that they require</li><li>Single endpoint responds to queries</li></ol><p>GraphQL is a query language for APIs. You query an API for required data just like you query for required columns in SQL.</p><h3>Why is GraphQL so popular?</h3><p>Because it solves the problems that we face while developing/consuming REST APIs. Let’s look at the problems that exists in REST APIs.</p><p>In REST, the data returned by the API is fixed. This means that if we need data for a page, we have got 2 options:</p><h4>1. Write an API to get only the exact data needed for the page</h4><p>Listed below are the downsides to this approach:</p><ol><li>API is tightly coupled to its usage. Any change to the front-end needs corresponding change to backend.</li><li>The API is not reusable since it might compute and return unwanted data.</li></ol><h4>2. Write few generic APIs and make calls these APIs to fetch data and use it</h4><p>Listed below are the downsides to this approach:</p><ol><li>This means that multiple calls needs to be made to server to get the required data.</li><li>Generic API might return data that are not required for the page.</li></ol><h3>How does GraphQL solve this problem?</h3><p>The GraphQL API consumers can specify the attributes that they require and by calling the single GraphQL endpoint, they can exactly get the required data.</p><ol><li>This will reduce the data usage as only necessary data is being returned by the server.</li><li>Reduced latency time as only one API call is being made.</li></ol><p>This is the advantage of using GraphQL APIs. Many big companies are using GraphQL APIs for quite sometime now.</p><h3><strong>Concepts in GraphQL:</strong></h3><ol><li><em>Any request is a POST request </em>— Since the clients request for data, the clients will need to send all the attributes that it requires to the server. Since GET request has length limitations, any GraphQL API request has to be a POST request.</li><li><em>Query </em>— A GraphQL query is used to fetch data.</li><li><em>Mutation</em> — Mutations are used to create/update/delete records.</li><li><em>Subscription</em> — Clients can subscribe to an event and this creates a connection between client and server. When that even occurs, the server pushes the data to clients.</li><li><em>Schema</em> — Schema defines the capabilities of the API. It has the queries, mutations and subscriptions that the clients can use.</li></ol><p>I have only specified a brief introduction on the above mentioned core concepts. It will be most effective to learn about these concepts by actual implementation.</p><p>At Report Bee, we use Ruby on Rails for back-end development. So, in the next blog of this series, we will go into core concepts and implement a GraphQL API using “graphql” gem.</p><p>Stay tuned for the next blog.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbdf25ceda39" width="1" height="1" alt=""><hr><p><a href="https://medium.com/reportbee/graphql-blog1-bbdf25ceda39">GraphQL — An Introduction (Blog #1 of 3)</a> was originally published in <a href="https://medium.com/reportbee">reportbee</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All you need is a CUE]]></title>
            <link>https://medium.com/@shriramrbk/all-you-need-is-a-cue-36d624a5013d?source=rss-9effcf63c5bc------2</link>
            <guid isPermaLink="false">https://medium.com/p/36d624a5013d</guid>
            <category><![CDATA[routine]]></category>
            <category><![CDATA[habit-building]]></category>
            <dc:creator><![CDATA[Shriram Balakrishnan]]></dc:creator>
            <pubDate>Sat, 31 Mar 2018 19:57:53 GMT</pubDate>
            <atom:updated>2018-03-31T19:57:53.032Z</atom:updated>
            <content:encoded><![CDATA[<p>You would have probably read about <em>cues</em> in <a href="https://en.wikipedia.org/wiki/The_Power_of_Habit"><strong>The Power of Habit</strong></a>. If you haven’t read already, I highly recommend you to read this. It gives great deal of information about how habits are formed.</p><p>I will give a short real life example of <strong><em>cue</em></strong>. Let’s say that you take a break at 5.00 PM everyday. In this simple act resides the power of cues. If you have done it enough number of times, then 5&#39;O clock becomes your cue. The action that follows this cue becomes your habit. Before you know, taking a break at 5&#39;O clock would have become your habit.</p><p>Now this simple <em>cue</em> hack can be used to form a habit as explained by the author <a href="https://en.wikipedia.org/wiki/Charles_Duhigg"><strong>Charles Duhigg</strong></a>.</p><p>Here’s how I used this hack to form few habits to ensure I do stuffs that I had always wanted to do.</p><p>Exercising and Journalling are those habits.</p><p><strong>1. Exercise:</strong><br>Normally we put off exercise when we are running late. But have you wondered why we don’t skip brushing our teeth? The answer is that it has already become our habit. So I took brushing as <em>cue</em> for this exercise task. As soon as I brushed, I consciously remembered my decision of exercising and exercised for 3–4 days. And then it became a routine. Even if I was running late, I would exercise immediately after brushing.</p><p><strong>2. Journalling:</strong><br>Going to bed is my cue for this task. As soon as I hit my bed, I would pick up my diary placed near my bed and write my day’s experience.</p><p>After experimenting with these two habits, I have used this in two more places. Like keeping mobile out of my reach as soon as I make my bed (to prevent myself from looking at it) and meditate before making my bed.</p><p>Thinking now, I can see few more places in my day where I can set cues to do few more things.</p><p>I have been doing this for 2 months now. Since this works like a charm for me, I thought of sharing this so that someone can use this proven technique to do things and get better.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36d624a5013d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>