<?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: Zakir Sajib</title>
    <description>The latest articles on Forem by Zakir Sajib (@zakirsajib).</description>
    <link>https://forem.com/zakirsajib</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%2F270317%2Fc8f61cd0-11f1-4b49-bc61-0ba560b9c85d.png</url>
      <title>Forem: Zakir Sajib</title>
      <link>https://forem.com/zakirsajib</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/zakirsajib"/>
    <language>en</language>
    <item>
      <title>Spice up your mac desktop with custom widgets, yes we can write our own widgets using coffeescript, HTML5, JSX, React and JS</title>
      <dc:creator>Zakir Sajib</dc:creator>
      <pubDate>Sat, 26 Mar 2022 09:45:08 +0000</pubDate>
      <link>https://forem.com/zakirsajib/spice-up-your-mac-desktop-with-custom-widgets-yes-we-can-write-our-own-widgets-using-coffeescript-html5-jsx-react-and-js-1o4l</link>
      <guid>https://forem.com/zakirsajib/spice-up-your-mac-desktop-with-custom-widgets-yes-we-can-write-our-own-widgets-using-coffeescript-html5-jsx-react-and-js-1o4l</guid>
      <description>&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%2F9zd7fbiqd6eouwgkn0hc.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%2F9zd7fbiqd6eouwgkn0hc.png" alt="Mac custom widgets in desktop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are native widgets available in mac but I don't like them.&lt;/p&gt;

&lt;p&gt;I like my own widgets on desktop. Such as Weather, Currency, Timezones, or even system information such as CPU load, Memory status, Disk or network status. I want full control what these widgets are doing and most importantly I want to hone my existing skill sets - JavaScript, React, JSX and learn new thing CoffeeScript. &lt;/p&gt;

&lt;p&gt;Its possible using third party app called - &lt;a href="https://tracesof.net/uebersicht/" rel="noopener noreferrer"&gt;ubersicht&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Its a mac app which will allow any JavaScript, React, JSX code to run on your mac desktop and display information. &lt;/p&gt;

&lt;p&gt;According to Übersicht:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Übersicht lets you run system commands and display their output on your desktop in little containers, called widgets.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First you need to download and install Übersicht and you can put it into &lt;code&gt;applications&lt;/code&gt; folder or anywhere and run it. &lt;/p&gt;

&lt;p&gt;Location of all its widgets are: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;..../Library/Application Support/Übersicht/widgets&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Inside this widgets folder we can create our own widgets. &lt;/p&gt;

&lt;p&gt;The widgets folder may look 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F685fpycr13r3qruo8bwn.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%2F685fpycr13r3qruo8bwn.png" alt="The widgets folder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Folder syntax: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;{foldername}.widget&lt;/code&gt;&lt;br&gt;
&lt;code&gt;hello-world.widget&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.widget&lt;/code&gt; is important, without this &lt;code&gt;.widget&lt;/code&gt; Übersicht won't recognize. &lt;br&gt;
You can also write your code without creating any folder. Just put your &lt;code&gt;index.js&lt;/code&gt; or &lt;code&gt;index.jsx&lt;/code&gt; or &lt;code&gt;index.coffee&lt;/code&gt; within this location (&lt;code&gt;..../Library/Application Support/Übersicht/widgets&lt;/code&gt;) and it will run if there is no error. &lt;/p&gt;

&lt;h3&gt;
  
  
  How to Debug
&lt;/h3&gt;

&lt;p&gt;Its quite similar the way we inspect element in browser.&lt;/p&gt;

&lt;p&gt;click &lt;code&gt;show debug console&lt;/code&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%2Fuploads%2Farticles%2Fwf07c0p907vk8o5ds7py.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%2Fwf07c0p907vk8o5ds7py.png" alt="Debugging 1"&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%2Fuploads%2Farticles%2Fkghrmzmnudaw12x6ff65.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%2Fkghrmzmnudaw12x6ff65.png" alt="Debugging 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A minimal widget looks like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Code&lt;/strong&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%2Fuploads%2Farticles%2Fhumziy8btie9sj288pq7.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%2Fhumziy8btie9sj288pq7.png" alt="Sample Code to run your own Übersicht widget"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A sample of complex code to see current month as calendar in CoffeeScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample Code&lt;/strong&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%2Fuploads%2Farticles%2Fde59mkdjnhjkgo6hbqwj.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%2Fde59mkdjnhjkgo6hbqwj.png" alt="A complex code to see current month as calendar in CoffeeScript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;so you got an idea now!&lt;/p&gt;

&lt;p&gt;Your widget can have following stuffs at a minimal level:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

README.md
screenshot.png
widget.json
index.coffee


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

&lt;/div&gt;

&lt;p&gt;or &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

README.md
screenshot.png
widget.json
index.js


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

&lt;/div&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

README.md
screenshot.png
widget.json
index.jsx


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

&lt;/div&gt;

&lt;p&gt;To learn more how to write your code see this &lt;a href="https://github.com/felixhageloh/uebersicht" rel="noopener noreferrer"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you prefer writing in coffeescript then this &lt;a href="https://github.com/felixhageloh/uebersicht/blob/master/ClassicWidgets.md" rel="noopener noreferrer"&gt;link&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;If you download first existing widgets to try and see how they look then its available in &lt;a href="https://tracesof.net/uebersicht-widgets/" rel="noopener noreferrer"&gt;Übersicht official site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After writing your own widget, you can &lt;a href="https://tracesof.net/uebersicht-widgets/#submit" rel="noopener noreferrer"&gt;submit your widget&lt;/a&gt; to Übersicht. &lt;/p&gt;

&lt;h2&gt;
  
  
  Small Warning:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;All widgets are built by third party developers in Übersicht and most of them are nonreponsive to answer your any queries or question (well, it happened to me but you might be lucky). you can create issue in their github and wait for their response. Good luck.&lt;/li&gt;
&lt;li&gt;Don't mess around with system command. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My Currency convertor.
&lt;/h2&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;a href="https://github.com/zakirsajib/bdt-currency-converter.widget" rel="noopener noreferrer"&gt;Currency Conversion&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
      <category>ubersicht</category>
    </item>
    <item>
      <title>Adding a Loader Spinner in Gatsby site</title>
      <dc:creator>Zakir Sajib</dc:creator>
      <pubDate>Wed, 17 Jun 2020 08:13:48 +0000</pubDate>
      <link>https://forem.com/zakirsajib/adding-loader-spinner-in-gatsby-site-n4g</link>
      <guid>https://forem.com/zakirsajib/adding-loader-spinner-in-gatsby-site-n4g</guid>
      <description>&lt;p&gt;Usually we don't need a loader spinner in our Gatsby site as Gatsby sites usually fast (Super blazing fast as Gatsby.org claims). But in some situations we need it. For example if our users have poor WiFi or or slow 2G/3G connections then it would be a good idea to have loader spinner before contents load. I tested my few Gatsby sites in Regular 3G, 4G and WIFI it super fast but when I tested in slow 2G or DSL it takes time. That's the reality. &lt;/p&gt;

&lt;p&gt;So I was wondering if I could show a some kind of loading spinner before the site or components are ready.&lt;/p&gt;

&lt;h2&gt;
  
  
  html.js
&lt;/h2&gt;

&lt;p&gt;This is the file where we will use to load our spinner.&lt;/p&gt;

&lt;p&gt;First step: Type the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cp .cache/default-html.js src/html.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will copy the default-html.js file and rename it as html.js and will place it in our src directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W3HsBf1N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dkhckp3ftmxx4ketpg3y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W3HsBf1N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dkhckp3ftmxx4ketpg3y.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you open the html.js file you may see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react"
import PropTypes from "prop-types"

export default function HTML(props) {
  return (
    &amp;lt;html {...props.htmlAttributes}&amp;gt;
      &amp;lt;head&amp;gt;
        &amp;lt;meta charSet="utf-8" /&amp;gt;
        &amp;lt;meta httpEquiv="x-ua-compatible" content="ie=edge" /&amp;gt;
        &amp;lt;meta
          name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no"
        /&amp;gt;
        {props.headComponents}
      &amp;lt;/head&amp;gt;
      &amp;lt;body {...props.bodyAttributes}&amp;gt;
        {props.preBodyComponents}
        &amp;lt;div
          key={`body`}
          id="___gatsby"
          dangerouslySetInnerHTML={{ __html: props.body }}
        /&amp;gt;
        {props.postBodyComponents}
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  )
}

HTML.propTypes = {
  htmlAttributes: PropTypes.object,
  headComponents: PropTypes.array,
  bodyAttributes: PropTypes.object,
  preBodyComponents: PropTypes.array,
  body: PropTypes.string,
  postBodyComponents: PropTypes.array,
}

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

&lt;/div&gt;



&lt;p&gt;Now we have to add our own image (either gif or SVG) and add few lines of css and regular javascript codes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import LoaderSVG from './img/loader.svg'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div
    key={`loader`}
              id="___loader"
              style={{
                alignItems: "center",
                backgroundColor: "#F2F2F2",
                display: "flex",
                justifyContent: "center",
                position: "absolute",
                left: 0,
                top: 0,
                right: 0,
                bottom: 0,
                zIndex: 100,
              }}
           &amp;gt;
              &amp;lt;img 
                src={LoaderSVG} 
                alt="loading spinner" 
                width="150" 
                height="150"
              /&amp;gt;
        &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complete code something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react"
import PropTypes from "prop-types"
import LoaderSVG from './img/loader.svg'

export default function HTML(props) {
  return (
    &amp;lt;html {...props.htmlAttributes}&amp;gt;
      &amp;lt;head&amp;gt;
        &amp;lt;meta charSet="utf-8" /&amp;gt;
        &amp;lt;meta httpEquiv="x-ua-compatible" content="ie=edge" /&amp;gt;
        &amp;lt;meta
          name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no"
        /&amp;gt;
        {props.headComponents}
      &amp;lt;/head&amp;gt;
      &amp;lt;body {...props.bodyAttributes}&amp;gt;
        {props.preBodyComponents}
        &amp;lt;div
              key={`loader`}
              id="___loader"
              style={{
                alignItems: "center",
                backgroundColor: "#F2F2F2",
                display: "flex",
                justifyContent: "center",
                position: "absolute",
                left: 0,
                top: 0,
                right: 0,
                bottom: 0,
                zIndex: 100,
              }}
           &amp;gt;
           &amp;lt;img 
              src={LoaderSVG} 
              alt="loading spinner" 
              width="150" 
              height="150"
           /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div
          key={`body`}
          id="___gatsby"
          dangerouslySetInnerHTML={{ __html: props.body }}
        /&amp;gt;
        {props.postBodyComponents}
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  )
}

HTML.propTypes = {
  htmlAttributes: PropTypes.object,
  headComponents: PropTypes.array,
  bodyAttributes: PropTypes.object,
  preBodyComponents: PropTypes.array,
  body: PropTypes.string,
  postBodyComponents: PropTypes.array,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will add our JavaScript code in &lt;code&gt;gatsby-browser.js&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Add this following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const onInitialClientRender = () =&amp;gt; {
    setTimeout(function() {
        document.getElementById("___loader").style.display = "none"
    }, 1000)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save it. &lt;/p&gt;

&lt;p&gt;Now we can restart our Gatsby. And run the site. You may not notice the loader spinner if you have fast connection but if you can change your connection to DSL or slow 2G and refresh the browser then you will see the loader. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pBBcSC_0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3k7ioft09ungezvdyfh3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pBBcSC_0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3k7ioft09ungezvdyfh3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Vb1NFdE7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1rq82z7lksdv59n1tjvj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Vb1NFdE7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1rq82z7lksdv59n1tjvj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reference: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.gatsbyjs.org/docs/custom-html/"&gt;https://www.gatsbyjs.org/docs/custom-html/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.gatsbyjs.org/docs/browser-apis/#onInitialClientRender"&gt;https://www.gatsbyjs.org/docs/browser-apis/#onInitialClientRender&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Live site: &lt;br&gt;
&lt;a href="https://nirvana.netlify.app/"&gt;Nirvana&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gatsby</category>
    </item>
    <item>
      <title>Display your repository from GitHub using GraphQL API</title>
      <dc:creator>Zakir Sajib</dc:creator>
      <pubDate>Tue, 16 Jun 2020 10:36:57 +0000</pubDate>
      <link>https://forem.com/zakirsajib/display-your-repository-from-github-using-graphql-api-2927</link>
      <guid>https://forem.com/zakirsajib/display-your-repository-from-github-using-graphql-api-2927</guid>
      <description>&lt;p&gt;I am building a website using Gatsby and main focus of this website would be showcase of my skills, experiences. Basically a kind of visual resume.&lt;/p&gt;

&lt;p&gt;As a developer, I want to showcase my coding quality to potential clients. Almost all my codes are saved into GitHub and there are many repositories but I want to display only the important ones. I used the pinned feature of GitHub where you can show only 6 repositories.&lt;/p&gt;

&lt;p&gt;So I want to display &lt;strong&gt;5 pinned repositories&lt;/strong&gt; of my choice and display each repository’s &lt;strong&gt;name&lt;/strong&gt;, &lt;strong&gt;url&lt;/strong&gt;, &lt;strong&gt;description&lt;/strong&gt; and home page &lt;strong&gt;url&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Kxc1URHd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oj8xa6ey4am9suw2xjmc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Kxc1URHd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oj8xa6ey4am9suw2xjmc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3O0cBoQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/k1ftnwvztmago9plljyh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3O0cBoQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/k1ftnwvztmago9plljyh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Final product of repository section &lt;a href="https://zsresume.netlify.app/"&gt;my resume site&lt;/a&gt; where 5 repositories are displayed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V2MFuBT1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qn9vri5wvb591thgrmny.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V2MFuBT1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qn9vri5wvb591thgrmny.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article, I will discuss how you can pull repository from GitHub using GraphQL API in Gatsby site.&lt;/p&gt;

&lt;p&gt;First we need to find out an appropriate Gatsby plugin for GitHub. There are few plugins out there but I found the following one which works and do the job:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.gatsbyjs.org/packages/gatsby-source-github/"&gt;gatsby-source-github&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets install this plugin in our Gatsby project. In your Gatsby project’s root, type the following command in terminal: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install gatsby-source-github --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After successfully installing the plugin, lets open the &lt;strong&gt;gatsby-config.js&lt;/strong&gt; file in editor.&lt;/p&gt;

&lt;p&gt;But hang on, before doing that we need to create our &lt;strong&gt;personal access token&lt;/strong&gt; for our GitHub account.&lt;/p&gt;

&lt;p&gt;Personal access tokens function like ordinary OAuth access tokens. They can be used instead of a password for Git over HTTPS, or can be used to &lt;a href="https://developer.github.com/v3/auth/#basic-authentication"&gt;authenticate to the API over Basic Authentication&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Its very easy and straight forward.&lt;/p&gt;

&lt;p&gt;Login to your GitHub account and go to &lt;em&gt;settings&lt;/em&gt; and then &lt;em&gt;Developer settings&lt;/em&gt;. In Developer settings page, click the &lt;em&gt;generate new token button&lt;/em&gt;. In &lt;em&gt;New personal access token&lt;/em&gt; page give a meaningful name of the token in &lt;em&gt;note&lt;/em&gt; field and select correct scope(s).&lt;/p&gt;

&lt;p&gt;Selecting only &lt;code&gt;public_repo&lt;/code&gt; will do the job. And then click save token.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wAM6fs7h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vpogm15pfdiqb74rdhia.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wAM6fs7h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vpogm15pfdiqb74rdhia.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have our token, copy it and paste in &lt;code&gt;gatsby-config.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
   resolve: 'gatsby-source-github',
         options: {
           headers: {
             Authorization: `Bearer pasteyourpersonalaccesstokencodehere`,
           },
           queries: [
             `{ viewer {                 
               pinnedItems(first: 5, types: REPOSITORY){
                nodes {
                  ... on Repository {
                 id                       
                 name
                 url
                 description
                 homepageUrl
               }
              }
             }}
           }`,
        ],
     },
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code block will go within&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins: [

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

&lt;/div&gt;



&lt;p&gt;block in &lt;code&gt;gatsby-config.js&lt;/code&gt; file. This is important.&lt;/p&gt;

&lt;p&gt;Now our &lt;strong&gt;personal access token&lt;/strong&gt; will go in &lt;strong&gt;options: headers: Authorization&lt;/strong&gt; field after the word &lt;strong&gt;Bearer&lt;/strong&gt;. Don’t remove the word &lt;strong&gt;Bearer&lt;/strong&gt;. This is important.&lt;/p&gt;

&lt;p&gt;Now the queries part. The query starts with &lt;code&gt;{ viewer { ... } }&lt;/code&gt;. This is the query I built using GitHub GraphQL API explorer which is found here: &lt;a href="https://developer.github.com/v4/explorer/"&gt;https://developer.github.com/v4/explorer/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You need to login to your GitHub account and go to to the &lt;a href="https://developer.github.com/v4/explorer/"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;GitHub’s GraphQL API Explorer makes use of your &lt;strong&gt;real, live, production data&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k8MRMM3e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xfzkcdi3p7q62i1c7g2n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k8MRMM3e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xfzkcdi3p7q62i1c7g2n.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here I crafted the queries and then copy and paste it into &lt;code&gt;gatsby-config.js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;My goal was to find out all repository from pinned items so my query was like this: &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Ahzq72lAyqc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;After you copy the code and paste into &lt;code&gt;gatsby-config.js&lt;/code&gt; file, save it.&lt;/p&gt;

&lt;p&gt;One of the important parts is done.&lt;/p&gt;

&lt;p&gt;Now lets move on to second part.&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;gatsby develop&lt;/code&gt; command in terminal.&lt;/p&gt;

&lt;p&gt;Open browser and connect:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://localhost:8000/__graphQL"&gt;https://localhost:8000/__graphQL&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;From here, we will build our custom query and will use it on our code later.&lt;/p&gt;

&lt;p&gt;We will create a component for example Repository and copy the query and paste there. So lets build the query first. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iqejt94V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://zsbloggatsby.files.wordpress.com/2020/01/fireshot-capture-178-graphiql_-http___localhost_8009___graphql.jpg%3Fw%3D768" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iqejt94V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://zsbloggatsby.files.wordpress.com/2020/01/fireshot-capture-178-graphiql_-http___localhost_8009___graphql.jpg%3Fw%3D768" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;so this is our query which shows all pinned repository items and their meta data such as &lt;strong&gt;name, url, description and home page url&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Keep the GraphQL window open and now create a component called &lt;strong&gt;Repository.js&lt;/strong&gt; file inside &lt;strong&gt;src/components&lt;/strong&gt; directory of your &lt;strong&gt;gatsby&lt;/strong&gt; project.&lt;/p&gt;

&lt;p&gt;This is my Repository.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import {useStaticQuery, graphql} from 'gatsby'

const Repository = () =&amp;gt; {
const data = useStaticQuery(graphql`
   query MyQuery {
      githubViewer {
    pinnedItems {
    nodes {
      description
      homepageUrl
      name
      url
    }
      }
    }
  }
`)
return(
&amp;lt;div&amp;gt;
{data.githubViewer.pinnedItems.nodes.map((repository, i) =&amp;gt; (
    &amp;lt;div className="pf-grid-item" key={i}&amp;gt;
      &amp;lt;a className="pf-project" 
         href={repository.url} 
         target="_blank" 
         rel="noopener noreferrer"&amp;gt;
         GitHub URL
      &amp;lt;/a&amp;gt;
      &amp;lt;h2&amp;gt;{repository.name}&amp;lt;/h2&amp;gt;
      &amp;lt;p&amp;gt;{repository.description}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;{repository.homepageUrl}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
))}
&amp;lt;/div&amp;gt;
)
};
export default Repository
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I used &lt;code&gt;useStaticQuery&lt;/code&gt; hook to query the data, but you can choose either &lt;code&gt;useStaticQuery&lt;/code&gt; or &lt;code&gt;StaticQuery&lt;/code&gt; or &lt;code&gt;Page query&lt;/code&gt;. See the screenshot below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3A_7N1j2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://zsbloggatsby.files.wordpress.com/2020/01/code-explorer.jpg%3Fw%3D768" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3A_7N1j2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://zsbloggatsby.files.wordpress.com/2020/01/code-explorer.jpg%3Fw%3D768" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see &lt;strong&gt;nodes&lt;/strong&gt; data comes as an array so I used &lt;strong&gt;map&lt;/strong&gt; method to loop through the data and accessed the data repository with dot operator.&lt;/p&gt;

&lt;p&gt;Now you can call this &lt;strong&gt;Repository&lt;/strong&gt; component from other component like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Repository /&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;This is an example of how we can use GraphQL to retrieve data from any third party application. This example could be a starting point to start with and sky is the limit to explore unknown!&lt;/p&gt;

&lt;p&gt;Live example: &lt;a href="https://zsresume.netlify.app/"&gt;https://zsresume.netlify.app/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>graphql</category>
      <category>github</category>
      <category>react</category>
    </item>
  </channel>
</rss>
