<?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>Forem: Lakshay</title>
    <description>The latest articles on Forem by Lakshay (@ilakshay).</description>
    <link>https://forem.com/ilakshay</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F314528%2F4be1f733-1615-47a9-9df0-4c96169116a9.jpg</url>
      <title>Forem: Lakshay</title>
      <link>https://forem.com/ilakshay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ilakshay"/>
    <language>en</language>
    <item>
      <title>From Ghostbusting Tiny Monsters to Resurrecting the Giant</title>
      <dc:creator>Lakshay</dc:creator>
      <pubDate>Thu, 27 Jun 2024 10:43:07 +0000</pubDate>
      <link>https://forem.com/ilakshay/from-ghostbusting-tiny-monsters-to-resurrecting-the-giant-13l5</link>
      <guid>https://forem.com/ilakshay/from-ghostbusting-tiny-monsters-to-resurrecting-the-giant-13l5</guid>
      <description>&lt;p&gt;Well, I am not really busting ghosts here although it would be fun to be a part of the original Ghostbuster series.&lt;/p&gt;

&lt;p&gt;This one, 2 years later, is about the same side project. It is the story of haunting microservices, deployments, and my heartfelt sad decision to move back to the monolith.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recap - A series of unfortunate events
&lt;/h3&gt;

&lt;p&gt;The backend of my ECom side project was 6 node apps running Apollo GraphQL servers and another node app running Apollo gateway, to stitch all the federated schemas - or as I would quote "One ring to rule them all"!&lt;/p&gt;

&lt;p&gt;Once upon a time I used to deploy these easily when Heroku was free. Its seamless CI/CD pipelines were smooth AF and the cold starts weren't bad either.&lt;/p&gt;

&lt;p&gt;So what happened?!&lt;/p&gt;

&lt;p&gt;Sauron decided to turn things upside down and Heroku went from free to paid, forcing me to move towards gathering the alliances or thinking about monoliths.&lt;/p&gt;

&lt;h3&gt;
  
  
  Challenges you ask?!
&lt;/h3&gt;

&lt;p&gt;Managing 7 node servers - even for development locally, was tiring.&lt;br&gt;
Like imagine hitting &lt;code&gt;pnpm start&lt;/code&gt; for each server one by one, I found that tiring. Of course, I could have written a shell script but... never mind.&lt;/p&gt;

&lt;p&gt;This was just a small thing. The bigger one was deploying this. This being just a side project, a playground for me to tinker with my ideas, I did not want to shed some bucks around.&lt;/p&gt;

&lt;p&gt;So I started exploring the possible free solutions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Possible solution I explored?
&lt;/h3&gt;

&lt;p&gt;I did find multiple free services that were allowing me to do something similar, but there were hurdles, hurdles that got me thinking - "I shall not pass!"&lt;/p&gt;

&lt;p&gt;All the free services are limited to a very low grade CPU which was not able to run all the 7 servers. Deploying them individually was another idea but the cold starts were bad, like reeeaaaallly bad, that many times API calls used to fail because response time was too much for our dear gateway to wait for.&lt;/p&gt;

&lt;p&gt;Sweating on this for days, I got an idea - what if I could bind them all under one shell! Yes, the glorious concept of monorepos often allows us to ship and manage projects seamlessly. &lt;/p&gt;

&lt;p&gt;But as Uncle Ben said - "with great powers, comes great responsibility", and with monorepos comes heavy machinery. &lt;/p&gt;

&lt;p&gt;I used Vercel's Turborepo for this task since that is what we use at work. It did make my job easy for local development, I was even able to set up a workspace-global prisma instance which was much much easier than maintaining the schema for each service using Mongoose.&lt;/p&gt;

&lt;p&gt;But as I mentioned above, free services don't give a strong CPU, running 1 server wasn't easy, imagine running 7 under a shell. Kaboom!&lt;/p&gt;

&lt;p&gt;...and that's what happened. Some would start and some would not since RAM was not sufficient either. &lt;/p&gt;

&lt;h3&gt;
  
  
  So how will Frodo reach the fires of Mount Doom?
&lt;/h3&gt;

&lt;p&gt;Since I did not want to pay, and being the only dev in a side project, all of it was just killing the fun, and the only way forward I could see was doing things the old way.&lt;/p&gt;

&lt;p&gt;So one Saturday night at 00:00 I took out my favorite Starbucks' mug and poured me a noice freshly brewed cup of french press, and forked a new branch from &lt;code&gt;main&lt;/code&gt; and named it &lt;code&gt;project-knightfall&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;First of all, if you know what is Project Knightfall, we should be friends!&lt;/p&gt;

&lt;p&gt;I took the plunge, re-wrote a lot of it, deleted a few things, and was able to get the backend up and running in a few hours.&lt;/p&gt;

&lt;p&gt;I deployed it on - shoutout to - render.com and even though the cold start is really annoying, but things were finally working. No calls were failing, schema operations were working fine, and post cold start, response time was also good.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Coding is fun, building is fun but sometimes making such small decisions can ruin multiple nights and lives.&lt;/p&gt;

&lt;p&gt;For let's say a hobbyist it is annoying to encounter such challenges. But you definitely learn a lot, after all your mistakes are your best teachers.&lt;/p&gt;

&lt;p&gt;It is good that we have new SSR frameworks like Remix and Next which do eliminate such issues to some extent(?) but - and I am frontend guy - building a federated GraphQL backend was super cool and I wanted to do a lot more with it.&lt;/p&gt;

&lt;p&gt;Do you think, I should pay and get back on it? Do you have such stories to share? &lt;/p&gt;

&lt;p&gt;Drop in the comments and say hi!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>backend</category>
      <category>node</category>
    </item>
    <item>
      <title>GraphQL Microservices and the magic of Apollo Federation</title>
      <dc:creator>Lakshay</dc:creator>
      <pubDate>Sun, 06 Mar 2022 12:29:05 +0000</pubDate>
      <link>https://forem.com/ilakshay/graphql-microservices-and-the-magic-of-apollo-federation-545n</link>
      <guid>https://forem.com/ilakshay/graphql-microservices-and-the-magic-of-apollo-federation-545n</guid>
      <description>&lt;h2&gt;
  
  
  I am just a UI Engineer.
&lt;/h2&gt;

&lt;p&gt;Hello from the Dev cave! 🍻&lt;br&gt;
Hope you are safe and healthy.&lt;br&gt;
I am a UI engineer or a Frontend developer. I am just trying to explore stuff and build cool things. I don't have enough experience or knowledge on backend.&lt;/p&gt;

&lt;p&gt;Why I say this is because if you have worked with GraphQL and you are skilled/knowledgeable on the backend, I would love to hear your views in the comment section and anyways in which I can improve my code and application.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Application and Tech Stack
&lt;/h2&gt;

&lt;p&gt;For this post, I will be designing an application where a user can perform the following actions -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;signup&lt;/li&gt;
&lt;li&gt;login&lt;/li&gt;
&lt;li&gt;update user profile&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For these actions, I will be creating 4 services. It feels like being a part of LotR - &lt;/p&gt;

&lt;p&gt;"One Ring to rule them all,&lt;br&gt;
One Ring to find them,&lt;br&gt;
One Ring to bring them all&lt;br&gt;
and in the darkness bind them." &lt;/p&gt;

&lt;p&gt;😂 😂&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Auth - a service to rule them all and to handle sign up and login.&lt;/li&gt;
&lt;li&gt;User - a service to find them all and to handle user profile operations.&lt;/li&gt;
&lt;li&gt;Gateway - the gateway service to bind them all and create a super graph.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Gateway service
&lt;/h2&gt;

&lt;p&gt;The simplest of all services. All it needs to do is compile all the sub graphs into a super graph, redirect the request to the correct resolver and if you want, you can also validate your user token in this service - but I haven't.&lt;/p&gt;

&lt;p&gt;Let's install the dependencies.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;apollo-server - we will use this to create an instance of the appolo server and start it.&lt;/li&gt;
&lt;li&gt;@apollo-gateway - this will be used to configure our apollo gateway and also to create a connection between your federated gateway and your subgraphs.&lt;/li&gt;
&lt;li&gt;apollo-serve-core - we use this to enable a plugin that enables the GraphQL playground.&lt;/li&gt;
&lt;li&gt;dotenv - we use this to inject environment variables.&lt;/li&gt;
&lt;li&gt;esm - we use this as a transpiler so that we can use ES6 stuff in our code or as they say - "babel-less, bundle-less ECMAScript module loader".&lt;/li&gt;
&lt;li&gt;graphql - The JavaScript reference implementation for GraphQL&lt;/li&gt;
&lt;li&gt;nodemon - a utility to keep track of your JS files and restart the server if any changes occur.&lt;/li&gt;
&lt;li&gt;wait-on - gives a hint to your node server to wait for another service/server to run on another port before running this one.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's start building!
&lt;/h2&gt;

&lt;p&gt;It will be pretty much straight forward. You can amend the implementation as per your requirements.&lt;/p&gt;

&lt;p&gt;Create a new file - index.js - and import the required stuff.&lt;/p&gt;

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

&lt;p&gt;Now, I will use RemoteGraphQLDataSource class utility to create a connection between my Gateway and all other services. The only thing I want is my Gateway to pass down the context along with the request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54kegoikpf5amg3o6ahe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54kegoikpf5amg3o6ahe.png" alt="Creating a connection between my Gateway and all other services"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All we need here is, to pass down my authorisation header from the gateway to the other connected sub graphs.&lt;br&gt;
I am not sure, why they used a class for this, but this is how Apollo has provided it. It was a bummer but I couldn't find any other way. You can real more about it &lt;a href="https://www.apollographql.com/docs/federation/api/apollo-gateway/#class-remotegraphqldatasource" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's configure our gateway!&lt;/p&gt;

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

&lt;p&gt;While initialising our ApolloGateway, we need to provide 2 options - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;serviceList - an array of objects where each object represents a service i.e. a subgraph apollo server.&lt;/li&gt;
&lt;li&gt;buildService - a function that defines how we build the connection to the subgraphs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's configure our server!&lt;/p&gt;

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

&lt;p&gt;While initialising the apollo server, we pass some config - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the apollo gateway that we initialised above.&lt;/li&gt;
&lt;li&gt;we set subscriptions to false as - I am not sure about this - Apollo Federation does not support subscriptions as of now but either way, I haven't tried it yet. If you want to read how to configure subscriptions, check out this blog post &lt;a href="https://www.apollographql.com/blog/backend/federation/using-subscriptions-with-your-federated-data-graph/" rel="noopener noreferrer"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;A plugins array.&lt;/li&gt;
&lt;li&gt;context - remember we extended the class RemoteGraphQLDataSource, to enable our connection to pass of the headers or anything sent in the request, we need to return that from here so that our class extension can access it via context.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Last thing to do is is to tell our server to listen to the port for any requests.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  One last thing!
&lt;/h2&gt;

&lt;p&gt;So we have setup our gateway, the connections and even the servers. But when we are running it on or local environment, I don't want to run it unless my subgraph services are running.&lt;/p&gt;

&lt;p&gt;So here is my start script -&lt;/p&gt;

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

&lt;p&gt;So what it means is wait for the http resources to become available on port 4001 and then run index.js with nodemon by using esm.&lt;/p&gt;

&lt;p&gt;So that's it, that's your gateway!&lt;/p&gt;

&lt;p&gt;We have our gateway setup to combine various sub graphs. Time to start building the underlying logics or the subgraphs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Auth service!
&lt;/h2&gt;

&lt;p&gt;This service will be responsible for your user sign up and login. And the dependencies for this project are almost the same.&lt;/p&gt;

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

&lt;p&gt;Also, just for your info, this is my project structure. You may create something similar or something of your own. If you have a better idea, feel free to add it in the comments.&lt;/p&gt;

&lt;p&gt;And this is something that we be constant throughout all the services.&lt;/p&gt;

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

&lt;p&gt;Create your index.js and import the dependencies.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Configure your Apollo server -
&lt;/h3&gt;

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

&lt;p&gt;What is happening here - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I am not initialising a new schema because this service won't run independently. Since this will be triggered by a gateway, we do this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;buildFederatedSchema([{ typeDefs, resolvers, tracing: true }])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;next is configuring the context - I prefer to inject my DB modals in context so that way all the resolvers already have the access to it without me needing to import it explicitly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, if we are running on local host, we need not check the x-api-key in headers. I often forget what my key is while playing with queries and mutations on the playground so this really helps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type Defs
&lt;/h3&gt;

&lt;p&gt;I will create 3 types - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;user - describes a user.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjy7dvin4pdtk64z6d2f6.png" alt="user type"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a class="mentioned-user" href="https://dev.to/key"&gt;@key&lt;/a&gt;(fields: "id") means that if this is ever referenced externally i.e. from another service, then it will be done through the id field.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;new user input - describes the user input required for sign up.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdv0emcxhz8zxl66444xk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdv0emcxhz8zxl66444xk.png" alt="new user input"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;sign in - describes the user input required for sign in. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzv4u8hzdrss560cm83w4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzv4u8hzdrss560cm83w4.png" alt="sign in user input"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And this Auth service will house the following operations - &lt;/p&gt;

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

&lt;p&gt;I am assuming that since you are trying Apollo federation, you already are having experience with GraphQL and hence I am not deep diving into all the resolvers for Queries and Mutations. Nothing changes there. The only resolver I will be discussing here is about resolving the references.&lt;/p&gt;

&lt;p&gt;Once this service/sub graph is contacted/connected from another service, to resolve the User reference, we need to tell our Auth service how to do it by creating a resolver for the reference.&lt;/p&gt;

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

&lt;p&gt;Since this resolver will be called out by our gateway/apollo server when it needs to resolver the reference of type User, it will always have a reference object that will have that key property which we defined in our typeDef - in this case user Id. You can use that to fetch your user from the DB.&lt;/p&gt;

&lt;p&gt;Once you have it all setup, don't forget to start your server and also add it in the service list in your gateway serve - if you already haven't.&lt;/p&gt;

&lt;h2&gt;
  
  
  The User service!
&lt;/h2&gt;

&lt;p&gt;This service will be responsible for your user profile related operations like get user profile or update something. And the dependencies for this project are almost the same as Auth service. We just won't need stuff like bcrypt or jsonwebtoken.&lt;/p&gt;

&lt;p&gt;Also, remember in the gateway service, we passed down the authorisation header and x-api-key. Don't forget to consume that in the context here. If user is not authorised, that is the token is not present, you must reject the request.&lt;/p&gt;

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

&lt;p&gt;Also I have created a helper to check the auth token -&lt;/p&gt;

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

&lt;p&gt;Talking about the main thing here - type definitions. Once you have defined a type, you cannot define it again. If you try to define your User type here again, Apollo will throw an error because it is already getting a User type from the auth service.&lt;/p&gt;

&lt;p&gt;So to use the User type and to do you operations, you need to extend it.&lt;/p&gt;

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

&lt;p&gt;Once you have extended it, you can then add more fields to it. Also, since this type is an extension of a User type coming from an another service, we tell GraphQL that it will be referenced using the Id field by adding '@external' directive in front of it.&lt;/p&gt;

&lt;p&gt;Rest is all the same. &lt;/p&gt;

&lt;p&gt;You can build as many services as you want for separating the concern - the only key to understand how to define and link the sub graphs. &lt;/p&gt;

&lt;p&gt;Try building around this and add a Trip service that will have source, dest, date, and an array of members. If you do try, feel free to reach out to me in case you get stuck.&lt;/p&gt;

&lt;p&gt;That's all folks! That's all I know around Apollo federation - defining, extending and linking schemas or types. :D&lt;/p&gt;

&lt;p&gt;Any feedback, feel free to comment below or hit me up. Hope I helped someone in some way.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>react</category>
      <category>graphql</category>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>GraphQL Subscriptions - magic on the frontend</title>
      <dc:creator>Lakshay</dc:creator>
      <pubDate>Sat, 31 Jul 2021 21:52:32 +0000</pubDate>
      <link>https://forem.com/ilakshay/graphql-subscriptions-magic-on-the-frontend-2ngh</link>
      <guid>https://forem.com/ilakshay/graphql-subscriptions-magic-on-the-frontend-2ngh</guid>
      <description>&lt;p&gt;Hello from the Dev cave! 🍻&lt;br&gt;
Hope you are safe and healthy.&lt;/p&gt;

&lt;p&gt;This one is a long due about how I implemented a notification system in one of my react projects using Apollo GraphQL subscriptions.&lt;/p&gt;

&lt;h2&gt;
Install the dependencies
&lt;/h2&gt;

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

&lt;h2&gt;
Amending the Apollo Client config
&lt;/h2&gt;

&lt;p&gt;
The thing with subscriptions is that it doesn't work on the same http protocol like queries and mutations. Subscriptions work using websockets. So we need to tweak our Apollo Client config to enable and handle this communication.
&lt;/p&gt;

&lt;p&gt;
Import the dependencies - 
&lt;/p&gt;

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

&lt;p&gt;
Just like we create a http link to enable communication for queries and mutations, we will create a an instance of WebSocketLink for our subscriptions. 
&lt;/p&gt;

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

&lt;p&gt;
In the uri  property, pass the endpoint for subscriptions which you have defined in your server, something like - ws://server:port/subscriptions.

In the options property, we pass various config parameters ( like reconnect i.e. auto reconnect once it disconnects) and connection parameters like your auth and other headers.
&lt;/p&gt;

&lt;p&gt;
Once done, we inform/configure the Apollo Client how it can differentiate when to use which link. For this, we use the "split" functionality and "getMainDefinition" utility.

Apollo docs explain this extremely clearly - Although Apollo Client can use your WebSocketLink to execute all operation types, in most cases it should continue using HTTP for queries and mutations. This is because queries and mutations don't require a stateful or long-lasting connection, making HTTP more efficient and scalable if a WebSocket connection isn't already present.

Split here basically helps in separating the communication channel. We use the getMainDefinition utility to check whether our operation is a query (or a mutation) or a subscription.
&lt;/p&gt;

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

&lt;p&gt;Once done, we feed this splitLink to our client. &lt;/p&gt;

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

&lt;h2&gt;Consuming subscriptions&lt;/h2&gt;

&lt;p&gt;
Just like queries and mutations, we have a hook for this!
The useSubscription hooks, consumes the subscription and return data, loading and error variables.

It takes a defined GQL subscription as input along with connection options - like I have use shouldResubscribe here. Doing this, confirms that this is not a one time things and the application should be open to receive updates. 
&lt;/p&gt; 

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

&lt;p&gt;Give this a shot and let me know how it worked out for you.&lt;/p&gt;

&lt;p&gt;Cheers! &lt;/p&gt;

</description>
      <category>react</category>
      <category>graphql</category>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>GraphQL Subscriptions - the magic in the servers</title>
      <dc:creator>Lakshay</dc:creator>
      <pubDate>Sat, 26 Jun 2021 22:05:39 +0000</pubDate>
      <link>https://forem.com/ilakshay/graphql-subscriptions-the-magic-in-the-servers-53j7</link>
      <guid>https://forem.com/ilakshay/graphql-subscriptions-the-magic-in-the-servers-53j7</guid>
      <description>&lt;p&gt;Hello from the Dev cave! 🍻&lt;br&gt;
Hope you are safe and healthy.&lt;/p&gt;

&lt;p&gt;This one is about how I implemented a notification system in one of my projects using Apollo GraphQL subscriptions.&lt;/p&gt;

&lt;p&gt;The example that I am going to use here is an example project that has 2 types of users - manager and associate and below are 2 scenarios.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;As a manager and a logged in user,  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I want to get notified when my associate raises a request&lt;/li&gt;
&lt;li&gt;I want my associate - if logged in - to get notified when I raise a request for them.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;As an associate and a logged in user,  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I want my manager - if logged in - to get notified when I raise a request.&lt;/li&gt;
&lt;li&gt;I want to get notified when my manager raises a request for me.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Let's see how I have implemented this!&lt;/p&gt;

&lt;h3&gt;
  
  
  Some Server side stuff
&lt;/h3&gt;

&lt;h4&gt;
  
  
  TypeDefs
&lt;/h4&gt;

&lt;p&gt;Define a type Subscription along with all the subscriptions that you want. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flsof111smt9jij5rgbju.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flsof111smt9jij5rgbju.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Resolvers
&lt;/h4&gt;

&lt;p&gt;Just like queries and mutations, subscriptions also have a resolver.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57x66f19fkq3mitiuesh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57x66f19fkq3mitiuesh.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
A couple of points here - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A resolver name should match the subscription name like newRequestAdded here. In that object, we will have a subscribe object where you define the logic for what data should be returned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I am using withFilter method that assists me to filter out data that needs to be returned. This is optional. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;First parameter that withFilter accepts is the function that you would use to register an event. Subscriptions here work on the concept of PubSub or Publish-Subscribe i.e. you define an event and register it.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0np3dl3auecu3fpxj6jm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0np3dl3auecu3fpxj6jm.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
How we got this pubsub instance here, I will cover that in the points below. Just the thing to consider is that each subscription has the same pubsub instance throughout the app and each subscription resolver must return an asyncIterator with the array of event names to register those events and get in the pubsub ecosystem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Second parameter is filter functions that returns true if the criteria is met and subscription should be sent to a client. This function can be async as per your requirements. In this function, you will get&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;payload which the resolved value via the mutations's resolver.&lt;/li&gt;
&lt;li&gt;variables i.e. the stuff you got as a subscription variable&lt;/li&gt;
&lt;li&gt;context if you choose to inject this while configuring your Apollo server for subscriptions.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Publishing an event
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhsbsbp8fo7cqpogn4p2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhsbsbp8fo7cqpogn4p2.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
The best way to inject the pubsub instance is via context. That way you can have a single copy and avoid weird traps or situations like circular dependencies. So say I added a new request in my DB and before I return it from the resolver I use the publish method on my pubsub instance to emit this event with a payload (sounds familiar? REDUX - TADA! And that is why I used the work ecosystem above! :D).&lt;br&gt;
Whatever payload you are passing, the key should be same as the subscription name - in this case newRequestAdded and newRequest is the object Mongo returned after saving the new request.&lt;/p&gt;

&lt;h4&gt;
  
  
  Configure Apollo Server instance.
&lt;/h4&gt;

&lt;p&gt;We need to define a couple of things that will assist Apollo server to get on with this. Let's start by installing the dependency.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F839z8yg989z43yjzo2m9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F839z8yg989z43yjzo2m9.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From this, import PubSub and create its instance.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjtmnonfh6jvx2in46i92.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjtmnonfh6jvx2in46i92.png" alt="image"&gt;&lt;/a&gt; Yeah yeah I know I should be using webpack and ES6+! Will do that some other weekend.&lt;/p&gt;

&lt;p&gt;We need to modify our server config. The context here also gives you a connection object. What we need to do is check that if we get a connection object, you can use it for securing your app by accessing API KEY headers from this and/or just return it directly for subscription resolvers to handle.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq9xd4ooamo7gml940y60.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq9xd4ooamo7gml940y60.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
Once that is done, we need to define the endpoint.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhpsnstzaizt8v1gb82z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhpsnstzaizt8v1gb82z.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
We define a path - or endpoint - which we will configure in our client. Just remember that this is a websocket driven function and hence our endpoint protocol changes i.e. in my case it will be - ws://localhost:5000/subscriptions.&lt;/p&gt;

&lt;p&gt;This config object also give us 2 functions onConnect which you can use to return connection parameters i.e. the variables received AND onDisconnect.&lt;/p&gt;

&lt;p&gt;Once it is done, give it a shot using GraphiQL. That's all the config we need on the server side, rest is all your logic.&lt;/p&gt;

&lt;p&gt;Give this a shot and we'll talk about frontend in the next post!&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>react</category>
      <category>graphql</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Configuring GraphQL for backend</title>
      <dc:creator>Lakshay</dc:creator>
      <pubDate>Sun, 16 May 2021 14:42:15 +0000</pubDate>
      <link>https://forem.com/ilakshay/configuring-graphql-for-backend-58f6</link>
      <guid>https://forem.com/ilakshay/configuring-graphql-for-backend-58f6</guid>
      <description>&lt;p&gt;Hello from the Dev cave! 🍻&lt;br&gt;
Hope you are safe and healthy.&lt;/p&gt;

&lt;p&gt;This one is about sharing with you all how I configure GraphQL for my backend. It might help someone, or someone might have a better idea and would increase my knowledge.&lt;/p&gt;

&lt;p&gt;I also did one about how I configure and consume on frontend and here is a link to that &lt;a href="https://www.instagram.com/p/CMjFAbDDVmN/" rel="noopener noreferrer"&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For my setup, I use NodesJS &amp;amp; Apollo Server with MongoDB. You can find its docs and read more about Apollo &lt;a href="https://www.apollographql.com/docs/apollo-server/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You need three main things to implement GraphQL - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Something that can understand the GraphQL spec, implement it and handle its requests - something like Apollo server. There are many out there, but I prefer Apollo because I find it simple. As their docs say, Apollo Server is an open-source, spec-compliant GraphQL server that's compatible with any GraphQL client, including Apollo Client. Also, I use Apollo client for my frontend.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Type Definitions - things that define your operations and the shape of data those operations will handle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Resolvers - functions that assist to resolve (or fulfil) those operations.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;p&gt;Install the dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add apollo-server graphql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once installed, I suggest you to create a folder - something like graphql - in your src directory, just keep things organised. Inside that, you can create your resolvers and type defs.&lt;/p&gt;

&lt;p&gt;What I do is, I have a single source for types i.e. typeDefs.js and one folders for resolvers. This folder has respective resolvers as per type defs like users.js, posts.js, etc and all of them being combined in an index file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type Definitions
&lt;/h3&gt;

&lt;p&gt;Import the gql tag from apollo-server package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { gql } = require('apollo-server');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I use gql tag to defines types. Writing Type Defs is like defining schemas that shape your data. They define the basic structures with fields on which your GraphQL operates.&lt;/p&gt;

&lt;p&gt;One key thing to remember about them is, GraphQL follows these schemas strictly. It doesn't care what you do or how you do, all it cares for is how you maintain the shape of the data you are receiving or returning.&lt;/p&gt;

&lt;p&gt;Let's write 3 TypeDefs - a user schema, a query (or a request) to get that user and a mutation ( an operation that does something).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;A User Schema&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57z4mgk7unvuz58ml55s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57z4mgk7unvuz58ml55s.png" alt="TypeDef User"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A Query and Mutation&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    type Query{
        getUserByUserId(userId: String): User
        getAllUsers: [User]!
    }

    type Mutation{
        login(username: String!, password: String!): User!
        register(registerInput: RegisterInput): User!
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Also you can create custom input types to expect an argument in mutations or queries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;input RegisterInput{
        name: String!
        email: String!
        password: String!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ! means that this value is required and cannot be null.&lt;/p&gt;

&lt;h3&gt;
  
  
  Queries and Mutation (i.e. resolvers)
&lt;/h3&gt;

&lt;p&gt;You can defines your queries and mutations like general async functions. All Apollo GraphQL demands demands is that - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You should wrap these in a JS Object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Name of each queries and mutation should be same as what you have defined in Type Definitions. Remember, GraphQL is a very strict follower.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All queries should be wrapped in a parent Object with key Query and same for mutations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The values you return from these resolvers, should have the same shape as you have defined in the Type Defs.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;You may write different types of queries/mutations in their own files like one for users and one for posts, etc. You can combine them in an index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const usersResolvers = require('./users');

module.exports = {
    Query: { 
        ...usersResolvers.Query,
    },
    Mutation: {
        ...usersResolvers.Mutation,
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have your initial template ready, you need to configure the Apollo server instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Apollo server instance
&lt;/h2&gt;

&lt;p&gt;I configure the Apollo server instance with 3 config options - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Type defs&lt;/li&gt;
&lt;li&gt;The Resolvers&lt;/li&gt;
&lt;li&gt;Context&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Think of context like a middleware which gives you access to the request object. You'll learn more about this when you'll start learning and/or implementing GraphQL.&lt;/p&gt;

&lt;p&gt;I use this context, to check for the auth header to confirm whether the request has arrived from a valid source.&lt;/p&gt;

&lt;p&gt;Below is the snippet.&lt;/p&gt;

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

&lt;p&gt;Once this is done, you can hit your package manager yarn/npm to start and spin this server.&lt;/p&gt;

&lt;p&gt;Let me know what you think about this and if you are experienced with GraphQL, I would love to know your suggestions on making my GQL code better.&lt;/p&gt;

&lt;p&gt;Any feedback, feel free to comment below or hit me up. Hope I helped someone in some way.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>apolloserver</category>
      <category>node</category>
      <category>apollorgaphql</category>
    </item>
    <item>
      <title>I finally learnt SASS!</title>
      <dc:creator>Lakshay</dc:creator>
      <pubDate>Sat, 21 Nov 2020 20:10:18 +0000</pubDate>
      <link>https://forem.com/ilakshay/i-finally-learnt-sass-1kn6</link>
      <guid>https://forem.com/ilakshay/i-finally-learnt-sass-1kn6</guid>
      <description>&lt;h1&gt;
  
  
  Hello.
&lt;/h1&gt;

&lt;p&gt;Well, for some reason I've been putting myself off from learning SASS/SCSS even though it is widely used and in demand. But since now I am working on improving my HTML, CSS and Vanilla JS skills, so I thought why not!&lt;/p&gt;

&lt;p&gt;I decided to get started with the challenges on &lt;a href="https://www.frontendmentor.io/"&gt;Frontend mentor&lt;/a&gt; but first, I learnt something new.&lt;/p&gt;

&lt;p&gt;This is a short 20 min &lt;a href="https://youtu.be/Zz6eOVaaelI"&gt;course&lt;/a&gt; that I referred as reading the documentation was making me feel sleepy. But always go for the documentations first!&lt;/p&gt;

&lt;p&gt;So here are the key take aways from the course - &lt;/p&gt;

&lt;h2&gt;
  
  
  1. The SASS compiler and extension
&lt;/h2&gt;

&lt;p&gt;So this seems to be an awesome extension for VS Code. So can find it &lt;a href="https://marketplace.visualstudio.com/items?itemName=ritwickdey.live-sass"&gt;here&lt;/a&gt;. It is called "Live SASS Compiler". Looking at the number of installs, seems to be widely used.&lt;/p&gt;

&lt;p&gt;So this extension is continuously watching your SASS files for changes, and if any changes occur, it compiles your SASS files into CSS - because browsers can only understand CSS and not SASS - and also adds appropriate prefixes for supporting various functionalities (like flex) across browsers.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Variables
&lt;/h2&gt;

&lt;p&gt;Variables are simple to define in SASS. Variable names should always begin with a $ and then you can write something appropriate to justify that variable's usage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$background: #ffffff;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do note, that these are different from CSS variables. SASS variables are imperative.  If you use a variable and then change its value, it won't change where you have used it previously.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Mixins
&lt;/h2&gt;

&lt;p&gt;Don't we love functions? When we can separate a commonly used block of code and call again and again, why write the entire thing again! &lt;/p&gt;

&lt;p&gt;Mixins are functions in SASS. They allow you to reuse styles just like functions. To define a mixin, you start with an identifier @mixin followed by the name of the mixin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@mixin flexCenter {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just like functions, you can also pass different values to mixins for more flexibility. For example, consider the following mixin - flexCenter - its task is to apply a flexbox to a container and center items but flex direction can be either row or a column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@mixin flexCenter($direction) {
    height: 100vh;
    display: flex;
    flex-direction: $direction;
    justify-content: center;
    align-items: center;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have defined your mixin, to add them in a style, you can use &lt;a class="mentioned-user" href="https://dev.to/include"&gt;@include&lt;/a&gt; followed by the mixin name passing on values if any.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;header{
    @include flexCenter(column);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, just like in functions we can assign a default value for the arguments, we can do that here as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@mixin flexCenter($direction: row) {
    height: 100vh;
    display: flex;
    flex-direction: $direction;
    justify-content: center;
    align-items: center;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Nesting styles
&lt;/h2&gt;

&lt;p&gt;You can nest styles easily in SASS/SCSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;header{
    background: rgb(100, 100, 177);
    height: 100vh;

    button {
        background-color: $primaryButton;
        &amp;amp;:hover{
            background-color: red;
        }

        &amp;amp;::after{
            content: "hello";
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Interpolation
&lt;/h2&gt;

&lt;p&gt;String interpolation has always made my life easy. We can do that here also. In JavaScript, we perform interpolation using ${} but here we do it with #{}.&lt;/p&gt;

&lt;p&gt;Following is an example from the docs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@mixin corner-icon($name, $top-or-bottom, $left-or-right) {
  .icon-#{$name} {
    background-image: url("/icons/#{$name}.svg");
    position: absolute;
    #{$top-or-bottom}: 0;
    #{$left-or-right}: 0;
  }
}

@include corner-icon("mail", top, left);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Extends
&lt;/h2&gt;

&lt;p&gt;Using mixins is one option to share styles. Another is called extends. Say you have a class that has some specific styles and you define another one that will be using the same styles but with some extra features or modifications.&lt;/p&gt;

&lt;p&gt;You can either use mixins to separate the style block and just apply where ever needed or you can extend the class having that style to the new class (class and extends - kinda reminds me of inheritance 😆).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.some-container{
    //some styles
    height: 100px;
    width: 70px;
}

.another-container{
    @extend .some-container;
    width: 100px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Operators
&lt;/h2&gt;

&lt;p&gt;We also have operators our usual operators. I am not going into this as you must be aware operators from JS or any other language. But just as an overview, following is what we have-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;unary operators not, +, -, and /&lt;/li&gt;
&lt;li&gt;*, /, and %&lt;/li&gt;
&lt;li&gt;&amp;gt;, &amp;gt;=, &amp;lt; and &amp;lt;=&lt;/li&gt;
&lt;li&gt;== and !=&lt;/li&gt;
&lt;li&gt;and (boolean AND)&lt;/li&gt;
&lt;li&gt;or (boolean OR)&lt;/li&gt;
&lt;li&gt;=&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;It was interesting in learning SASS/SCSS today. I will be playing with this a lot more. As of now I am using styled-components in my React applications but I will definitely try a React app with SCSS once I a done playing.&lt;/p&gt;

&lt;p&gt;As I said, this was my first hands-on on SASS/SCSS. Let me know what I have understood is correct and if not, please do let me know the right concept.&lt;/p&gt;

&lt;p&gt;Cheers! 🍺&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>html</category>
      <category>design</category>
    </item>
  </channel>
</rss>
