<?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 Draj K on Medium]]></title>
        <description><![CDATA[Stories by Draj K on Medium]]></description>
        <link>https://medium.com/@draj.dev?source=rss-495f277dc5d9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uYMlVQlJNP-dCtrCbaBWoA.png</url>
            <title>Stories by Draj K on Medium</title>
            <link>https://medium.com/@draj.dev?source=rss-495f277dc5d9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 08:52:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@draj.dev/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[Short lived tokens with GoLang]]></title>
            <link>https://medium.com/@draj.dev/short-lived-tokens-with-golang-da7097d9c60d?source=rss-495f277dc5d9------2</link>
            <guid isPermaLink="false">https://medium.com/p/da7097d9c60d</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Draj K]]></dc:creator>
            <pubDate>Fri, 31 Jul 2020 06:11:23 GMT</pubDate>
            <atom:updated>2020-09-07T08:41:45.501Z</atom:updated>
            <content:encoded><![CDATA[<h3>GoLang — Short lived, self issued JWTS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7PVIbHeNBYUyXnzpEi73xg.png" /></figure><h3>Before we start ..</h3><p>I would not go through what JWTS are and boastfully display how good GoLang is :)</p><p>Instead, I will tell my story on why and how I used self issued JWT (json web token) instead of using Identity providers like Auth0, OKTA &amp; others.</p><p>Story starts when I came across a requirement to forward session information to our frontend (ReactJS SPA), so that all communication with our Backend (GoLang service) is secured.</p><p>Our frontend does not have a login page as we did not required users to authenticate. We relied on third party to forward us request and we then take care of it. We then notify the same third party when we are done.</p><p>We decided to use short lived tokens (~10 minutes) bound to a single resource which our frontend would have access to.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f7kFOzaiLWZYFO8-Afe_ug.png" /></figure><h3>Self Issued tokens</h3><p>Our GoLang service accepts requests from third party, performs its checks and then forwards request to our frontend with self issued tokens.<br>Our frontend then stores the token in memory and uses it to communicate with our backend.</p><p>Here is how i create and verify short lived tokens bound to a resource:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbdde251fda4f8a79fee272a2b437d84/href">https://medium.com/media/cbdde251fda4f8a79fee272a2b437d84/href</a></iframe><p>I also created this utility function to generate redirect url.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bb1f2973f2ab131f42b2721c0697bb1/href">https://medium.com/media/1bb1f2973f2ab131f42b2721c0697bb1/href</a></iframe><h3>Verifying tokens</h3><p>Our backend was already setup with Gorilla Mux router, so I decided to create a custom middleware to verify tokens.</p><p>Here is how my middleware looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a976b6b59c2518253cc5e4db5940dca9/href">https://medium.com/media/a976b6b59c2518253cc5e4db5940dca9/href</a></iframe><p>Thanks for reading :)</p><p>This is how I solved my problem and would love to learn whether you would go down the same road or use a different path.</p><h3>References</h3><p><a href="https://jwt.io/">https://jwt.io/</a><br><a href="https://golang.org/">https://golang.org/</a><br><a href="https://github.com/gorilla/mux">https://github.com/gorilla/mux</a><br><a href="https://github.com/dgrijalva/jwt-go">https://github.com/dgrijalva/jwt-go</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da7097d9c60d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[onSuccess() for Apollo React Hooks]]></title>
            <link>https://medium.com/@draj.dev/onsuccess-for-apollo-react-hooks-dbe679a9efdb?source=rss-495f277dc5d9------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbe679a9efdb</guid>
            <category><![CDATA[apollo-client]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Draj K]]></dc:creator>
            <pubDate>Fri, 21 Feb 2020 05:17:03 GMT</pubDate>
            <atom:updated>2020-02-23T21:25:34.436Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*iZE7e2UiosV9bm8tnTnl8A.png" /></figure><p>Let’s agree that we ❤ React hooks and using Apollo React hooks works like a charm.</p><p>Recently working on a project with React and GraphQL, My team felt a need to have onSuccess() callback in addition to onError() and onCompleted() options.</p><p>After spending hours in debugging, I found out that onCompleted()</p><ul><li>works differently depending on the errorPolicy.</li><li>is not dependant on whether or not there is a successful response.</li><li>also called when server sends partial or no data with error(s).</li></ul><p>I found documentation to be a little misleading and interestingly enough, I also saw a <a href="https://github.com/apollographql/apollo-client/pull/5907/files">pull request</a> to change the wordings :)</p><p>I came up with this wrapper function to store error in local state and then use it to trigger onSuccess().</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b47eb02f22950a7a669fbd03fbf45e84/href">https://medium.com/media/b47eb02f22950a7a669fbd03fbf45e84/href</a></iframe><p>Additionally, we use this wrapper to handle known internal errors that require some context and handle other generic errors with apollo link error. We then push these errors to a centralised error queue to display errors on UI and to make sure it doesn’t show duplicate errors.</p><p>Statefulness of useMutation wrapper can be extracted to a function and reused if there is a use case to use it for other hooks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbe679a9efdb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL API— Read data from SQL, NoSQL and HTTP endpoints]]></title>
            <link>https://medium.com/@draj.dev/graphql-api-to-read-from-sql-nosql-databases-3f5ffdbdc8ef?source=rss-495f277dc5d9------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f5ffdbdc8ef</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[api-development]]></category>
            <dc:creator><![CDATA[Draj K]]></dc:creator>
            <pubDate>Fri, 05 Apr 2019 03:28:57 GMT</pubDate>
            <atom:updated>2019-04-08T20:19:53.686Z</atom:updated>
            <content:encoded><![CDATA[<h3>GraphQL API — Get data from SQL, NoSQL and HTTP datasources</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FxjTqgK0rAIIU5GBdiHN5w.png" /></figure><h3>GraphQL — What is it ?</h3><p><strong>GraphQL</strong> is an open, declarative data-fetching specification and one of the modern ways to query and manipulate data from different sources including and not limited to <strong><em>sql</em></strong>, <strong><em>nosql</em></strong> and <strong><em>http</em></strong>.</p><p>Important selling aspects are its strong-type system, hierarchical structure and declarative nature — which means there is no reliance on server to structure data and instead clients like React or React Native can request data in whatever structure they need.</p><p>Born and bred by Facebook, <strong>GraphQL</strong> has 2 parts to it; a query language and runtime. We would be looking at the runtime part of it to build a scalable API to query data from</p><ul><li><strong>Relational datasource (eg: PostgreSQL, Microsoft SQL Server, etc)</strong></li><li><strong>Document based datasource ( eg: DynamoDB, CosmoDB, MongoDB etc)</strong></li><li><strong>REST endpoint (eg: API built with .NET WebAPI, NodeJS Express, etc)</strong></li></ul><blockquote><strong>GitHub</strong> uses GraphQL as it offers more flexibility for the developers. The option to precisely generate the information that a user wants is a great advantage over sending multiple REST calls to receive the same …</blockquote><h3>Before we start</h3><p>I would not be sharing a step-by-step process and overwhelm you with GitHub repository and code hell. Instead, I will use code snippets to focus and explain what I mean.</p><p>I would be sharing code snippets from an API built with NodeJs and express. Here is what the finished version looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S5AjuatFdbLRf8uVeoRw6g.png" /></figure><h3>Our first schema</h3><p>Our schema exported here would be of type <strong>GraphQLSchema. </strong>GraphQLSchema takes two configuration objects; query and mutation. We will come to mutation in next blog post and would be playing with query for now.</p><p>Our objects will be exposed as graphs and to traverse any graph, we need a starting point which in this instance would be <strong>RootQueryType</strong>. All the fields we define here, would be available in top level query selection scope. Lets see how a simple schema looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c28c5456cac8389e2af0edcb15c2ba18/href">https://medium.com/media/c28c5456cac8389e2af0edcb15c2ba18/href</a></iframe><p><strong>resolve()</strong> function in <strong>GraphQLObjectType </strong>tells javascript on how to resolve that <strong>title</strong>. I have extended this function in next steps.</p><h3>Lets query</h3><p>Here is how easy it is to query with <strong><em>GraphQL</em></strong>.</p><p>I used <strong><em>graphiql </em></strong>to execute the query, which acts as a browser IDE or playground to test queries, mutations and schemas. There is a code snippet in next step to show how i enabled <strong><em>graphiql</em></strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/390efcffc8adb7fd10d30d0e9ac856f8/href">https://medium.com/media/390efcffc8adb7fd10d30d0e9ac856f8/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be0e1bfc6b06538ff27279f49c6e25ec/href">https://medium.com/media/be0e1bfc6b06538ff27279f49c6e25ec/href</a></iframe><p>Clients can query data over <strong><em>http</em></strong> and use frameworks like <strong><em>apollo</em></strong>. However, there is no compulsion to use frameworks like <strong><em>apollo</em></strong>, <strong><em>relay</em></strong> etc and clients are not restricted to stick to <strong><em>http</em></strong>.</p><p>Clients can choose to read data similar to whichever way they consume an <strong>REST</strong> endpoint and could go and use <strong><em>websockets </em>😃 </strong>if need so.</p><h3>Set up connections</h3><p>We will use <strong><em>aws-sdk</em></strong> to connect to <strong><em>DynamoDB </em></strong>and<strong> <em>pg </em></strong>for <strong><em>PostgreSQL</em></strong>. I would not go through how to use pg and aws-sdk to read data as that is a seperate topic all together.</p><p>Connections setups live in a seperate file and objects exported from that file would be utilised to communicate with the datasource. I normally pass connection pools down to graphql context so that they are accessible in <strong><em>resolver()</em></strong> .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38e4eeea37b351ef982a790ddb47478e/href">https://medium.com/media/38e4eeea37b351ef982a790ddb47478e/href</a></iframe><h3>Read data</h3><p>I have split out the code which gets number of likes from <strong><em>dynamodb</em></strong>, here is how it looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f03fa28823bce03510676c1f53c6c7eb/href">https://medium.com/media/f03fa28823bce03510676c1f53c6c7eb/href</a></iframe><p>I call them utils and they take datasource connection and read data from it. Util in previous code snippet gets number of likes from <strong><em>DynamoDB.</em></strong> I have used similar pattern to get data from <strong><em>PostgreSQL. </em></strong>To read from a <strong><em>REST</em></strong> endpoint, I have used <strong><em>axios</em></strong> which is a seperate util and follows similar approach.</p><p>All magic happens in r<strong><em>esolver()</em></strong> as mentioned before. <strong><em>resolver () </em></strong>decide which datasource can be used to get data. Third argument in resolver function has the <strong><em>graphql</em></strong> context.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a4e73e3eb39e336caea0caf130a7795/href">https://medium.com/media/2a4e73e3eb39e336caea0caf130a7795/href</a></iframe><h3>Next steps</h3><p>There are few things which I want to point out</p><ul><li>Use frameworks like <strong><em>dataloader</em></strong> to improve performance for this API by calling sql queries and rest endpoints in a batch and cache the responses for repeated information</li><li>Structure your utils in such a way that they can be used for multiple schemas</li><li>Explore and see how AWS AppSync can be used to get data from dynamoDB, lambdas, S3 buckets and various other datasources</li></ul><p>I would definitely create a blog to try out these and watch this space if you found this blog interesting.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f5ffdbdc8ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>