<?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>DEV Community: Jacob Knaack</title>
    <description>The latest articles on DEV Community by Jacob Knaack (@jacobknaack).</description>
    <link>https://dev.to/jacobknaack</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%2F136876%2F046f596b-788a-4951-ab81-65405b0970ab.png</url>
      <title>DEV Community: Jacob Knaack</title>
      <link>https://dev.to/jacobknaack</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jacobknaack"/>
    <language>en</language>
    <item>
      <title>Creating a Bash Command</title>
      <dc:creator>Jacob Knaack</dc:creator>
      <pubDate>Tue, 10 Sep 2019 20:01:39 +0000</pubDate>
      <link>https://dev.to/jacobknaack/creating-a-bash-command-341h</link>
      <guid>https://dev.to/jacobknaack/creating-a-bash-command-341h</guid>
      <description>&lt;p&gt;Let's walk through the process of adding a fresh baby bash script as a command that you can run in your terminal.&lt;/p&gt;

&lt;p&gt;Say you have a super rad script that does mega cool things somewhere on your computer:&lt;/p&gt;

&lt;h3&gt;
  
  
  hello-world
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;


&lt;span class="nb"&gt;echo &lt;/span&gt;Hello world
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Allowing your bash terminal to run this script file as a command just takes a few simple steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Place your script somewhere that scripts go.
&lt;/h2&gt;

&lt;p&gt;I recommend creating a directory at &lt;code&gt;~/&lt;/code&gt; called &lt;code&gt;bin&lt;/code&gt; or &lt;code&gt;.bin&lt;/code&gt;.  The bin folder is typically where commands go, so I'm going to replicate this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ cd ~/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ mkdir .bin/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ mv path/to/script ~/.bin/YOUR-SCRIPT-NAME&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Add this Directory to your PATH.
&lt;/h2&gt;

&lt;p&gt;Add a line to your .bash_profile that will make sure your bash terminal has a path to this file when running your command.&lt;/p&gt;

&lt;p&gt;If you don't have one create one with&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ touch ~/.bash_profile&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  .bash_profile
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#Here's the line you should add:&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;:/Users/YOUR-USERNAME/.bin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can either restart your terminal at this point or source this file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;source ~/.bash_profile&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Add Permissions for Users to run this command.
&lt;/h2&gt;

&lt;p&gt;We'll use the illusive &lt;code&gt;chmod&lt;/code&gt; command to let users run this newly created command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ chmod u+x ~/.bin/YOUR-SCRIPT-NAME&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that's it! You should now be able to run your script from anywhere in your terminal.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>terminal</category>
      <category>command</category>
      <category>script</category>
    </item>
    <item>
      <title>Making A Bunch of Requests from a Node Server?  Try Promise.all!</title>
      <dc:creator>Jacob Knaack</dc:creator>
      <pubDate>Tue, 30 Jul 2019 22:14:36 +0000</pubDate>
      <link>https://dev.to/jacobknaack/making-a-bunch-of-requests-from-a-node-server-try-promise-all-23fl</link>
      <guid>https://dev.to/jacobknaack/making-a-bunch-of-requests-from-a-node-server-try-promise-all-23fl</guid>
      <description>&lt;p&gt;Sometimes you find yourself needing to make a ton of http requests.  For the most part this a bad idea, and you should really be abstracting your requests and not hammering a REST API since that's how you break things on the internet.&lt;/p&gt;

&lt;p&gt;Buuuuuut for testing purposes or just trying to get something to friggin' work, we might be feeling a lil' hacky.  We are developers ya know, and girls just want to have fun!&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%2Fi.makeagif.com%2Fmedia%2F10-24-2015%2FKP9C6v.gif" 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%2Fi.makeagif.com%2Fmedia%2F10-24-2015%2FKP9C6v.gif" alt="hack-a-lackin"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer:
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Please don't use this on any existing REST service that you don't control.  This will possibly break any rate restrictions that service has put in place, and worse case could overload their servers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I found myself in one of these scenarios while testing a bunch of mock spreadsheet data, where I wanted to make hundreds of requests to some server routes our team was building.  Thus, the advent of this Node code you see below you.&lt;/p&gt;

&lt;p&gt;Before continuing, this hack expects some knowledge around JS Promises, ES6 Syntax, and Node Modules.  With that out of the way let's look at this solution and break it down:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Our Solution
&lt;/h4&gt;

&lt;p&gt;This module doesn't do anything super fancy.  But it does utilize some super fancy built in javascript Objects. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It formats an array of promises that we can feed into &lt;code&gt;Promise.all&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When those asynchronously resolve we get a big happy bundle (an array for those strictly typed folks) of response objects :).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  How do we Achieve this?
&lt;/h4&gt;

&lt;p&gt;We leverage a handy Array prototype &lt;code&gt;.map&lt;/code&gt; method to turn our array of request options, into new array containing promises:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promiseArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reqArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="c1"&gt;// things our Promise should do&lt;/span&gt;
&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each Promise will &lt;em&gt;resolve&lt;/em&gt; result of the request or &lt;em&gt;reject&lt;/em&gt; the error if the request fails, asynchronously of course:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;httpPromise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;finally we just return the result of &lt;code&gt;Promise.all&lt;/code&gt; which we pass our newly created array of Promises, or console an error if we get errors in those requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;promiseArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;responses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;responses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bulk request failed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this helps with whatever crazy asynchronous actions you're trying to accomplish.  This can easily be refactored for use in other environments besides Node and can be used with other events (Database queries, Cloud Resource Interactions) that you want Javascript to handle only upon completion.&lt;/p&gt;

&lt;p&gt;Happy Hacking :)&lt;/p&gt;

</description>
      <category>node</category>
      <category>async</category>
      <category>javascript</category>
      <category>promise</category>
    </item>
    <item>
      <title>Building a Full Stack SMS-enabled Chat Application using Twilio, Node.js, GraphQL and Cosmic JS</title>
      <dc:creator>Jacob Knaack</dc:creator>
      <pubDate>Fri, 21 Jun 2019 19:55:35 +0000</pubDate>
      <link>https://dev.to/jacobknaack/building-a-full-stack-sms-enabled-chat-application-using-twilio-node-js-graphql-and-cosmic-js-24f3</link>
      <guid>https://dev.to/jacobknaack/building-a-full-stack-sms-enabled-chat-application-using-twilio-node-js-graphql-and-cosmic-js-24f3</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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbtzxs1o0lenwcwa8tqky.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbtzxs1o0lenwcwa8tqky.png" alt="cosmic messenger splash"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What a day to build!  Today we are exploring how far chat has come, and how easy it is build a fully functional chat application complete with the ability to send SMS messages with just a few (super-rad) developer tools that are available to anyone with fingertips. There's quite a lot to cover so let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  TLDR:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://cosmicjs.com/apps/cosmic-messenger" rel="noopener noreferrer"&gt;Node.js Chat App Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/cosmicjs/nodejs-chat-app" rel="noopener noreferrer"&gt;Node.js Chat App Source Code&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Let's talk about &lt;strong&gt;goals&lt;/strong&gt;, what we want to accomplish in the next hour or so.&lt;/p&gt;

&lt;p&gt;Our application is at it's core a chat system. We're going to keep things very simple here. Our system will be able to register new users with minimal user information,  create and emit messages to users in the chat room, and finally log users out of the chat room when they want to leave.&lt;/p&gt;

&lt;p&gt;For bonus points we are also going to configure our system with an SMS notification system, that will send a Text notification to an admin account whenever a message is sent across our system.  Pretty cool right?&lt;/p&gt;

&lt;h3&gt;
  
  
  Before We Start
&lt;/h3&gt;

&lt;p&gt;This system is going to utilize a number of technologies to function.  Most importantly Cosmic JS for managing all of our data:  our Users, and our Messages.  In order to follow along with this tutorial you should a free bucket on their platform and create a &lt;strong&gt;User&lt;/strong&gt; and a &lt;strong&gt;Message&lt;/strong&gt; Object.&lt;/p&gt;

&lt;p&gt;in order to send SMS messages we are leveraging &lt;a href="https://www.twilio.com/" rel="noopener noreferrer"&gt;Twilio&lt;/a&gt;.  A communications platform that allows developers to make phone calls and send text messages through a web API.  To implement our notifications you'll have to register for a free Twilio number and start a trial account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Software Requirements?
&lt;/h3&gt;

&lt;p&gt;We are using Node JS as our runtime environment so please be sure you have a recent(ish) version of node installed.  With that business out of the way we can start building.&lt;/p&gt;

&lt;h3&gt;
  
  
  High Level Overview
&lt;/h3&gt;

&lt;p&gt;This is a full stack application, which means we are building a web-server to handle our requests and serve our client side application.  We are going to create an &lt;strong&gt;Express&lt;/strong&gt; application that will run on our Node JS server to handle routing to a small API and serve HTML, and &lt;strong&gt;Webpack&lt;/strong&gt; to bundle our client interface built with &lt;strong&gt;React&lt;/strong&gt; and &lt;strong&gt;Graphql&lt;/strong&gt;.  This way we can utilize a central server to make requests to the different parts of our application: our Interface, our Controllers, and our Web Services.&lt;/p&gt;

&lt;p&gt;There are quite a few moving parts here so let's jump in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Building Our Web Server
&lt;/h2&gt;

&lt;p&gt;This is backbone our our app which will allow us to control the various pieces of our core application.  We are going to start by creating and initializing a project directory where all of our dependencies will be installed.  Let's open up our terminal and create some files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir chat 
$ cd chat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This creates a directory called &lt;strong&gt;chat&lt;/strong&gt; and changes our current directory into that chat directory.  Now we can initialize this directory as a &lt;strong&gt;Node&lt;/strong&gt; project:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Your terminal will present you with a couple of prompts to create our &lt;strong&gt;package.json&lt;/strong&gt; file that will contain most of the metadata about our project.  I recommend hitting the enter key through these steps unless you know some specific information you'd like to give your application.  You can always change these values later.&lt;/p&gt;

&lt;p&gt;Now let's install some &lt;strong&gt;Node Modules&lt;/strong&gt; we need to run our project and save them to &lt;strong&gt;package.json&lt;/strong&gt; dependency list. We are going to install our bundler &lt;strong&gt;webpack&lt;/strong&gt; along with the necessary loaders we need to bundle our JS, HTML, and SASS /CSS files as well as our server framework express:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; webpack webpack-cli clean-webpack-plugin @babel/core @babel/preset-env @babel/preset-react babel-loader file-loader sass-loader css-loader node-sass url-loader style-loader express express-session cookie-parser body-parser socket.io socket.io-client cosmicjs dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We are saving these all as project dependencies since any deployment container we use will need to use these to build and run our application.&lt;/p&gt;

&lt;p&gt;Next we are also going to install the dependencies required for rendering our User Interface:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; react react-dom react-router-dom react-icons graphql react-apollo apollo-boost axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Setting Up Our Entry File
&lt;/h3&gt;

&lt;p&gt;Now that we have some dependencies installed, we need to create an entry file that will handle all of the requests to our application.  When a user makes a request to the default route '/', we will serve an HTML file.  When the client makes a request to our API, we use endpoints appended with '/api'.  The first step is just creating the file:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ touch index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Let's open this file in our text editor and set up &lt;strong&gt;Express&lt;/strong&gt; so that we serve some HTML when a browser navigates to our localhost server:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We are looking for an &lt;strong&gt;index.html&lt;/strong&gt; file that is kept in a directory located at &lt;strong&gt;./public&lt;/strong&gt; so let's go ahead and create this file at &lt;strong&gt;./public/index.html&lt;/strong&gt; and insert some boilerplate to make sure our HTML is being served by our express server.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We should be able to start our server using &lt;strong&gt;node&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;When this command runs we should see a simple console message:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cosmic Messenger listening on port : 3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now pointing our browser to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; will show us a mostly blank screen but now we see a little "Hello World" at top of our page.  Now our server is set up to serve content from our html file.&lt;/p&gt;
&lt;h3&gt;
  
  
  Configuring Webpack
&lt;/h3&gt;

&lt;p&gt;We want to build our interface with &lt;strong&gt;React&lt;/strong&gt;, but we need to serve this interface from a directory that our node server can access.  We also need to compile our javascript from the fancy syntax that we use to build our components to something that all browsers can process.  To do this we are going to use &lt;strong&gt;Webpack&lt;/strong&gt; to bundle all of our files into a specific location, and Babel to compile all of our code.&lt;/p&gt;

&lt;p&gt;Let's create a file called &lt;strong&gt;webpack.config.js&lt;/strong&gt; and add some configuration for bundling our client interface:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;This is going to allow us to create source code and organize it in a meaningful way using whatever directory structure we like for structuring logic, then bundle it all into one file that our &lt;strong&gt;index.html&lt;/strong&gt; can reference while it's served from our webServer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initializing Our React App
&lt;/h3&gt;

&lt;p&gt;We have our config file created, but now we need to create some source code and make sure &lt;strong&gt;webpack&lt;/strong&gt; bundles everything properly.  Let's go ahead and create a folder called src and touch a file called &lt;strong&gt;app.js&lt;/strong&gt; within.  From there we can create a simple React component that will render the same thing as before, but now we are serving javascript bundled together and injected into our index.html served from our web server.  Server side rendering baby!&lt;/p&gt;

&lt;p&gt;Here's what our &lt;strong&gt;app.js&lt;/strong&gt; file will look like initially:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Before we run &lt;strong&gt;webpack&lt;/strong&gt; and serve our interface we need to install some developer dependencies and add some simple configuration values to our &lt;strong&gt;package.json&lt;/strong&gt;. Specifically we need to tell our server that we are using &lt;strong&gt;babel&lt;/strong&gt; to compile our interface code and some npm scripts so that we can run our Web Server and bundle our React code.&lt;/p&gt;

&lt;p&gt;Let's install some dependencies that we only need to use for local development purposes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; morgan nodemon webpack-dev-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;With those installed, let's open &lt;strong&gt;package.json&lt;/strong&gt; and add a &lt;strong&gt;prestart&lt;/strong&gt;, &lt;strong&gt;start&lt;/strong&gt;, and &lt;strong&gt;dev&lt;/strong&gt; properties to our scripts object as well as a &lt;strong&gt;babel&lt;/strong&gt; config object. Here's how things should look:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Now we can run &lt;strong&gt;webpack&lt;/strong&gt; and node simultaneously by simply running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In a second you'll see some output text from &lt;strong&gt;webpack&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffh0e16ho5b0po7lvogp6.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffh0e16ho5b0po7lvogp6.png" alt="Initial Webpack Output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Heading back to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; should produce the same result as before, but now we are serving a &lt;strong&gt;React&lt;/strong&gt; application allowing us to create sensible component classes and render them within our &lt;strong&gt;index.html&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating our REST API
&lt;/h3&gt;

&lt;p&gt;We are going to interface with our Cosmic JS resources by making requests to our server code via &lt;strong&gt;express&lt;/strong&gt; routes which we'll configure right now.&lt;/p&gt;

&lt;p&gt;We will need three &lt;strong&gt;POST&lt;/strong&gt; routes that will handle requests to our server.  One for registering users that visit the app, one for messages that get sent through the messenger, and a logout route for users wanting to leave the chat.&lt;/p&gt;

&lt;p&gt;We also want to configure middleware for handling the request bodies sent through our api, body-parser,  a module for creating session cookies to weakly authenticate requests to our messaging service, express-session. Finally we need to configure web sockets so that we can emit events to all the clients connected to our server via socket.io.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Socket.io&lt;/strong&gt; will emit 3 separate events for the time being: One when a user &lt;strong&gt;registers&lt;/strong&gt; so that other clients can track who is logged in to the app in real time.  Inversely, we are tracking a &lt;strong&gt;logout&lt;/strong&gt; event that will let user know when users have left the chat.  And one for when a  *&lt;em&gt;message&lt;/em&gt; is sent.&lt;/p&gt;

&lt;p&gt;If you didn't install them at the beginning of our server setup, you can install them with a quick &lt;strong&gt;npm&lt;/strong&gt; command:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; socket.io socket.io-client express-session body-parser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now let's open up our entry file: &lt;strong&gt;index.js&lt;/strong&gt;, and add our routes and middleware configuration:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;You'll notice several new libraries we are importing.  Most notably we are using a middleware called &lt;strong&gt;twilioNotifications&lt;/strong&gt; and &lt;strong&gt;cosmicjs&lt;/strong&gt; which need to be configured before our server will function properly.  For twilio notifications to work, we need to create some middleware that will control when an SMS message is sent.  We also need to configure this middleware with authentication tokens and keys for the twilio web server.  For cosmicjs we need to do the same, grab some Auth Tokens and save them in our environment variable config file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure Our Environment Variables
&lt;/h3&gt;

&lt;p&gt;Let's create a file called .env at the root of our project directory.  In it we'll need to store some environment variables, but also some sensitive config variables for our web services.  Here's what you need:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You'll need to grab two sets of authentication credentials for our environment variables.   From twilio you'll need your &lt;strong&gt;ACCOUNT SID&lt;/strong&gt; and &lt;strong&gt;AUTH TOKEN&lt;/strong&gt; as well as the phone number associated with your account, which will be located at &lt;a href="https://www.twilio.com/console" rel="noopener noreferrer"&gt;https://www.twilio.com/console&lt;/a&gt;.  From &lt;strong&gt;cosmicjs&lt;/strong&gt; we need to grab our read and write keys to authenticate our requests. These can be found at &lt;a href="https://cosmicjs.com/cosmic-messenger/settings/main" rel="noopener noreferrer"&gt;https://cosmicjs.com/cosmic-messenger/settings/main&lt;/a&gt;. You may have to generate these from the general settings panel.&lt;/p&gt;

&lt;p&gt;Once these are here we must update our &lt;strong&gt;webpack.config.js&lt;/strong&gt; so that we can reference these variables in our client side javascript.  The updated file should look something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You'll notice that we added some global app variables using the &lt;strong&gt;DefinePlugin&lt;/strong&gt; method of &lt;strong&gt;webpack&lt;/strong&gt;.  Now these variables can be used globally throughout our application thanks to Webpack's bundling.&lt;/p&gt;

&lt;h3&gt;
  
  
  Our SMS Notification Middleware
&lt;/h3&gt;

&lt;p&gt;Create a directory called middleware and place a couple files within:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir middleware 
$ touch middleware/twilioNotifications.js middleware/twilioClient.js middleware/config.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Our &lt;strong&gt;twilioClient&lt;/strong&gt; file will handle making the request to the Twilio API:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Our twilioNotification file will handle the request object from express and make sure that any routes that use the module will trigger the Twilio client:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Finally we are going to create a &lt;strong&gt;config.js&lt;/strong&gt; to configure our middleware with the necessary configuration variables required to make our app play nicely with &lt;strong&gt;Twilio's&lt;/strong&gt; API::&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now our app is all set to function as a chat server.  All thats left is to create our React components and make them talk to our server to function as a chat interface.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building Our Interface Components
&lt;/h3&gt;

&lt;p&gt;Our interface will be very straight forward.  We'll start by building out our &lt;strong&gt;app.js&lt;/strong&gt; file and set up two routes, one for our registration form, and another for our chat input, messages, and user list.  We also want to configure our &lt;strong&gt;graphql&lt;/strong&gt; client so that we can fetch data directly from &lt;strong&gt;Cosmic JS&lt;/strong&gt; when we are render each page.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let's create a folder called &lt;strong&gt;components&lt;/strong&gt; under the &lt;strong&gt;src&lt;/strong&gt; directory. In here we will put all of our React components that we want to import into &lt;strong&gt;app.js&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now we need to create our two components rendered within our routing logic: &lt;strong&gt;Chat&lt;/strong&gt; and  &lt;strong&gt;loginForm&lt;/strong&gt;.  We'll start with our login form at &lt;strong&gt;src/components/loginForm.js&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Next we need to create the components for our chat form, for which we'll create a new directory called &lt;strong&gt;chat&lt;/strong&gt; in the components directory.  In here we'll create three files, one for the parent chat form component: &lt;strong&gt;src/components/chat/index.js&lt;/strong&gt;:, one for the list of messages: &lt;strong&gt;src/components/chat/messageList.js&lt;/strong&gt; and one for the list of users: &lt;strong&gt;src/components/chat/userList.js&lt;/strong&gt;.  Let's start with our chat form:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This component contains our message form that sends text data to our chat server. You'll notice it also emits an event using a module we need to build for handling web socket events.  We'll get to that in a second, before that let's create our &lt;strong&gt;userList&lt;/strong&gt; and &lt;strong&gt;messageList&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Our &lt;strong&gt;UserList&lt;/strong&gt; simply displays our user's within our UI. It fetches those users from Cosmic JS's &lt;strong&gt;graphql&lt;/strong&gt; servers and then subscribes to our socket module which refetches data every time our server emits those events.&lt;/p&gt;

&lt;p&gt;Now let's create our &lt;strong&gt;MessageList&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now Let's create our socket module that will let these components subscribe to our server events.  Create a new folder called &lt;strong&gt;lib&lt;/strong&gt; and create a file within called &lt;strong&gt;socket.js&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With that, we now have a complete full stack chat application, equipped with a client interface rendered server side. With a bonus of notifying an admin when messages are sent over the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;Since this app is built and run completely from web server, we can easily deploy this using any hosting service that supports &lt;strong&gt;Node JS&lt;/strong&gt; containers.  I'd recommend &lt;a href="https://dashboard.heroku.com" rel="noopener noreferrer"&gt;Heroku&lt;/a&gt; or &lt;strong&gt;Cosmic JS&lt;/strong&gt; since they both support project structures like this and can quickly create deployments.&lt;/p&gt;

&lt;p&gt;That's all for me this week ya'll. Until the next time.&lt;/p&gt;

</description>
      <category>react</category>
      <category>node</category>
      <category>graphql</category>
      <category>cosmicjs</category>
    </item>
    <item>
      <title>Building and Designing a Portfolio Site Using Gatsby JS and Cosmic JS</title>
      <dc:creator>Jacob Knaack</dc:creator>
      <pubDate>Wed, 15 May 2019 16:35:09 +0000</pubDate>
      <link>https://dev.to/jacobknaack/building-and-designing-a-portfolio-site-using-gatsby-js-and-cosmic-js-2de7</link>
      <guid>https://dev.to/jacobknaack/building-and-designing-a-portfolio-site-using-gatsby-js-and-cosmic-js-2de7</guid>
      <description>&lt;p&gt;Creating / updating our portfolios is a necessary evil these days.  Places change, people change, and thus the cycle of content creation and management churns continuously.  When you find yourself in need of  a portfolio redesign, there are tons of tools and services to consider.  One that should currently peak your interest is the &lt;a href="https://www.gatsbyjs.org/"&gt;Gatsby JS&lt;/a&gt; static site generator along with a headless CMS, like &lt;a href="https://cosmicjs.com/"&gt;Cosmic JS&lt;/a&gt;.  Today, with these two tools, we will create a working portfolio ready for continuous deployment, with the power to be rebuilt when content changes are made.&lt;/p&gt;

&lt;p&gt;TLDR:&lt;br&gt;
&lt;a href="https://cosmicjs.com/apps/gatsby-agency-portfolio"&gt;Gatsby Portfolio Site Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/cosmicjs/gatsby-agency-portfolio"&gt;Download the Codebase&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  0.0 Before We Start
&lt;/h2&gt;

&lt;p&gt;We are creating a portfolio site mostly with static data, but it would nice to be able to easily modify the content of our site without needing to modify a bunch of source code.  So we are building a client that consumes content stored on a Content Management Service and programmatically displays it at whatever URL we choose. &lt;/p&gt;
&lt;h3&gt;
  
  
  0.1 Tools We Are Using
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.gatsbyjs.org/"&gt;Gatsby JS&lt;/a&gt; - This is a static site generator that will automatically fetch new data and rebuild our site files when changes are made to our content.  Comes bundled with data fetching wizardry GraphQL and the ever present React JS front end framework.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.gatsbyjs.org/"&gt;Cosmic JS&lt;/a&gt; - Our Content Management Service that will store all the information we need about our site.  Cosmic JS offers very flexible data model definition that will allow us to store all types of information, from iterables to simple text fields and HTML content.   NOTE! - In order to follow along with this tutorial you will need to create a bucket on Cosmic JS and fill it with the appropriate data objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://rsuitejs.com/en"&gt;RSuite&lt;/a&gt; - A library of pre-styled components that works with react to give us pre-styled components.  This will allow us to use components that look great out of the box, while also giving us flexibility to make adjustments as needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.netlify.com/"&gt;Netlify&lt;/a&gt; (Optional) - A deployment service that will let us hook directly into a git repository.  Using this we can configure webooks for rebuilding static files as well as make automatic deploys when source code changes occur.&lt;br&gt;
Let's go ahead and begin configuring our setup.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  1.0 Installation and Setup
&lt;/h2&gt;

&lt;p&gt;We only have a few software requirements needed to start building. Mainly we need &lt;a href="https://nodejs.org/en/"&gt;Node JS&lt;/a&gt; either npm or yarn, and we will be using git to do some deployment things on Netlify if you so choose.&lt;/p&gt;
&lt;h3&gt;
  
  
  1.1 Initializing our Project
&lt;/h3&gt;

&lt;p&gt;Once you get those installed, we can begin setting up our development environment.  Gatsby uses a very handy dandy CLI to allow us to bootstrap our project with a project directory ready to build and serve from a Node environment.&lt;/p&gt;

&lt;p&gt;If you don't have the CLI you can install it with a simple npm terminal command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$npm&lt;/span&gt; &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; gatsby-cli
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will take a moment to install but after a few seconds you will have access to gatsby terminal command which we can use to initialize our project:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$gatsby&lt;/span&gt; new gatsby-portfolio
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we have a directory called gatsby-portfolio in the location you ran the gatsby command, change to that directory and list its contents:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$cd&lt;/span&gt; gatsby-portfolio/ &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-la&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You should see a list of folders and files similar to this:&lt;/p&gt;

&lt;p&gt;.&lt;br&gt;
├── node_modules&lt;br&gt;
├── src&lt;br&gt;
├── .gitignore&lt;br&gt;
├── .prettierrc&lt;br&gt;
├── gatsby-browser.js&lt;br&gt;
├── gatsby-config.js&lt;br&gt;
├── gatsby-node.js&lt;br&gt;
├── gatsby-ssr.js&lt;br&gt;
├── LICENSE&lt;br&gt;
├── package-lock.json&lt;br&gt;
├── package.json&lt;br&gt;
└── README.md&lt;/p&gt;

&lt;p&gt;open up package.json and we will see we have some terminal scripts that we can now use to build / serve our project. Try running the start script in your terminal:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$npm&lt;/span&gt; start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$yarn&lt;/span&gt; start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;After a few seconds we should see a success message in our terminal and we should be able to view our initial project view on our &lt;a href="http://localhost:8000"&gt;localhost&lt;/a&gt;, you should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ggUnGqFR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/kictrbj4n23fgkvdii1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ggUnGqFR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/kictrbj4n23fgkvdii1n.png" alt="starter-screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great, now press ctrl + C within your terminal to stop the development server and we are now ready install our node libraries.&lt;/p&gt;
&lt;h3&gt;
  
  
  1.2 Installing our Libraries
&lt;/h3&gt;

&lt;p&gt;We require a few plugins from Gatsby to allow our Graphql queries to run, as well as a few extras for environment configuration and our component library RSuite.  From within your project directory, we need to run some terminal commands to get our js libraries:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$yarn&lt;/span&gt; add rsuite dotenv gatsby-source-cosmicjs gatsby-plugin-sass gatsby-
plugin-less node-sass less
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Let's go through these and talk about what we are adding to our project here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;rsuite&lt;/strong&gt; - our component library that I mentioned above.  Installing this let's us import js classes and insert pre-styled React components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;dotenv&lt;/strong&gt; - Allows us to configure our source code with sensitive api keys, token, whatever that may change between developers but should be present when the source code is built.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;gatsby-source-cosmicjs&lt;/strong&gt; - a gatsby plugin that will let us easily make graphql requests to the Cosmic JS graphql API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;gatsby-plugin-sass / gatsby-plugin-less / node-sass / less&lt;/strong&gt; - Gatsby plugins and styling libraries that will let us use .scss and .less files.  These will allow us to import rsuite styling specs and bundle them properly on build.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  1.3 Configuring Gatsby
&lt;/h3&gt;

&lt;p&gt;In order for Gatsby to be able to build our html files, we need to fetch data from Cosmic JS and build each page using the data graphql retrieves.  Let's go ahead and open gatsby-config.js and add our installed packages:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;The first thing you you should notice is our requiring of  &lt;code&gt;dotenv&lt;/code&gt;.  This will make our environment variables accessible in this config file at run time and allow Gatsby to configure our components with our credentials needed to make API requests.&lt;/p&gt;

&lt;p&gt;We have added our plugins for sass and less, and also added our gatsby-source-cosmicjs plugin.  You'll notice that we are using some environment variables to configure this plugin so we need to add a hidden file that will store these variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$touch&lt;/span&gt; .env
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now add your variables to this file and dotenv will take care of defining these using line 1 of our gatsby-config.js file.  You can find the value to assign to these variables from within your Cosmic JS bucket: Bucket Name &amp;gt; Dashboard &amp;gt; Settings &amp;gt; Basic Settings:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Now we are ready to build!&lt;/p&gt;

&lt;h2&gt;
  
  
  2.0 Building Our Components
&lt;/h2&gt;

&lt;p&gt;So what are we building exactly.  Well, basically we will create one large Page component and create a series of display components to handle each section of our portfolio.  Let's break this down:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/pages/index.js&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is our home page and where all of our components will be loaded and used to display portfolio info.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;src/components/projects.js&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This will be our projects section that will take project data and display information about each project.  This will take a prop composed of our portfolio projects and iterate over them to display data when appropriate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;src/components/work.js&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This will a section highlighting our skills and the types of services / work we offer to people that might peruse our portfolios.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;src/components/about.js&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This will be a section that talks about us, displaying any personal data we want to share.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;src/components/contact.js&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finally we have a component that we will use for displaying a contact form that will let users email us if they have any inquires.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2.1 The Home Page
&lt;/h3&gt;

&lt;p&gt;This is our main component that act as our entry point for our portfolio.  Its job is to make several Graphql API requests for our portfolio data from Cosmic JS and pass that data onto the different sections of our portfolio.  Let's look at the component and talk about what's happening:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The only display element that really lives here is the splash screen which gives us a little bit copy that can be displayed about your company.   But the meat of our content will be pushed into each section component.&lt;/p&gt;

&lt;p&gt;Now let's update the header so that we can navigate around our home page.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.2 Modify the Header Component
&lt;/h3&gt;

&lt;p&gt;By default Gatsby gives us a &lt;strong&gt;Layout&lt;/strong&gt; component that lets us wrap each page with a &lt;strong&gt;Header&lt;/strong&gt; and &lt;strong&gt;Footer&lt;/strong&gt;.  We are going to add some navigation for our portfolio into the header so users can navigate to different sections of our Portfolio by clicking on a nav bar that we will import from &lt;strong&gt;rsuite&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;These links will scroll the home page down to each section via the name properties placed on each section on our home page.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.3 The Work Component
&lt;/h3&gt;

&lt;p&gt;This component takes in our data about any services we provide, specifically a names, a short summary, and a more in depth description, and let's us display that to our users:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This iterates through our services objects.  For every service that exists we add a new div to the work component section. Now we can modify and add data to our services on Cosmic JS and it will update accordingly.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.4 The Projects and About Components
&lt;/h3&gt;

&lt;p&gt;These sections will behave in essentially the same way, we display some information from our &lt;strong&gt;Home&lt;/strong&gt; page object from Cosmic JS. Just a bit of text to give the user some context for each section, but after that we just iterating through our list objects we have saved to our Cosmic JS bucket.&lt;/p&gt;

&lt;p&gt;Here’s our Projects component that will iterate through our projects and display images and text:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The about component will behave the same way.  It will iterate through a list of people and display some images and information stored in the services objects on Cosmic JS:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  2.5 The Contact Component
&lt;/h3&gt;

&lt;p&gt;Lastly we have a component that will users to contact us at an email we specify.  This will handle our contact form, and will be initialized with some state variables so we can control our user inputs, all the input and form components are handled by &lt;strong&gt;rsuite&lt;/strong&gt; so we don't have to add too many styling properties to our form fields and feedback components:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Essentially we validate our form fields by checking if all the form field contain a value, then we use a the mailto url to open an email client and populate it with our message.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.0 Deployment (optional)
&lt;/h2&gt;

&lt;p&gt;Now we are ready to deploy our app.  The most important part of this process is making sure that our app rebuilds itself when we change any data on Cosmic JS.  If we integrate continuous deployment using git Netlify will allow us to activate a buildhook in order to generate new static files using updated data fetched from the Cosmic JS API.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1 Deploying from Git
&lt;/h3&gt;

&lt;p&gt;Go to &lt;a href="https://www.netlify.com/"&gt;Netlify&lt;/a&gt; and create an account if you don't aready have one.  From the apps dashboard click New Site from Git at the app dahsboard.  From there you will be walked through the process of authorizing Netflify to access a repository from a git service (github, gitlab, bitbucket).&lt;/p&gt;

&lt;p&gt;You will have to add you &lt;strong&gt;COSMIC_READ_KEY&lt;/strong&gt; and your &lt;strong&gt;COSMIC_BUCKET_SLUG&lt;/strong&gt; as environment variables. This can be found under the deploy settings once the app has been created on Netlify.  In the same area you can create your buildhook to allow Cosmic JS to request a rebuild of your files once an update is made:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AM1FwXMj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/nswumacomhql7of2c5uh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AM1FwXMj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/nswumacomhql7of2c5uh.png" alt="buildhook config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the buildhook is created go over to Cosmic JS and add it to the webhooks tab under the settings for your bucket:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E2jPUwsE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hejzya7vpotz72os7irh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E2jPUwsE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hejzya7vpotz72os7irh.png" alt="webhooks settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow the instructions for adding a webhook that fires a post request to that url from Netlify every time an object is updated and you should have a continuously deployed, super fast portfolio site ready to be updated.&lt;/p&gt;

&lt;p&gt;Thanks for following along with me and I'll see you next time I decide to build something cool :) &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>gatsby</category>
      <category>cosmicjs</category>
    </item>
    <item>
      <title>Building a Progressive Blog App with Apollo and Cosmic JS</title>
      <dc:creator>Jacob Knaack</dc:creator>
      <pubDate>Mon, 22 Apr 2019 16:18:54 +0000</pubDate>
      <link>https://dev.to/jacobknaack/building-a-progressive-blog-app-with-apollo-and-cosmic-js-1g1g</link>
      <guid>https://dev.to/jacobknaack/building-a-progressive-blog-app-with-apollo-and-cosmic-js-1g1g</guid>
      <description>&lt;p&gt;Hello stranger.  Come with me on a short adventure as we build a fast, simple interface for publishing blog style articles. Something sensible.  It should look clean and simple yet have the ability to perform powerful resource fetching with some query magic. I'm talking about Graphql, the query language built with web APIs in mind. And speaking of web APIs,  we will also be using our handy Content Management Service: Cosmic JS, to Create and Store our Blog Data.&lt;/p&gt;

&lt;h2&gt;
  
  
  TLDR:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://cosmicjs.com/apps/apollo-blog"&gt;Progressive Apollo Blog&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/jacobknaack/apollo-blog"&gt;Progressive Apollo Blog Codebase&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  0.0 Before You Start
&lt;/h2&gt;

&lt;p&gt;Before you go any further make sure you have the required developer tools installed on your machine.  Mainly you will need &lt;a href="https://nodejs.org/"&gt;Node JS&lt;/a&gt;, its accompanying package manager: &lt;em&gt;npm&lt;/em&gt;, and &lt;a href="https://git-scm.com/"&gt;git&lt;/a&gt;.  Git is semi optional and will only be used to code storage and deployment if that is your goal.&lt;/p&gt;

&lt;p&gt;Once you have those installed we can begin setting up our project and eventually start writing some Javascript.&lt;/p&gt;
&lt;h3&gt;
  
  
  0.1 Libraries and packages
&lt;/h3&gt;

&lt;p&gt;Let's go over the main packages that we will use to create our blog platform.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://facebook.github.io/create-react-app/"&gt;Create React App&lt;/a&gt;&lt;/strong&gt; - We will be leveraging the ever prevalent React library to build our UI components.  To bootstrap our project we will also be using a handy command line tool create-reate-app so that we can spend as little time as possible on configuration and setup and just get to creating meaningful javascript.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://react.semantic-ui.com/"&gt;Semantic UI&lt;/a&gt;&lt;/strong&gt; - This is a UI framework that will allow us to import fully styled UI components.  No fussing around with tedious CSS specs to get something that looks nice right out of the gate. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.apollographql.com/"&gt;Apollo / Graphql&lt;/a&gt;&lt;/strong&gt; - These two packages go hand in hand.  Apollo will be used as the Graphql client to make requests to our Graphql server endpoints.   It's easy to configure and very straight forward to use. Again, allowing us to focus on writing less javascript and instead focusing on the high level composition of our application.  Graphql of course will allow us to make requests that return only data we care about, letting us efficiently grab content from Cosmic JS.&lt;br&gt;
I believe with all that boring business out of the way, we can start slapping our keyboards!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  1.0 Setting Up Our Project
&lt;/h2&gt;

&lt;p&gt;Let's fire up our terminal and start bootstrapping our project source files.  For those that don't have Create React App installed, we can do that with a simple npm command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$npm install -g create-react-app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Once this finishes (or you have this package already installed), we can summon our initial project files by running the cli:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$create-react-app apollo-blog
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You will notice a fair amount of wizardry occurring in your terminal as our app is bootstrapped with all sorts of powerful libraries and source code.  Taking a look at our file system will show a directory titled &lt;code&gt;apollo-blog&lt;/code&gt;.  Changing directories into this folder will show a structure similar to this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apollo-blog/
    README.md 
    node_modules/
    package.json
    public/
        index.html
        favicon.ico
    src/
        App.css
        App.js
        App.test.js
        index.css
        index.js
        logo.svg
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We should now be able to run:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$npm start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$yarn start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will run the start script stored in our &lt;strong&gt;package.json&lt;/strong&gt; file which is all bundled together with Webpack and build tools that will handle compiling all of our React source code and bundling that into files ready to be served to a browser.&lt;/p&gt;

&lt;p&gt;After the script runs you should see a simple message in your terminal, and your default browser should open to a tab with project running.  You should see something like this:&lt;/p&gt;
&lt;h3&gt;
  
  
  1.1 Installing Javascript Libraries
&lt;/h3&gt;

&lt;p&gt;The next step is installing some npm libraries so that will help quickly create a good looking App Interface as well as easily fetch data from the Cosmic JS graphql servers.&lt;/p&gt;

&lt;p&gt;In your terminal run the following command and let's talk about what each of these packages will be doing for us:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$npm install --save apollo-boost react-apollo graphql dotenv semantic-ui-css semantic-ui-react react-router-dom
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;apollo-boost / react-apollo / graphql&lt;/strong&gt; - These guys are going to serve as the main solution for getting our data efficiently and scalably from Cosmic JS servers to our client side UI.  They provide wrappers for our components to be able to configure queries  and provide useful variables for data loading and error handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;dotenv&lt;/strong&gt; - this package will allow is to configure semi sensitive keys so we don't have to store anything publicly for accessing our Cosmic JS Bucket.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;semantic-ui-css / semantic-ui-react&lt;/strong&gt; - Our UI framework that will let us import pre-styled containers and components letting us focus on programming functionality, spending less time fidgeting with CSS and other styling considerations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;react-router-dom&lt;/strong&gt; - A react library that will allow us to configure url routing logic to control linking within our app and accessing URL parameters.&lt;/p&gt;

&lt;p&gt;with these installed we can begin writing some javascript!&lt;/p&gt;
&lt;h3&gt;
  
  
  1.2 Configuring Environment Variables
&lt;/h3&gt;

&lt;p&gt;We are going to add a minimal security precaution so that we won't let just any request come through our bucket's API endpoint,  feel free to skip this section but be warned that your components will behave a little differently and your bucket can be queried from anyone with the knowhow to make API Requests.  So, you know... don't put any sensitive info in your buckets.&lt;/p&gt;

&lt;p&gt;We are going to require a &lt;strong&gt;read_key&lt;/strong&gt; to be attached to every API request to our bucket.  You'll have to generate one by heading to the settings tab for your Cosmic JS bucket and clicking on Basic Settings. &lt;strong&gt;Cosmic JS &amp;gt; Your Bucket &amp;gt; settings &amp;gt; main&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;At the top there should be as section titled &lt;strong&gt;API Access &amp;gt; API Read Access Key&lt;/strong&gt;.  Click on the button to &lt;strong&gt;Generate new key&lt;/strong&gt;.  Copy the key to your clipboard and let's configure our project directory with our config file:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$touch .env
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;this file is going to be used to configure our project with our API Read Access Key so we don't have to hard code it in with our source code and commit it for the world / other prying eyes to see.&lt;/p&gt;

&lt;p&gt;simply add one line to your .env file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Make sure you prefix your variable name with &lt;strong&gt;REACT_APP&lt;/strong&gt;!  The build scripts that bundle our source code will look for this to make variables available in our compiled javascript.  With that done, we are now ready to use this key in our React project.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.0 Creating our Routes and Components
&lt;/h2&gt;

&lt;p&gt;Finally, we are ready to create our Components and set up our routes.  In an effort to keep things as simple as possible we are just creating two routes and two main components:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Routes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{protocol}://{host}/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The home route that will list our articles.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{protocol}://{host}/article/:articleName
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The route that will display an article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Components&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/views/home.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This component will fetch all articles and display general article info.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/views/article.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The component that will fetch a single article and display all article content.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.1 Configuring  App.js
&lt;/h3&gt;

&lt;p&gt;Let's go ahead and open the &lt;strong&gt;App.js&lt;/strong&gt; component in whatever text editor you prefer, and start to configure it with our routing logic and our Graphql client. &lt;/p&gt;

&lt;p&gt;We are going to import one component wrapper from &lt;strong&gt;react-apollo&lt;/strong&gt; and one constructor function that will let the components within our component wrapper make requests to a specified Graphql server ( for us this will be the Cosmic JS's Graphql server).  &lt;/p&gt;

&lt;p&gt;We also want to import our wrapping components for &lt;strong&gt;react-router-dom&lt;/strong&gt;. This will allow us to us our browser url to systematically switch component views and access any url parameters.&lt;/p&gt;

&lt;p&gt;Lastly we are going to import a CSS file from &lt;strong&gt;Semantic UI&lt;/strong&gt; so that we can leverage it's class selectors and give imported components their base stylings.&lt;/p&gt;

&lt;p&gt;Here's what our &lt;strong&gt;App.js&lt;/strong&gt; should look like after everything is properly configured:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h3&gt;
  
  
  2.2 Creating The Home View
&lt;/h3&gt;

&lt;p&gt;Let's create a new file in a new directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/views/home.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This folder will store all of our view layer React Components.  Our Home View will just return some brief info about our site and a list of articles that have been published to our blog.&lt;/p&gt;

&lt;p&gt;The main parts of this component are a heading that display a title and a site description, and a list that will display Semantic UI components.&lt;/p&gt;

&lt;p&gt;We also will need to configure this view with some &lt;strong&gt;Graphql&lt;/strong&gt; logic so that it can make requests to Cosmic JS.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Now we have ye old React Component,  that will fetch articles from our content manager (Cosmic JS) before the component renders and pass those as an array on component props.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Apollo&lt;/strong&gt; also gives us some handy properties attached to its data object, specifically an &lt;strong&gt;error&lt;/strong&gt; object, and a &lt;strong&gt;loading&lt;/strong&gt; boolean, which we use to conditionally render components, such as loading message when the request is fetching, and an error message when our error object returns a message.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.3 Creating The Article View
&lt;/h3&gt;

&lt;p&gt;With our list of articles displaying on our home page, we need to Create the view that will display the blog content contained in each article object.&lt;/p&gt;

&lt;p&gt;Let's create another file in our &lt;strong&gt;views&lt;/strong&gt; directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/views/article.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Creating this component is similar to the Home View, but our query is going to include an article identifier from our URL stored on a prop: match which &lt;strong&gt;react-router-dom&lt;/strong&gt; creates for us when we render a component at a Route.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;This component is much simpler than our Home component, mostly due to the fact that we are just using the Query component wrapper instead of the turning our component into a higher level component using the &lt;strong&gt;Graphql&lt;/strong&gt; component.  This gives us direct access to our &lt;strong&gt;Graphql&lt;/strong&gt; directly in our component logic but &lt;em&gt;does not make it available on component props&lt;/em&gt;! So take note.&lt;/p&gt;

&lt;p&gt;For both components I have included some minimal styling objects in the source code, but feel free to use your creativity and change up the styling properties to create your own look and feel.&lt;/p&gt;

&lt;p&gt;And that's it!  We have built two relatively simple and scalable React components that are configured to two separate url routes. We can control our views easily with &lt;strong&gt;react-router-dom&lt;/strong&gt; and fetch our data sensibly with the power of &lt;strong&gt;Apollo / Graphql&lt;/strong&gt; and Cosmic JS.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.0 Deployment
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;create-react-app&lt;/strong&gt; comes bundled with some handy deployment scripts if you feel so inclined to make your application available to the world.  &lt;/p&gt;

&lt;h3&gt;
  
  
  3.1 Running the Build Script
&lt;/h3&gt;

&lt;p&gt;If you take a look at the &lt;strong&gt;package.json&lt;/strong&gt; file that we have a script called build.  Running this will compile all your project files and bundle them into a static build directory that is ready to deploy to any hosting service.&lt;/p&gt;

&lt;p&gt;You'll also notice after the script finishes that you have an option to publish directly to github pages using the serve module.  You just need to add a homepage value in &lt;strong&gt;package.json&lt;/strong&gt; and make sure the serve package is installed with either npm or yarn.&lt;/p&gt;

&lt;p&gt;Follow the link at the end of the build message to learn more about deployment using this method.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2 Deploying Using Netlify and Github
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.netlify.com/"&gt;Netlify&lt;/a&gt; provides app building and deployment services and they will give you a free ( if not a little bit verbose ) URL for you to deploy client side code.  The process is pretty easy and we can use a github repository to deploy our code continuously when we make changes to our source code.&lt;/p&gt;

&lt;p&gt;After you create and account / Log in to the Netlify dashboard, there should be a tab called &lt;strong&gt;sites&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Clicking the &lt;strong&gt;sites&lt;/strong&gt; tab will reveal a list of sites you have deployed on Netlify. From here you have two options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deploy using your static build directory created using the &lt;strong&gt;build&lt;/strong&gt; script.&lt;/li&gt;
&lt;li&gt;Connect your Netlify account to a git repository and use continuous deployment.  &lt;em&gt;This step requires that you commit your code to a git repository&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Deploying the static build directory is super simple but will require you to manually build and upload files to Netlify.  Simply drag the build directory directly over the area at the bottom of the sites tab from the Netlify Dashboard.  &lt;/p&gt;

&lt;p&gt;This will automatically deploy your site to a url that Netlify will generate.  Simple but requires work every time you update your source code.&lt;/p&gt;

&lt;p&gt;On the far right side there will be a button that reads &lt;strong&gt;New site from Git&lt;/strong&gt;. Selecting this option will walk you through the process of authorizing Netlify to access a code repository.  After these steps are finished Netlify will build your site files from a build script you specify and deploy it automatically each time you make a commit  to your repo!&lt;/p&gt;

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

&lt;p&gt;Well that's it for me today.  Happy hacking until we meet again :)&lt;/p&gt;

&lt;p&gt;If you have any comments or questions about building apps with Cosmic JS, &lt;a href="https://twitter.com/cosmic_js"&gt;reach out to us on Twitter&lt;/a&gt; and join the &lt;a href="https://cosmicjs.com/community"&gt;conversation on Slack&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>apollo</category>
      <category>react</category>
      <category>cosmicjs</category>
      <category>graphql</category>
    </item>
    <item>
      <title>How to Build a Documentation App With Gatsby and Cosmic JS</title>
      <dc:creator>Jacob Knaack</dc:creator>
      <pubDate>Sun, 17 Feb 2019 01:14:42 +0000</pubDate>
      <link>https://dev.to/jacobknaack/how-to-build-a-documentation-app-with-gatsby-and-cosmic-js-1p16</link>
      <guid>https://dev.to/jacobknaack/how-to-build-a-documentation-app-with-gatsby-and-cosmic-js-1p16</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ybbSeo_0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/8kcmm857hqe913u73edt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ybbSeo_0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/8kcmm857hqe913u73edt.png" alt="Gatsby-Docs-title-image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Documentation?... Documentation.  Let’s say for a second that you want to create a way to easily publish and read docs, err… documentation.  By the end of this read you will be able to do just that, all with the power of Gatsby (a static site generator) and Cosmic JS (an easy to setup and use Content Management System).  Grab some coffee, find a comfy chair and let’s build something cool.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TL:DR&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cosmicjs.com/apps/gatsby-docs"&gt;Gatsby Documentation App Demo&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/cosmicjs/gatsby-docs-app"&gt;Check out the codebase&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  1.0 - Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is &lt;a href="https://www.gatsbyjs.org/"&gt;Gatsby&lt;/a&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Gatsby is an easy to use framework for generating static web site files.  It comes bundled with all sorts of hotness, like React JS for building web components, and GraphQL for handling our component state without the need to configure something like Redux to handle external data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about &lt;a href="https://cosmicjs.com/"&gt;Cosmic JS&lt;/a&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cosmic JS will handle our publishing and data storage.  It’s easy to set up and easy to implement for apps like this, yet scalable enough to handle more complex projects across larger teams.  We will use this to create and store our documentation content.  This will us allowing us to focus on how our users interact with our app, and let Cosmic JS do all the heavy lifting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is that all?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Well no… we are going to convert our docs from markdown to html,  since that’s what web Browsers like.  To do this we are going to use a package called &lt;a href="http://showdownjs.com/"&gt;Showdown&lt;/a&gt;, that can handle parsing and converting markdown to and from HTML.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Any Requirements?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Oh yeah, you will need to have access to a terminal, a Cosmic JS account with a &lt;a href="https://cosmicjs.com/getting-started"&gt;bucket and a documentation object&lt;/a&gt;, and a recent(ish) version of &lt;a href="https://nodejs.org/en/"&gt;Node JS&lt;/a&gt; installed on your machine in order to install the necessary software to make this app work.  I’m going to be using yarn to run my build scripts but you can npm if you like.  Just remember to choose one(npm or yarn) and stick with it as things can get little hairy when it’s time to deploy.&lt;/p&gt;

&lt;p&gt;Let’s Build!!&lt;/p&gt;
&lt;h2&gt;
  
  
  1.1 - Setting up our Development Environment
&lt;/h2&gt;

&lt;p&gt;To get started we will want to install Gatsby and install our dependencies.  Easy peasy. Gatsby uses a handy command line interface (CLI) to build our initial project files.  First we will want to install the CLI by installing it globally with npm:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm install -g gatsby-cli&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives us access to the &lt;code&gt;gatsby&lt;/code&gt; command and allows us to initialize our project.  Run the following script to create a new directory filled with a project template:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ gatsby new gatsby-docs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Wait a second to allow the script to complete and you will notice a new directory created called &lt;code&gt;gatsby-docs&lt;/code&gt;.   Let’s see what’s inside by changing directories:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ cd gatsby-docs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;you should see a directory structure similar to this:&lt;/p&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── node_modules
├── src
├── .gitignore
├── gatsby-browser.js
├── gatsby-config.js
├── gatsby-node.js
├── gatsby-ssr.js
├── LICENSE
├── package-lock.json
├── package.json
└── README.md
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Much of this will look familiar if are used to creating Node applications but some of this will be a little new. You should be able to get a development server up and running by executing the start script:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ yarn start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After a second you should see a success prompt letting you know that everything has compiled properly and your app is live.&lt;/p&gt;

&lt;p&gt;Now you can open up your browser pointing to &lt;code&gt;localhost:8000&lt;/code&gt; and see the compiled output.  It should look something very similar to this:&lt;br&gt;
&lt;a href="https://camo.githubusercontent.com/ce605e30e35492dfafc3b651c1430596574b411b/68747470733a2f2f64726f7073696e6e2e73332e616d617a6f6e6177732e636f6d2f53637265656e25323053686f74253230323031372d30382d3236253230617425323031322e35372e3430253230504d2e706e67" class="article-body-image-wrapper"&gt;&lt;img src="https://camo.githubusercontent.com/ce605e30e35492dfafc3b651c1430596574b411b/68747470733a2f2f64726f7073696e6e2e73332e616d617a6f6e6177732e636f6d2f53637265656e25323053686f74253230323031372d30382d3236253230617425323031322e35372e3430253230504d2e706e67" alt="gatsby default screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congrats! You have set up a working Gatsby site. But before we dig into what is going on under the covers let’s install the rest of our dependencies that will power our app:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ yarn add cosmicjs showdown highlight.js dotenv node-sass gatsby-plugin-sass gatsby-source-graphql&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Whoa... That’s a lot of newly installed packages, but each of these are super useful I swear.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;cosmicjs&lt;/code&gt; will be used to add new content to our app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;showdown&lt;/code&gt; is the text parser I mentioned that will handle markdown and html conversion.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;highlight.js&lt;/code&gt; is going to handle our syntax highlighting inside of our converted markdown text.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;dotenv&lt;/code&gt; is an environment variable package that will make sure our sensitive tokens and/or runtime environment is configured from a &lt;code&gt;.env&lt;/code&gt; file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;node-sass&lt;/code&gt; and the &lt;code&gt;gatsby-plugin-sass&lt;/code&gt; packages will allow to use &lt;code&gt;.scss&lt;/code&gt; files to style our components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;gatsby-source-graphql&lt;/code&gt; will allow us to leverage GraphQL queries on external data (ie - use the Cosmic JS GraphQL api)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With all of that business out of the way we can look at our directory and configure our Gatsby source code to run properly.&lt;/p&gt;
&lt;h2&gt;
  
  
  2.0 - Configuring Gatsby
&lt;/h2&gt;

&lt;p&gt;Now we can dig into our directory and make sure Gatsby is configured properly for using the technologies that will scalably and sensibly power our app.&lt;/p&gt;

&lt;p&gt;The first file we will want to look into is &lt;code&gt;gatsby-config.js&lt;/code&gt;.  This file is used to configure high level plugins that allow any source code we write to be bundled properly when our static files are built.  It also contains a little bit of metadata that describes our site to users and can be queried in our React Components.&lt;/p&gt;

&lt;p&gt;Here we will add our newly installed plugins to the default configuration you see before you.  First we just need to add &lt;code&gt;gatsby-plugin-sass&lt;/code&gt; to the plugins list, allowing us to import sass files and leverage sass to write sensible styling specs for each component.&lt;/p&gt;

&lt;p&gt;Next up we will add an object to end of our plugins list for &lt;code&gt;gatsby-source-graphql&lt;/code&gt; that will configure our external GraphQL API endpoint to allow us to fetch data from Cosmic JS.  Here’s how things should look:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now we are set to make GraphQL queries to the Cosmic JS GraphQL API!  Next, Let’s talk for a second about Gatsby and how things are going to break down.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.1 Building our App with Gatsby
&lt;/h2&gt;

&lt;p&gt;I’ve mentioned that Gatsby is a static site generator, but what does that mean? Gatsby takes all the fancy code we create and produces static files that are pre-configured using the config files we specify. By doing so we get increased performance for sites that may have lots of images, data to be fetched, and other assets that can slow down web applications.&lt;/p&gt;

&lt;p&gt;Let’s now get some source code created. Our site is going to use just two ‘Pages’, one that will serve a home page to list the documentation we've created, and one for viewing a piece of documentation. But to fetch the content that we are going to display, we are going to use GraphQL, which we have recently configured. We will need to add some variables to our &lt;code&gt;gatsby-node.js&lt;/code&gt; file in order to allow our static files to have the necessary parameters to make API calls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a .env file and add your Cosmic JS environment variables&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In your Cosmic JS Bucket &amp;gt; Basic Settings menu you will see fields for a bucket-slug and read and write keys down at the bottom.  Copy all three of these things and add them to a .env file.&lt;/p&gt;

&lt;p&gt;At your project root, type into your terminal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ touch .env&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now Create three lines:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We will use these with our &lt;code&gt;dotenv&lt;/code&gt; package to allow our src files to access these variables when necessary.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open up &lt;code&gt;gatsby-node.js&lt;/code&gt; and add config variables to pages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We are now going to use Gatsby’s built in node API to give each page in our site access to the environment variable we just created.  First we will import the variables from our &lt;code&gt;.env&lt;/code&gt; file using &lt;code&gt;dotenv&lt;/code&gt;, then we will explicitly set each variable in our page’s context. Your file should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Creating our first page&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we are going to create our first page that will grab all of the documentation objects and display them at the root of our site,  on &lt;code&gt;index.js&lt;/code&gt;.  First let’s create our list by creating a folder in the components directory titled docs - &lt;code&gt;/src/components/docs/&lt;/code&gt; and in that folder we will create a file titled &lt;code&gt;index.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This will be our module that is first displayed when we render our page after fetching our docs.  Here is the source code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;What's going on here&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This page basically runs a big loop over our &lt;code&gt;docs&lt;/code&gt; and returns some fancy jsx.  We map through the docs array and produce a Link from Gatsby that contains the title, a date, and some content that uses a description for the piece of documentation that was published.&lt;/p&gt;

&lt;p&gt;Feel free to add any &lt;code&gt;.scss&lt;/code&gt; files to this directory as well to get a styling that works for you for the given class names.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update the 'home' page with our new components&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we can open up our home page file at &lt;code&gt;/pages/index.js&lt;/code&gt; and import the components we just created and add them to our returned jsx. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now any &lt;code&gt;docs&lt;/code&gt; created on Cosmic JS, will appear here on the home page!&lt;/p&gt;

&lt;p&gt;Notice the exported query at the bottom of the file.  It contains two string type variable that will be present because we set the context object in our gatsby-node configuration.&lt;/p&gt;

&lt;p&gt;With our newly created home page working, let’s create our doc view that will display content from the documentation that we post.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating our doc display page&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of adding a new file to the pages directory in Gatsby, we are going to create a &lt;code&gt;templates&lt;/code&gt; directory and make a template page that we can configure on build, so that each time a doc is created, a new page can be created when we fetch our Docs from Cosmic JS.&lt;/p&gt;

&lt;p&gt;Start by creating a &lt;code&gt;templates&lt;/code&gt; directory at your project root, and then creating a &lt;code&gt;docPage.js&lt;/code&gt; file within. &lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── _templates
|   ├── docPage.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now add the page template complete with exported query that will fetch a singular doc from Cosmic JS.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Nothing will happen with this template until we tell Gatsby that it needs to create a page using this template. We do this so that Gatsby has a chance to fetch our Documentation from Cosmic JS before it builds the page using the necessary parameters for each GraphQL query at the bottom of &lt;code&gt;docPage.js&lt;/code&gt;.  We are using static site files after all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update Gatsby Node to build template pages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s go ahead and add an export function to &lt;code&gt;gatsby-node.js&lt;/code&gt; so that we are building docPage template from our GraphQL data:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now when Gatsby creates its pages, ie - the index page will fetch our docs and create a page for each doc that is retrieved and attach all the necessary params to the page Content of each page.  This way our template component is rendered and our GraphQL query should succeed!&lt;/p&gt;

&lt;h2&gt;
  
  
  3.0 Deployment
&lt;/h2&gt;

&lt;p&gt;Lastly we can talk about deployment and about how static sites work.  Deploying this bad boy can be a little tricky since this site uses a static build that won’t have the necessary pages of newly created docs until the deployment service has a chance to rebuild.&lt;/p&gt;

&lt;p&gt;My recommendation is to use [netlify}(&lt;a href="https://www.netlify.com/"&gt;https://www.netlify.com/&lt;/a&gt;) and link your source from GitHub or wherever you store your code.  From there you can trigger buildhooks in order to rebuild your site whenever certain events happen.  Cosmic JS will allow a post request to be fired off at an endpoint when objects are created, deleted, edited, etc.  So you can easily link the two together to make some magic happen.  Keep in mind, if you want to allow users to create Docs from within your UI: you will need to fire off a POST request manually to activate a buildhook whenever a doc is successfully created.&lt;/p&gt;

&lt;p&gt;Anyway, that’s all for me folks! Happy hacking.&lt;/p&gt;

</description>
      <category>react</category>
      <category>gatsbyjs</category>
      <category>graphql</category>
      <category>cosmicjs</category>
    </item>
  </channel>
</rss>
