<?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: Aditya Joshi</title>
    <description>The latest articles on Forem by Aditya Joshi (@adityajoshi12).</description>
    <link>https://forem.com/adityajoshi12</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%2F527089%2F884b5c2e-d4dc-4081-a49e-31628874eb3a.jpg</url>
      <title>Forem: Aditya Joshi</title>
      <link>https://forem.com/adityajoshi12</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/adityajoshi12"/>
    <language>en</language>
    <item>
      <title>Extending Kubectl with Plugins</title>
      <dc:creator>Aditya Joshi</dc:creator>
      <pubDate>Sat, 12 Nov 2022 17:16:03 +0000</pubDate>
      <link>https://forem.com/adityajoshi12/extending-kubectl-with-plugins-2e3c</link>
      <guid>https://forem.com/adityajoshi12/extending-kubectl-with-plugins-2e3c</guid>
      <description>&lt;p&gt;Plugins are software extensions that can be loaded on a program to improve its functionality.  &lt;code&gt;kubectl&lt;/code&gt;  is a tool that allows you to perform literally any and all Kubernetes-related tasks. This tool is used to make a list of all pods in the cluster and debug nodes. kubectl’s functionality can be extended with the help of the plugin. You can create kubectl plugins to solve some of the use cases that are complex in nature.&lt;/p&gt;

&lt;h2&gt;
  
  
  But what is the kubectl plugin?
&lt;/h2&gt;

&lt;p&gt;A plugin is a standalone executable file, whose name begins with  &lt;code&gt;kubectl-&lt;/code&gt;. To install a plugin, move its executable file anywhere on your  &lt;code&gt;PATH.&lt;/code&gt;  Krew is the package manager for  &lt;code&gt;kubectl&lt;/code&gt;  plugins as well as Kubernetes SIG that aims at solving the package management issue for  &lt;code&gt;kubectl.&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Criteria to be a plugin
&lt;/h2&gt;

&lt;p&gt;There are two important criteria to qualify as a kubectl plugin&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Plugin binary name must start with  &lt;code&gt;kubectl-&lt;/code&gt;  followed by the name of your plugin. Ex  &lt;code&gt;kubectl-decode&lt;/code&gt;,  &lt;code&gt;kubectl-count&lt;/code&gt;  .&lt;/li&gt;
&lt;li&gt; The plugin binary must be present in the  &lt;code&gt;PATH&lt;/code&gt;  variable so that kubectl can identify it as the plugin.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Usecase&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We will be creating a kubectl plugin that will decode the Kubernetes secrets. We all know that Kubernetes stores the Secrets in the base64 decoded format and if have to see the values of those secrets we need to extract the base64 encode value and then decode with plain text.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TC6ZMbTn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/1%2ADK15eY4MWNitRhS6-ivtqg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TC6ZMbTn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/1%2ADK15eY4MWNitRhS6-ivtqg.png" alt="Kubernetes secret stored in base64 format" width="880" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Kubernetes secret stored in base64 format&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Expectation
&lt;/h2&gt;

&lt;p&gt;Let's first set up the expectations/expected output from the plugin we will develop. we want the plugin to return the base64 encoded secrets in plain text. Also, it should support a way to specify the Kubernetes namespaces.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Uh181qAI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/1%2A1dLPo-vRQFl2AK1ah31Xgw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Uh181qAI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/1%2A1dLPo-vRQFl2AK1ah31Xgw.png" alt="kubectl decode plugin" width="880" height="879"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;For this article, we are going to create this plugin using the shell script but the same can be done using any programming language.&lt;/p&gt;

&lt;p&gt;We want this plugin to work like how a normal kubectl command works, where the user specifies the operation (create, update, delete and get) and the resource (pod, deployment, configmap, secret), also user can provide the namespace over which he wants to perform that operation, but if no namespace is provided default namespace is considered.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/2epdFWbiE_c"&gt;I have made a video on creating kubectl plugins using GoLang&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; We will create a text file with the name  &lt;code&gt;kubectl-decode&lt;/code&gt;  , where decode is the name of our plugin.&lt;/li&gt;
&lt;li&gt; Next, will use the kubectl command itself to get the secret and with the help of go-template, we can iterate over the secrets and decode them.&lt;/li&gt;
&lt;li&gt; Put the plugin in the  &lt;code&gt;PATH&lt;/code&gt;  variable so that kubectl can recognise it as the plugin.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
if [ $# -lt 1 ]  
then  
  echo "invalid argument "  
  exit 1  
fi  
NAMESPACE="default"  
if [ "$2" = "-n" ]  
then  
   if [ $# -eq 3 ]  
   then  
     NAMESPACE=$3  
    fi  
fi
kubectl get secrets "$1" -o go-template='{{range $k,$v := .data}}{{printf "%s: " $k}}{{if not $v}}{{$v}}{{else}}{{$v | base64decode}}{{end}}{{"\n\n"}}{{end}}' -n "$NAMESPACE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Sum up
&lt;/h2&gt;

&lt;p&gt;When we make use of this kubectl, we can simplify tedious tasks to be very simpler. kubectl plugins may look complex to you but trust me they will make your life much easier.&lt;/p&gt;

&lt;p&gt;There are some shortcoming that comes with the bash-based plugins but those can be easily solved when you will use any programming language like go, java, or nodejs.&lt;/p&gt;

&lt;p&gt;In this article we learned the basics of the kubectl plugin and what it takes to be a kubectl plugin, and also saw how we can create a kubectl plugin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;[Implementation]&lt;/strong&gt;  A full implementation for this pattern in this  &lt;a href="https://github.com/adityajoshi12/kubernetes-development/tree/main/kubectl-plugins"&gt;&lt;strong&gt;&lt;em&gt;GitHub repo&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;[Video]&lt;/strong&gt;  A detailed video explanation can be found  &lt;a href="https://youtu.be/QO7TRRPc1AY"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;[GoLang]&lt;/strong&gt;  implementation can be found  &lt;a href="https://youtu.be/2epdFWbiE_c"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks for reading and I hope you find this useful!&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>cloudnative</category>
      <category>go</category>
      <category>k8s</category>
    </item>
    <item>
      <title>GraphQL with NodeJs and MongoDB</title>
      <dc:creator>Aditya Joshi</dc:creator>
      <pubDate>Sat, 05 Dec 2020 16:23:54 +0000</pubDate>
      <link>https://forem.com/adityajoshi12/graphql-with-nodejs-and-mongodb-2bdm</link>
      <guid>https://forem.com/adityajoshi12/graphql-with-nodejs-and-mongodb-2bdm</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is GraphQL - A Query Language for APIs?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data.&lt;br&gt;
GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data.&lt;/p&gt;

&lt;p&gt;Most applications today need to fetch data from a server where that data is stored in a database. It's the responsibility of the API to provide an interface to the stored data that fits an application's needs.&lt;/p&gt;

&lt;p&gt;GraphQL is often confused with being a database technology. This is a misconception, GraphQL is a &lt;em&gt;query language&lt;/em&gt; for APIs - not databases. In that sense it's database agnostic and effectively can be used in any context where an API is used.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;A more efficient Alternative to REST&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Representational_state_transfer" rel="noopener noreferrer"&gt;REST&lt;/a&gt; has been a popular way to expose data from a server. When the concept of REST was developed, client applications were relatively simple and the development pace wasn't nearly where it is today. REST thus was a good fit for many applications. However, the API landscape has radically&lt;br&gt;
changed over the last couple of years. In particular, three factors have been challenging the way APIs are designed:&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;1. Increased mobile usage creates the need for efficient data loading&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;Increased mobile usage, low-powered devices, and sloppy networks were the initial reasons why Facebook developed GraphQL. GraphQL minimizes the amount of data that needs to be transferred over the network and thus majorly improves applications operating under these conditions.&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;2. Variety of different frontend frameworks and platforms&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;The heterogeneous landscape of frontend frameworks and platforms that run client applications makes it difficult to build and maintain one API that would fit the requirements of all. With GraphQL, each client can access precisely the data it needs.&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;3. Fast development &amp;amp; expectation for rapid feature development&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;Continuous deployment has become a standard for many companies, rapid iterations and frequent product updates are indispensable. With REST APIs, the way data is exposed by the server often needs to be modified to account for specific requirements and design changes on the client-side. This hinders fast development practices and product iterations.&lt;/p&gt;

&lt;p&gt;Over the past decade,&lt;a href="https://en.wikipedia.org/wiki/Representational_state_transfer" rel="noopener noreferrer"&gt;REST&lt;/a&gt; has become the standard (yet a fuzzy one) for designing web APIs. It offers some great ideas, such as &lt;em&gt;stateless servers&lt;/em&gt; and &lt;em&gt;structured access to resources&lt;/em&gt;. However, REST APIs have shown to be too inflexible to keep up with the rapidly changing requirements of the clients that access them.&lt;/p&gt;

&lt;p&gt;GraphQL was developed to cope with the need for more flexibility and efficiency! It solves many of the shortcomings and inefficiencies that developers experience when interacting with REST APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Schema &amp;amp; Type System in GraphQL&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;GraphQL uses a strong type system to define the capabilities of an API. All the types that are exposed in an API are written down in a &lt;em&gt;schema&lt;/em&gt; using the GraphQL Schema Definition Language (SDL). This schema serves as the contract between the client and the server to define how a client&lt;br&gt;
can access the data.&lt;/p&gt;

&lt;p&gt;Once the schema is defined, the teams working on frontend and backends can do their work without further communication since they both are aware of the definite structure of the data that's sent over the network.&lt;/p&gt;

&lt;p&gt;Frontend teams can easily test their applications by mocking the required data structures. Once the server is ready, the switch can be flipped for the client apps to load the data from the actual API.&lt;/p&gt;

&lt;p&gt;GraphQL has its own type system that's used to define the &lt;em&gt;schema&lt;/em&gt; of an API. The syntax for writing schemas is called &lt;a href="https://www.prisma.io/blog/graphql-sdl-schema-definition-language-6755bcb9ce51" rel="noopener noreferrer"&gt;Schema Definition&lt;br&gt;
Language&lt;/a&gt;(SDL).&lt;/p&gt;

&lt;p&gt;Here is an example of how we can use the SDL to define a simple type called Person:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;type Person { name: String! age: Int!}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This type has two &lt;em&gt;fields&lt;/em&gt;, they're called name and age and are&lt;br&gt;
respectively of type String and Int. The ! following the type means that&lt;br&gt;
this field is &lt;em&gt;required&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;schema&lt;/em&gt; is one of the most important concepts when working with a&lt;br&gt;
GraphQL API. It specifies the capabilities of the API and defines how&lt;br&gt;
clients can request the data. It is often seen as a &lt;em&gt;contract&lt;/em&gt; between&lt;br&gt;
the server and client.&lt;/p&gt;

&lt;p&gt;Generally, a schema is simply a collection of GraphQL types. However,&lt;br&gt;
when writing the schema for an API, there are some special &lt;em&gt;root&lt;/em&gt; types:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;type Query { \... }&lt;br&gt;
type Mutation { \... }&lt;br&gt;
type Subscription { \... }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mutation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In REST, any request might end up causing some side-effects on the&lt;br&gt;
server, but by convention it\'s suggested that one doesn\'t use GET&lt;br&gt;
requests to modify data. GraphQL is similar - technically any query&lt;br&gt;
could be implemented to cause a data write. However, it\'s useful to&lt;br&gt;
establish a convention that any operations that cause write should be&lt;br&gt;
sent explicitly via a mutation.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why do we need GraphQL?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;With traditional REST API calls, we didn't have the ability for the&lt;br&gt;
client to request a customized set of data. In contrast, GraphQL allows&lt;br&gt;
clients to define the structure of the data required, and the same&lt;br&gt;
structure of the data is returned from the server. This prevents&lt;br&gt;
excessively large amounts of data from being returned. However, it also&lt;br&gt;
adds a layer of complexity that may not be applicable for simple APIs.&lt;/p&gt;

&lt;p&gt;Moreover, maintaining multiple endpoints is difficult in REST&lt;br&gt;
architecture. When the application grows, the number of endpoints will&lt;br&gt;
increase, resulting in the client needs to ask for data from different&lt;br&gt;
endpoints. GraphQL APIs are more organized by providing structured types&lt;br&gt;
and fields in the schema while using a single API endpoint to request&lt;br&gt;
data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Graphql-compose?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Toolkit for generating complex GraphQL schemas in Node.js.&lt;br&gt;
Graphql-compose provides a convenient way to create GraphQL Schema. This schema is completely compatible with&lt;br&gt;
&lt;a href="https://github.com/graphql/graphql-js" rel="noopener noreferrer"&gt;GraphQL.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;graphql-compose -- provides a type registry with a bunch of methods for&lt;br&gt;
programmatic schema construction. It allows not only to extend types but&lt;br&gt;
also to remove fields, interfaces, args. If you want to write your graphql&lt;br&gt;
schema generator -- graphql-compose is a good instrument for you.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;provides methods for editing GraphQL output/input types (add/remove&lt;br&gt;
fields/args/interfaces)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;introduces Resolvers -- the named graphql fieldConfigs, which can be&lt;br&gt;
used for finding, updating, removing records&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;provides an easy way for creating relations between types via&lt;br&gt;
Resolvers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;provides converter from OutputType to InputType&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;provides projection parser from AST&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;provides GraphQL schema language for defining simple types&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;adds additional types Date, JSON&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Moving to the code&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let's start developing. First, we will create a new folder and initialize&lt;br&gt;
our package.json file. Then add the following packages with the command&lt;br&gt;
listed below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm init --y&lt;br&gt;
npm i express graphql express-graphql mongoose graphql-compose-mongoose graphql-compose&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;our folder structure is like this&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%2Fi%2F8hfczxybzt6jmdec1h9j.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%2Fi%2F8hfczxybzt6jmdec1h9j.png" alt="folder structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now in the server.js, we will set up a basic express server and will import the graphqlHTTP from express-graphql.&lt;br&gt;
We will also set up /graphql route and will define the graphql middleware and will set the graphql graphqlSchema.&lt;br&gt;
The beauty of the graphql-compose is that it will auto-generate the graphql scheme, mutations, and query for us based on our mongoose 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%2Fi%2Fi6kg0whudf7v5ee7fwwp.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%2Fi%2Fi6kg0whudf7v5ee7fwwp.png" alt="server.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's create our mongoose models, we'll be creating a book model and user model&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%2Fi%2Fas0dtg1fj2tlo3qajq4b.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%2Fi%2Fas0dtg1fj2tlo3qajq4b.png" alt="user.js"&gt;&lt;/a&gt;&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%2Fi%2Foxa28j34edaosodp3wwa.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%2Fi%2Foxa28j34edaosodp3wwa.png" alt="book.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now it's time to create GraphQL Scheme for the user model, where all the queries and mutations are defined.&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%2Fi%2Fbyz308k36ulckrzfggjo.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%2Fi%2Fbyz308k36ulckrzfggjo.png" alt="user.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you can see that the definition of all the resolvers is automatically generated by the graphql-compose-mongoose plugin like &lt;code&gt;findById&lt;/code&gt;, &lt;code&gt;findOne&lt;/code&gt;, &lt;code&gt;createOne&lt;/code&gt;, etc. You can find all the build-in &lt;a href="https://graphql-compose.github.io/docs/plugins/plugin-mongoose.html" rel="noopener noreferrer"&gt;mongooseResolvers&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how our GraphQL Scheme for &lt;code&gt;book&lt;/code&gt; looks like&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%2Fi%2F2r8fb7h8y5ylhztm9y92.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%2Fi%2F2r8fb7h8y5ylhztm9y92.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and now its time to build our schemas&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%2Fi%2Fh4e8lm0xfc177gt3mvyj.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%2Fi%2Fh4e8lm0xfc177gt3mvyj.png" alt="index.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this is done now its time to run our application&lt;br&gt;
&lt;code&gt;node start src/server.js&lt;/code&gt; and navigate to &lt;code&gt;http://localhost:5000/graphql&lt;/code&gt;&lt;br&gt;
you can insert the data using the mutations&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%2Fi%2Fvklcdw8f22genwo1wcwl.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%2Fi%2Fvklcdw8f22genwo1wcwl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
 To get the data use a query like this&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%2Fi%2Ftt5jz7ie176w92xbwscl.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%2Fi%2Ftt5jz7ie176w92xbwscl.png" alt="Alt Text"&gt;&lt;/a&gt;.&lt;br&gt;
You can find the documentation on the GraphQL playground&lt;br&gt;
Link to source code &lt;a href="https://github.com/adityajoshi12/GraphQL-MongoDB.git" rel="noopener noreferrer"&gt;link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>mongodb</category>
      <category>node</category>
      <category>graphqlcompose</category>
    </item>
    <item>
      <title>Hyperledger Fabric 2.2 Tutorial</title>
      <dc:creator>Aditya Joshi</dc:creator>
      <pubDate>Fri, 04 Dec 2020 04:19:20 +0000</pubDate>
      <link>https://forem.com/adityajoshi12/hyperledger-fabric-2-2-tutorial-44o1</link>
      <guid>https://forem.com/adityajoshi12/hyperledger-fabric-2-2-tutorial-44o1</guid>
      <description>&lt;p&gt;Hyperledger Fabric v2.2&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;This article shows how to setup a Hyperledger Fabric Network (HLF) v2.2, deploy chaincode, invoke, and query chaincode. We first give a high-level walk-through of the process, and later use Test Network provided in HLF examples to demonstrate the whole process. The script provided in Test Network makes bringing the blockchain Network along with Fabric CA servers much easier and we can use the crypto-material to perform the demonstration.&lt;/p&gt;

&lt;h2&gt;
  
  
  High-level Walk-through
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Generating crypto-material from the organizations and orderer (Org1, Org2, and Orderer) using Fabric CA server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generating channel artifacts eg. system channel genesis block, channel transaction, channel genesis block, etc,).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Updating anchor peers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating a channel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Joining the channel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deploying chaincode on the channel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Invoke or query the chaincode.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Golang&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Docker&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Nodejs&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Bring Up Network
&lt;/h2&gt;

&lt;p&gt;Here we first download the HLF binaries along with&lt;a href="https://github.com/hyperledger/fabric-samples" rel="noopener noreferrer"&gt; fabric examples&lt;/a&gt;. We will be using the Test network provided in the &lt;a href="https://github.com/hyperledger/fabric-samples" rel="noopener noreferrer"&gt;fabric examples&lt;/a&gt;. Our network will consist of Two Organization with one peer each, One orderer node, Three CA server (one for each organization and third for orderer), Two couchdb instances for state database(one for each peer)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Download the fabric binaries.&lt;/p&gt;

&lt;p&gt;curl &lt;strong&gt;-&lt;/strong&gt;sSL https:&lt;strong&gt;//&lt;/strong&gt;bit*&lt;em&gt;.&lt;/em&gt;&lt;em&gt;ly&lt;/em&gt;&lt;em&gt;/&lt;/em&gt;&lt;em&gt;2ysbOFE *&lt;/em&gt;|** bash &lt;strong&gt;-&lt;/strong&gt;s &lt;strong&gt;--&lt;/strong&gt; 2.3*&lt;em&gt;.&lt;/em&gt;&lt;em&gt;0 1.4&lt;/em&gt;&lt;em&gt;.&lt;/em&gt;*9&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Set the environment variable&lt;/p&gt;

&lt;p&gt;cd fabric-samples &amp;amp;&amp;amp; export PATH=$PWD/bin:$PATH&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Once step 2 is completed, go to the &lt;strong&gt;&lt;em&gt;test-network&lt;/em&gt;&lt;/strong&gt; folder.&lt;/p&gt;

&lt;p&gt;cd test-network&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the command and pass the options according to your need. To check all the available options run &lt;strong&gt;&lt;em&gt;./network.sh help&lt;/em&gt;&lt;/strong&gt;&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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AujAK6Rt_g_QtmkbMOe8ZtA.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%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AujAK6Rt_g_QtmkbMOe8ZtA.png" alt="help command"&gt;&lt;/a&gt;&lt;em&gt;help command&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To start the network with Fabric CA Servers, CouchDB and creating a channel mychannel.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;-ca: Use Certificate Authorities to generate network crypto material&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;-c: Name of channel to create (defaults to "mychannel”)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;-s: Peer state database to deploy: goleveldb (default) or couchdb&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;up — Bring up Fabric orderer and peer nodes. No channel is created.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;up createChannel: Bring up fabric network with one channel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;createChannel: Create and join a channel after the network is created&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;./network.sh up createChannel -ca -c mychannel -s couchdb&lt;/p&gt;

&lt;p&gt;if everything goes well that at the end you can see something like this.&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%2Fcdn-images-1.medium.com%2Fmax%2F2598%2F1%2AYptw-ETBFhqzFJIpd8KrXg.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%2Fcdn-images-1.medium.com%2Fmax%2F2598%2F1%2AYptw-ETBFhqzFJIpd8KrXg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we have completed step 1 to step 5 in the **High-level Walk-through **section of this article.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Next step to deploy chaincode, in the example repository there are multiple chaincode&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;-ccn: Chaincode name. This flag can be used to deploy one of the asset transfer samples to a channel. Sample options: basic (default), ledger, private, sbe, secured&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;-ccl: Programming language of the chaincode to deploy: go (default), java, javascript, typescript&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;-verbose: Verbose mode&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can explore all the available options in the help section&lt;/p&gt;

&lt;p&gt;./network.sh deployCC -ccn basic -ccl go -verbose&lt;/p&gt;

&lt;p&gt;once the chaincode deployment is successful you will get output like this&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%2Fcdn-images-1.medium.com%2Fmax%2F2594%2F1%2AwJCN2WP77EfWwByAkqISTw.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%2Fcdn-images-1.medium.com%2Fmax%2F2594%2F1%2AwJCN2WP77EfWwByAkqISTw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now over chaincode deployment is done and we will now invoke and query the chaincode. we will be using nodejs SDK to interact with the blockchain network.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;cd ../asset-transfer-basic/application-javascript &amp;amp;&amp;amp; npm install&lt;/p&gt;

&lt;p&gt;node app.js&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%2Fcdn-images-1.medium.com%2Fmax%2F2590%2F1%2AD9Y0qONnnafusuub8Uvihw.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%2Fcdn-images-1.medium.com%2Fmax%2F2590%2F1%2AD9Y0qONnnafusuub8Uvihw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This script will register the user &lt;em&gt;**appUser *&lt;/em&gt;&lt;em&gt;into the FileSystemWallet and will invoke the *&lt;/em&gt;&lt;em&gt;InitLedger&lt;/em&gt;** function of the chaincode and this function will create 6 records into the ledger. After this it call the function &lt;strong&gt;*GetAllAssets **and this function returns all the records stored in the ledger, we can access the CouchDB via the fauxton UI (&lt;/strong&gt; peer0.org1=&amp;gt; &lt;a href="http://localhost:7984/_utils/#" rel="noopener noreferrer"&gt;***http://localhost:5984/_utils/#&lt;/a&gt; and **peer0.org2=&amp;gt; &lt;a href="http://localhost:7984/_utils/#" rel="noopener noreferrer"&gt;**http://localhost:7984/_utils/#&lt;/a&gt;). After that it call **CreateAsset **which add a new record into the ledger.&lt;/p&gt;

&lt;p&gt;And we have out HLF network up and running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;I hope this article provides insight into Setting up Fabric Network v2.2 using the Fabric CA Servers along with CouchDB as the state database.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>hyperledger</category>
      <category>hyperledgerfabric</category>
      <category>blockchainnetwork</category>
    </item>
  </channel>
</rss>
