<?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: SATOSHI 💬</title>
    <description>The latest articles on Forem by SATOSHI 💬 (@theisomorphic).</description>
    <link>https://forem.com/theisomorphic</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%2F171747%2F81e95d30-2d51-44fa-b38c-8ddd23fb618f.jpg</url>
      <title>Forem: SATOSHI 💬</title>
      <link>https://forem.com/theisomorphic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/theisomorphic"/>
    <language>en</language>
    <item>
      <title>Familiarizing yourself with a new codebase - What worked for me</title>
      <dc:creator>SATOSHI 💬</dc:creator>
      <pubDate>Sun, 14 Jun 2020 01:24:31 +0000</pubDate>
      <link>https://forem.com/theisomorphic/familiarizing-yourself-with-a-new-codebase-what-worked-for-me-20ap</link>
      <guid>https://forem.com/theisomorphic/familiarizing-yourself-with-a-new-codebase-what-worked-for-me-20ap</guid>
      <description>&lt;p&gt;Starting a new dev role is always difficult - you'll have to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;learn new technologies and services and how they work together,&lt;/li&gt;
&lt;li&gt;dive into a new codebase and familiarize yourself with the teams/organizations coding conventions&lt;/li&gt;
&lt;li&gt;follow new development workflows and procedures&lt;/li&gt;
&lt;li&gt;work with new team mates&lt;/li&gt;
&lt;li&gt;set up your local development environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;etc. Although this was not my first role as a Front-end Developer, I was especially anxious because&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I've never worked in an organization with many teams dispersed within many departments (my first job consisted of me and one other developer.)&lt;/li&gt;
&lt;li&gt;I also felt (and still feel) like a Junior because I didn't receive much mentor-ship on my Front-end work in my previous role.&lt;/li&gt;
&lt;li&gt;I was thrown into a code base using Angular which I had no experience building with.&lt;/li&gt;
&lt;li&gt;I had to be on boarded remotely because of shelter in place order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I had to figure out a way to get the ball rolling so I could feel more comfortable with the new work and work environment. Here are a couple of tips that worked out for me:&lt;/p&gt;

&lt;h2&gt;
  
  
  What to do:
&lt;/h2&gt;

&lt;p&gt;If possible, make your first pull request within the first couple of days of starting. Find some small issue with the code and start working on getting your first PR merged into the code base. For me, I found a minor accessibility issue - the main navigation was missing focus states so that someone who is dependent on the keyboard to navigate the website couldn't tell which navigation item they were currently on. I had to explore the code base, find the main navigation component and its corresponding style sheet. I created a new branch, made the changes, made my first commit and finally made my first PR. I requested a PR review, got some good feedback, implemented more changes, got my PR approved, merged, and pushed to production! I introduced a couple lines of code into the code base and ended up learning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how to run the application locally&lt;/li&gt;
&lt;li&gt;commit message conventions&lt;/li&gt;
&lt;li&gt;the basic folder/file structure of an Angular app.&lt;/li&gt;
&lt;li&gt;how to use SCSS for styling&lt;/li&gt;
&lt;li&gt;
PR messages were formatted in a particular way.&lt;/li&gt;
&lt;li&gt;procedures for merging to the &lt;code&gt;production&lt;/code&gt; branch.&lt;/li&gt;
&lt;li&gt;how to test the changes on the staging site.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After going through this process, I was way more comfortable making subsequent changes to the codebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  What not to do:
&lt;/h2&gt;

&lt;p&gt;Before starting my new role, I had about a week between jobs so I decided that in the days before my start date, I would read through all of the documentation I could find in various repositories owned by my new team. Don't get me wrong, reading documentation is super important but this is a surefire way to overwhelm yourself before starting your new job. Instead, I recommend trying to get a high level of understanding of each repository you'll be working in -&lt;/p&gt;

&lt;p&gt;Try to get a good idea of the structure of the &lt;code&gt;README.md&lt;/code&gt; by reading the table of contents (if one is provided) otherwise, just go through the &lt;code&gt;README&lt;/code&gt; and read all of the section titles and subtitles. Don't worry too much about the granular details within each section. This is just a way for you to get an idea of what kind of information is available to you if you get stuck when coding or, setting up your development environment.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Side bar:&lt;/em&gt; Also, don't be me and start working before day one of your job. This is not expected of you and as I mentioned before, this will just overwhelm you.&lt;/p&gt;

&lt;p&gt;I hope these tips serve you well in your new role 😎.&lt;/p&gt;

</description>
      <category>career</category>
      <category>tips</category>
      <category>development</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Building a Mock Blue Bottle GraphQL API</title>
      <dc:creator>SATOSHI 💬</dc:creator>
      <pubDate>Sun, 23 Jun 2019 20:32:08 +0000</pubDate>
      <link>https://forem.com/theisomorphic/building-a-mock-blue-bottle-graphql-api-2mme</link>
      <guid>https://forem.com/theisomorphic/building-a-mock-blue-bottle-graphql-api-2mme</guid>
      <description>&lt;p&gt;&lt;em&gt;Cover Photo by Gerson Cifuentes on Unsplash&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  tl;dr
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;At Eve Porcello's and Alex Bank’s GraphQL Workshop, I built a mock Blue Bottle GraphQL API using &lt;code&gt;apollo-server&lt;/code&gt; and &lt;code&gt;faker&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;apollo-server&lt;/code&gt; allows you to add mock resolvers for all of your types in your GraphQL Schema.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;interface&lt;/code&gt;s in GraphQL help you make your GraphQL API more organized/scalable by creating a relationship between similar GraphQL types. &lt;/li&gt;
&lt;li&gt;You can play around with my final example in the GraphQL Playground here:
&lt;iframe src="https://codesandbox.io/embed/blue-bottle-blog-post-9xrk7"&gt;
&lt;/iframe&gt;
&lt;/li&gt;
&lt;li&gt;Check out my Github repository here:
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/iwakoscott" rel="noopener noreferrer"&gt;
        iwakoscott
      &lt;/a&gt; / &lt;a href="https://github.com/iwakoscott/mock-blue-bottle-graphql-api" rel="noopener noreferrer"&gt;
        mock-blue-bottle-graphql-api
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Building a Mock Blue Bottle GraphQL API&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/542304f58712c12d4b6857b2d80091bd55a42fc863a954b89c0c4e3012fe905f/68747470733a2f2f696d616765732e756e73706c6173682e636f6d2f70686f746f2d313439363337343230303539342d3231386439333032316338633f69786c69623d72622d312e322e3126697869643d65794a6863484266615751694f6a45794d446439266175746f3d666f726d6174266669743d63726f7026773d3135303026713d3830"&gt;&lt;img src="https://camo.githubusercontent.com/542304f58712c12d4b6857b2d80091bd55a42fc863a954b89c0c4e3012fe905f/68747470733a2f2f696d616765732e756e73706c6173682e636f6d2f70686f746f2d313439363337343230303539342d3231386439333032316338633f69786c69623d72622d312e322e3126697869643d65794a6863484266615751694f6a45794d446439266175746f3d666f726d6174266669743d63726f7026773d3135303026713d3830" alt="a handful of roasted coffee beans"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Table of Contents&lt;/h2&gt;

&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/iwakoscott/mock-blue-bottle-graphql-api/tree/START" rel="noopener noreferrer"&gt;Start Here&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/iwakoscott/mock-blue-bottle-graphql-api/tree/add-mock-resolvers" rel="noopener noreferrer"&gt;Adding mock resolvers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/iwakoscott/mock-blue-bottle-graphql-api/tree/fix-warnings" rel="noopener noreferrer"&gt;Fixing the __resolveType warning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/iwakoscott/mock-blue-bottle-graphql-api/tree/master" rel="noopener noreferrer"&gt;Finished Version&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting Started&lt;/h2&gt;

&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;To get started clone this repository by running:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;git clone https://github.com/iwakoscott/mock-blue-bottle-graphql-api.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;in your terminal.&lt;/p&gt;
&lt;ol start="2"&gt;
&lt;li&gt;&lt;code&gt;cd mock-blue-bottle-graphql-api&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;run &lt;code&gt;npm install&lt;/code&gt; to download dependencies.&lt;/li&gt;
&lt;li&gt;run &lt;code&gt;npm run start&lt;/code&gt; to start the server on &lt;code&gt;http://localhost:4001&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;

  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/iwakoscott/mock-blue-bottle-graphql-api" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;I recently had the opportunity to attend a GraphQL Workshop by &lt;a href="https://twitter.com/eveporcello" rel="noopener noreferrer"&gt;Eve Porcello&lt;/a&gt; and  &lt;a href="https://twitter.com/MoonTahoe" rel="noopener noreferrer"&gt;Alex Banks&lt;/a&gt;. In one of the labs, we were tasked with creating our own GraphQL Schema to mock one of our favorite web applications. (If you aren’t familiar with a GraphQL Schema, a schema defines all of the possible operations (mutations and queries,) and types from your GraphQL endpoint. It is pretty much the blueprint for your GraphQL API.) I decided to try to mock one of my favorite e-commerce websites: &lt;a href="//bluebottlecoffee.com"&gt;bluebottlecoffee.com&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: I don’t work for Blue Bottle. I just like their website (and some of their coffees.)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My Process 💭
&lt;/h2&gt;

&lt;p&gt;I started by browsing the &lt;a href="https://bluebottlecoffee.com/store/coffee" rel="noopener noreferrer"&gt;coffee&lt;/a&gt;  route and got to work:&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%2Ffnsqpcmcknkyl4qkjs0c.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%2Ffnsqpcmcknkyl4qkjs0c.png" alt="blend coffee section on bluebottlecoffee.com"&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1116u0gbihcy84mi7jsi.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%2F1116u0gbihcy84mi7jsi.png" alt="single origin section on bluebottlecoffee.com"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I immediately noticed they divided the coffees into two sections:  Blends and Single Origin coffees. (For any non-coffee connoisseurs,  a blend consists of coffees from multiple countries, whereas a single origin coffee comes from a single farm, multiple farms in a similar area, or, from multiple farms from the same country.)  This sounds like a great place to use an &lt;code&gt;enum&lt;/code&gt; type.  An &lt;code&gt;enum&lt;/code&gt;  or “enumeration” type is a collection of named values (used in many different programming languages.)  This is how you define an &lt;code&gt;enum&lt;/code&gt; type in GraphQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SourcingType&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;BLEND&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;SINGLE_ORIGIN&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s include the &lt;code&gt;SourcingType&lt;/code&gt; within our &lt;code&gt;Coffee&lt;/code&gt; type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Coffee&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;sourcingType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SourcingType&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I’ve also added a unique &lt;code&gt;id&lt;/code&gt; to each coffee. (The &lt;code&gt;!&lt;/code&gt; at the end of &lt;code&gt;ID&lt;/code&gt; and &lt;code&gt;SourcingType&lt;/code&gt; defines a non-nullable field.) Great, let’s add more properties to a &lt;code&gt;Coffee&lt;/code&gt; type. Both Single Origin and Blend coffee share common properties that I’ve marked out in the diagrams below:&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%2Feorovpuyuc6r6c30xgv5.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%2Feorovpuyuc6r6c30xgv5.png" alt="a card for a coffee blend."&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ftb34cbw2ebolbxfres71.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%2Ftb34cbw2ebolbxfres71.png" alt="a card for a single origin coffee."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A. An image URL&lt;br&gt;
B. A sourcing type (Single Origin or a Blend)&lt;br&gt;
C. The name of the coffee&lt;br&gt;
D. Flavor Profiles / Notes&lt;br&gt;
E. Unit Price&lt;br&gt;
F. Unit Weight&lt;/p&gt;

&lt;p&gt;Let’s add these to our schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;   type Coffee {
     id: ID!
     sourcingType: SourcingType!
&lt;span class="gi"&gt;+    name: String!
+    flavorNotes: [String!]!
+    unitPrice: Float!
+    unitWeight: Float!
+    imageURL: String!
&lt;/span&gt;   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt; and &lt;code&gt;Float&lt;/code&gt; are primitive scalars in GraphQL.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[String!]!&lt;/code&gt; looks a little spooky 👻 at first, but all this means is that the &lt;code&gt;flavorNotes&lt;/code&gt; property expects a non-nullable array of non-nullable strings. We expect at a minimum, an empty array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our schema looks great but, let’s make it more Robusta (this is not a typo, this is a terrible coffee dad joke.)  We will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;define a &lt;code&gt;Coffee&lt;/code&gt; interface and create a &lt;code&gt;SingleOrigin&lt;/code&gt; and &lt;code&gt;Blend&lt;/code&gt; type and&lt;/li&gt;
&lt;li&gt;add queries to our Schema.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;Coffee&lt;/code&gt; Interface ☕️
&lt;/h2&gt;

&lt;p&gt;Recall - coffees come in two types: Blends and Single Origin coffees. They share exactly the same properties but, what if we wanted to include information about the country of origin for each coffee? A blend will include many different countries, whereas, a single origin coffee will only have one. It would be nice to be able to abstract the &lt;code&gt;Coffee&lt;/code&gt; type and create two new types - &lt;code&gt;SingleOrigin&lt;/code&gt; and &lt;code&gt;Blend&lt;/code&gt;  that use the &lt;code&gt;Coffee&lt;/code&gt; type as a base.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; A &lt;code&gt;SingleOrigin&lt;/code&gt; type will have a property called &lt;code&gt;country&lt;/code&gt; while, &lt;/li&gt;
&lt;li&gt;the &lt;code&gt;Blend&lt;/code&gt; type will include a property - &lt;code&gt;countries&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;countries&lt;/code&gt; and &lt;code&gt;country&lt;/code&gt;  will return type &lt;code&gt;Country&lt;/code&gt; (which we will construct later.)  The only difference is that the &lt;code&gt;countries&lt;/code&gt; property on a &lt;code&gt;Blend&lt;/code&gt; will be &lt;code&gt;[Country!]!&lt;/code&gt;  (an array of countries) and the &lt;code&gt;country&lt;/code&gt; property on a &lt;code&gt;SingleOrigin&lt;/code&gt; will be &lt;code&gt;Country!&lt;/code&gt;  (one country.) &lt;/p&gt;

&lt;p&gt;This all sounds great, but, how do we implement this into our schema? In GraphQL, there is a special &lt;code&gt;interface&lt;/code&gt; type that defines a base data type to be used to implement other similar types. Let’s use this to create a &lt;code&gt;Coffee&lt;/code&gt;  base type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-  enum SourcingType {
-    BLEND
-    SINGLE_ORIGIN
-  }
-
-  type Coffee {
&lt;/span&gt;&lt;span class="gi"&gt;+  interface Coffee {
&lt;/span&gt;     id: ID!
&lt;span class="gd"&gt;-    sourcingType: SourcingType!
&lt;/span&gt;     name: String!
     flavorNotes: [String!]!
     unitPrice: Float!
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the &lt;code&gt;interface&lt;/code&gt; includes all properties from the previous &lt;code&gt;Coffee&lt;/code&gt; type however, the &lt;code&gt;sourcingType&lt;/code&gt; is removed. We no longer need this property since we are going to organize our coffees into &lt;code&gt;Blend&lt;/code&gt; and &lt;code&gt;SingleOrigin&lt;/code&gt; types so, this property can be inferred. To create &lt;code&gt;Blend&lt;/code&gt; and &lt;code&gt;SingleOrigin&lt;/code&gt; we will need to use a special keyword: &lt;code&gt;implements&lt;/code&gt; to define a relationship between our new types and the base &lt;code&gt;Coffee&lt;/code&gt; interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Blend&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;implements&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Coffee&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="c"&gt;# ...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SingleOrigin&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;implements&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Coffee&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="c"&gt;# ...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s give our new types some properties!  You might be thinking it would suffice to do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Country&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Blend&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;implements&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Coffee&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;countries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Country&lt;/span&gt;&lt;span class="p"&gt;!]!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SingleOrigin&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;implements&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Coffee&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Country&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what I initially thought as well. Since we are explicitly defining a relationship between our new types and the base &lt;code&gt;Coffee&lt;/code&gt;  interface using the &lt;code&gt;implements&lt;/code&gt; keyword, you might think it is inferred that these new types will inherit everything from the &lt;code&gt;Coffee&lt;/code&gt; interface. Our Schema, however, needs to be as explicit as possible. This means that we must redefine all properties from the &lt;code&gt;Coffee&lt;/code&gt; interface to our respective types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;  type Country {
    id: ID!
    name: String!
  }
&lt;span class="err"&gt;
&lt;/span&gt;   type Blend implements Coffee {
&lt;span class="gi"&gt;+    id: ID!
+    name: String!
+    flavorNotes: [String!]!
+    unitPrice: Float!
+    unitWeight: Float!
+    imageURL: String!
&lt;/span&gt;     countries: [Country!]!
   }
&lt;span class="err"&gt;
&lt;/span&gt;   type SingleOrigin implements Coffee {
&lt;span class="gi"&gt;+    id: ID!
+    name: String!
+    flavorNotes: [String!]!
+    unitPrice: Float!
+    unitWeight: Float!
+    imageURL: String!
&lt;/span&gt;     country: Country!
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding Queries for Coffee! 🌱
&lt;/h2&gt;

&lt;p&gt;Our schema is looking great however, we have not defined any queries in our schema. To do so we will define a &lt;code&gt;type Query&lt;/code&gt; which will hold all queries from our GraphQL API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;allCoffee&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Coffee&lt;/span&gt;&lt;span class="p"&gt;!]!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;allSingleOrigin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SingleOrigin&lt;/span&gt;&lt;span class="p"&gt;!]!&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;allBlends&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Blend&lt;/span&gt;&lt;span class="p"&gt;!]!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’ve defined three different queries: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;allCoffee&lt;/code&gt; will return all coffees in our database: &lt;code&gt;Blend&lt;/code&gt; and &lt;code&gt;SingleOrigin&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;allSingleOrigin&lt;/code&gt; will return all &lt;code&gt;SingleOrigin&lt;/code&gt; coffees, and&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;allBlends&lt;/code&gt; will return all &lt;code&gt;Blend&lt;/code&gt; coffees.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Adding our Schema to &lt;code&gt;apollo-server&lt;/code&gt;  🚀
&lt;/h2&gt;

&lt;p&gt;Let’s start by cloning the repository I’ve created for you by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/iwakoscott/mock-blue-bottle-graphql-api.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After cloning my repository, run  &lt;code&gt;cd mock-blue-bottle-graphql-api&lt;/code&gt; and run &lt;code&gt;npm install&lt;/code&gt; to download dependencies. Let’s start coding! We will begin by requiring &lt;code&gt;apollo-server&lt;/code&gt; by writing:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ApolloServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;apollo-server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the same file, let’s create another variable - &lt;code&gt;typeDefs&lt;/code&gt; that will hold on to our Schema we’ve been building as a string template.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;// ...
&lt;span class="gi"&gt;+const typeDefs = `
+  interface Coffee {
+    id: ID!
+    name: String!
+    flavorNotes: [String!]!
+    unitPrice: Float!
+    unitWeight: Float!
+    imageURL: String!
+  }
+
+  type Country {
+    id: ID!
+    name: String!
+  }
+
+  type Blend implements Coffee {
+    id: ID!
+    name: String!
+    flavorNotes: [String!]!
+    unitPrice: Float!
+    unitWeight: Float!
+    imageURL: String!
+    countries: [Country!]!
+  }
+
+  type SingleOrigin implements Coffee {
+    id: ID!
+    name: String!
+    flavorNotes: [String!]!
+    unitPrice: Float!
+    unitWeight: Float!
+    imageURL: String!
+    country: Country!
+  }
+
+  type Query {
+    allCoffee: [Coffee!]!
+    allSingleOrigin: [SingleOrigin!]!
+    allBlends: [Blend!]!
+  }
+`;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s now create a new instance of &lt;code&gt;ApolloServer&lt;/code&gt;  and pass in our schema.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;// ...
&lt;span class="gi"&gt;+  const server = new ApolloServer({
+    typeDefs,
+  });
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now have an &lt;code&gt;ApolloServer&lt;/code&gt; instance with our schema. You might be wondering: “This is great and all but, how are we resolving the queries specified in our schema?” We don’t have any  &lt;a href="https://graphql.org/learn/execution/#root-fields-resolvers" rel="noopener noreferrer"&gt;resolver functions&lt;/a&gt; let alone a database or any micro-services to get data from. We definitely can’t get real data but, we can get close!  I want to show you the power of mocked resolvers in  &lt;code&gt;apollo-server&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Building Mock Resolvers  🦜
&lt;/h2&gt;

&lt;p&gt;Let’s begin by installing my favorite package for creating fake data:  &lt;a href="https://www.npmjs.com/package/faker" rel="noopener noreferrer"&gt;faker&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; faker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’ll create an object literal and save it in a new variable called &lt;code&gt;mocks&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gi"&gt;+  const mocks = {
+
+  };
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now what we want to do is mock out &lt;em&gt;all&lt;/em&gt; custom types we’ve defined in our schema:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Blend&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SingleOrigin&lt;/code&gt;, and&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Country&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will demonstrate how to mock out a &lt;code&gt;Blend&lt;/code&gt; type (&lt;em&gt;I leave&lt;/em&gt; &lt;code&gt;Country&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; &lt;code&gt;SingleOrigin&lt;/code&gt; &lt;em&gt;as an exercise for the curious reader. Check out my  &lt;a href="https://github.com/iwakoscott/mock-blue-bottle-graphql-api/tree/master" rel="noopener noreferrer"&gt;Github repository&lt;/a&gt; for my implementation.&lt;/em&gt; )&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gi"&gt;+  const faker = require('faker');
+
+  function getRandomPrice() {
+    const randomPrice = Number(faker.finance.amount(12, 22, 2));
+    return Math.round(randomPrice * 10) / 10;
+  }
+
&lt;/span&gt;&lt;span class="p"&gt;const mocks = {
&lt;/span&gt;&lt;span class="gi"&gt;+  Blend: () =&amp;gt; ({
+    id: faker.random.number(),
+    name: faker.commerce.productName(),
+    flavorNotes: [
+      faker.commerce.productAdjective(),
+      faker.commerce.productAdjective()
+    ],
+    unitPrice: getRandomPrice(),
+    unitWeight: faker.random.number({ min: 8, max: 12 }),
+    imageURL: faker.image.imageUrl()
+  })
&lt;/span&gt;};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;“What about the &lt;code&gt;Coffee&lt;/code&gt; interface?” The interface is no longer is treated as a &lt;code&gt;type&lt;/code&gt;, instead &lt;code&gt;Coffee&lt;/code&gt; defines a relationship between all types that &lt;code&gt;implements&lt;/code&gt; this interface.&lt;/li&gt;
&lt;li&gt;How about the &lt;code&gt;Query&lt;/code&gt; type?  The &lt;code&gt;Query&lt;/code&gt; type doesn’t need to be mocked since all queries: &lt;code&gt;allCoffee&lt;/code&gt;, &lt;code&gt;allSingleOrigin&lt;/code&gt;, and &lt;code&gt;allBlends&lt;/code&gt; will resolve with our custom &lt;code&gt;types&lt;/code&gt; that we’ve already mocked out.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Starting our &lt;code&gt;apollo-server&lt;/code&gt;  ▶️
&lt;/h3&gt;

&lt;p&gt;After mocking out the rest of the custom types, we are now ready to see all of our hard work come to life. Let’s add our &lt;code&gt;mocks&lt;/code&gt; to our server instance, start our server, and listen to requests for coffee ☕️ :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; const server = new ApolloServer({
   typeDefs,
&lt;span class="gi"&gt;+  mocks
&lt;/span&gt; });
&lt;span class="gi"&gt;+
+  server
+    .listen(4001)
+    .then(({ url }) =&amp;gt;
+      console.log(`Listening for coffee orders on ${url}... ☕️`);
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;npm run start&lt;/code&gt; in your terminal and wait until you see &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Listening for coffee orders on &lt;a href="http://localhost:4001/" rel="noopener noreferrer"&gt;http://localhost:4001/&lt;/a&gt;... ☕️”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now open up your favorite browser and navigate to: &lt;a href="http://localhost:4001/" rel="noopener noreferrer"&gt;http://localhost:4001/&lt;/a&gt;&lt;br&gt;
You should see the GraphQL Playground.&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%2F4ffemr485r4yyqk1he4u.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%2F4ffemr485r4yyqk1he4u.png" alt="GraphQL Playground graphical user interface."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is where we will &lt;del&gt;order&lt;/del&gt; query our coffee. The left panel is where we will write our queries, and our response will appear on the right panel after clicking the play button. On the very right-hand side of the browser, you might have noticed the two tabs - DOCS and SCHEMA. SCHEMA will show you your raw schema you’ve defined and passed to the Apollo server instance, and the DOCS will present your schema in a nice, human-readable way. &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%2F0l6tp1mua022uk4cixl4.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%2F0l6tp1mua022uk4cixl4.png" alt="GraphQL Playground DOCS tab."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s write our first query! In the left panel type in the following query and click the “play” button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;allSingleOrigin&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;flavorNotes&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;unitPrice&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;unitWeight&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;imageURL&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything went accordingly, you should see a response in the right panel:&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%2Fjpltklkeczdgduykk3zp.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%2Fjpltklkeczdgduykk3zp.png" alt="our first GraphQL query in the playground."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s now try querying for &lt;code&gt;allCoffee&lt;/code&gt;. This is where things get a little bit more interesting.  Recall that &lt;code&gt;allCoffee&lt;/code&gt; query will return both &lt;code&gt;SingleOrigin&lt;/code&gt; and &lt;code&gt;Blend&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How would you write a query for &lt;code&gt;allCoffee&lt;/code&gt;  and include properties that are exclusive to each type of coffee? In our example, &lt;code&gt;SingleOrigin&lt;/code&gt; has a property &lt;code&gt;country&lt;/code&gt; while a &lt;code&gt;Blend&lt;/code&gt; has property &lt;code&gt;countries&lt;/code&gt;.  Unfortunately, you cannot just include both properties in the query, otherwise, you will get an error:&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%2Fbygfq9a3ko5cicc9sa52.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%2Fbygfq9a3ko5cicc9sa52.png" alt="GraphQL Error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The error gives us a helpful hint, however:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Did you mean to use an inline fragment on "SingleOrigin"?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An  &lt;a href="https://graphql.org/learn/queries/#inline-fragments" rel="noopener noreferrer"&gt;inline fragment&lt;/a&gt;  takes on an interesting syntax that looks similar to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax" rel="noopener noreferrer"&gt;ES6 Spread Operator&lt;/a&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;allCoffee&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;flavorNotes&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;unitPrice&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;unitWeight&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;imageURL&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;on&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SingleOrigin&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;on&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Blend&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;countries&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the &lt;code&gt;...&lt;/code&gt; and we use the &lt;code&gt;on&lt;/code&gt; keyword to specify which type the fragment is coming from. We then provide the properties we want from the specific type. Go ahead and run the updated query in the playground!  Since the data is randomly generated, you might get a response back with only  &lt;code&gt;Blend&lt;/code&gt;,  only  &lt;code&gt;SingleOrigin&lt;/code&gt; or a mix of both!  Super cool! 😎&lt;/p&gt;

&lt;h3&gt;
  
  
  One Last Detail
&lt;/h3&gt;

&lt;p&gt;You might have noticed a warning in the console:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Type "Coffee" is missing a "__resolveType" resolver. Pass&lt;br&gt;
 false into "resolverValidationOptions.requireResolversFo&lt;br&gt;
rResolveType" to disable this warning.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Whenever you are working with an interface, our resolvers need a little bit of assistance identifying which type is what (I don’t know about you but, even a connoisseur like me can’t taste the difference between a blend and a single origin coffee.) We only included mock resolvers,  so, to fix this error we need to create a new object literal called &lt;code&gt;resolvers&lt;/code&gt;  and add a property &lt;code&gt;Coffee&lt;/code&gt; on to it, along with this &lt;code&gt;__resolveType&lt;/code&gt;  function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gi"&gt;+  const resolvers = {
+    Coffee: {
+      __resolveType: coffee =&amp;gt; (coffee.countries ? 'Blend' : 'SingleOrigin')
+    }
+  }
+
&lt;/span&gt; const server = new ApolloServer({
   typeDefs,
   mocks,
&lt;span class="gi"&gt;+  resolvers
&lt;/span&gt; });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first argument, &lt;code&gt;coffee&lt;/code&gt; of &lt;code&gt;__resolveType&lt;/code&gt; refers to the coffee currently being resolved.  If the &lt;code&gt;coffee&lt;/code&gt; has property &lt;code&gt;countries&lt;/code&gt; we can assume it is a &lt;code&gt;Blend&lt;/code&gt; otherwise, it is a &lt;code&gt;SingleOrigin&lt;/code&gt;. Now if you re-run the server, the warning should be gone! 🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Words
&lt;/h2&gt;

&lt;p&gt;Getting a GraphQL server up and running is so simple with &lt;code&gt;apollo-server&lt;/code&gt;. You don’t even need to have to write resolvers, let alone have a database or micro-services to get data from. You can define a schema and mock all of your data until you are ready to connect to an actual source. Lastly, I want to make a special shout out to  &lt;a href="https://twitter.com/eveporcello" rel="noopener noreferrer"&gt;Eve Porcello&lt;/a&gt; and  &lt;a href="https://twitter.com/MoonTahoe" rel="noopener noreferrer"&gt;Alex Banks&lt;/a&gt; for giving me an opportunity to attend one of their  &lt;a href="https://www.graphqlworkshop.com/" rel="noopener noreferrer"&gt;GraphQL Workshops&lt;/a&gt;.  Keep an eye out for any workshops in your area. I highly recommend it! Also, check out their  &lt;a href="http://shop.oreilly.com/product/0636920137269.do" rel="noopener noreferrer"&gt;Learning GraphQL&lt;/a&gt; O’Reily book - a great place to start to learn the fundamentals of building Full Stack GraphQL Applications. ✌️&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>showdev</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Solving for the nth Fibonacci number</title>
      <dc:creator>SATOSHI 💬</dc:creator>
      <pubDate>Mon, 03 Jun 2019 18:53:43 +0000</pubDate>
      <link>https://forem.com/theisomorphic/solving-for-the-nth-fibonacci-number-2pda</link>
      <guid>https://forem.com/theisomorphic/solving-for-the-nth-fibonacci-number-2pda</guid>
      <description>&lt;p&gt;&lt;em&gt;Cover Photo Credit: Chloe Evans on Unsplash&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A classic interview question: “Please write a function &lt;code&gt;fibonacci&lt;/code&gt; that takes an integer &lt;code&gt;n&lt;/code&gt; and returns the nth Fibonacci number.” The Fibonacci sequence follows the following pattern:&lt;/p&gt;

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

0, 1, 1, 2, 3, 5, 8, 13…


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

&lt;/div&gt;

&lt;p&gt;The pattern continues by adding the previous two Fibonacci numbers together and therefore, the next value above would be &lt;code&gt;21&lt;/code&gt;. Now let’s write a function to get the &lt;code&gt;n&lt;/code&gt;th Fibonacci value so that,&lt;/p&gt;

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

// base Fibonacci numbers
fibonacci(0) // returns 0
fibonacci(1) // returns 1
// generated Fibonacci numbers
fibonacci(2) // returns 1
fibonacci(3) // returns 2
fibonacci(4) // returns 3
fibonacci(5) // returns 5
fibonacci(6) // returns 8
// ...


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Solution 1: Recursion
&lt;/h2&gt;

&lt;p&gt;This is the most popular way of solving this problem because it is easier to reason about since,&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2)


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

&lt;/div&gt;

&lt;p&gt;Let’s write this as a function:&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;function&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&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;This is great but, this has no stopping condition so it will go on forever. We need to specify that if &lt;code&gt;n&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt; (our base Fibonacci numbers) we return &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;, respectively.&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;function&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&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;Great! Try the function out for &lt;code&gt;n = 1&lt;/code&gt;, &lt;code&gt;n = 5&lt;/code&gt;, and &lt;code&gt;n = 50&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fibonacci(1)&lt;/code&gt; should return &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fibonacci(5)&lt;/code&gt; should return &lt;code&gt;5&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fibonacci(50)&lt;/code&gt; should return &lt;code&gt;12586269025&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You might have noticed that &lt;code&gt;fibonacci(50)&lt;/code&gt; hangs in the console for some time. In fact, it took my console around eight minutes to execute!&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%2Fojp3sn4f18lvfzhe8g52.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%2Fojp3sn4f18lvfzhe8g52.png" alt="console showing an 8 minute execution of f(50)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the downside to this solution. For large &lt;code&gt;n&lt;/code&gt; , the computation time takes way too long. The second solution fixes this problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 2: Using a Generator Function
&lt;/h2&gt;

&lt;p&gt;So the previous solution worked but, is super slow for large values of &lt;code&gt;n&lt;/code&gt;.&lt;br&gt;
Why is this the case? Well, let’s calculate &lt;code&gt;fibonacci(10)&lt;/code&gt; as an example by hand (I will denote &lt;code&gt;fibonacci&lt;/code&gt; as &lt;code&gt;f&lt;/code&gt; for sake of simplicity.)&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%2Fdshn8ptqif800qj6vw8e.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%2Fdshn8ptqif800qj6vw8e.png" alt="long recursive tree for f(10)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are having to dive into a bunch of the same rabbit holes over and over again to calculate &lt;code&gt;fibonacci(10)&lt;/code&gt;. Why do we have to do this if all we need is the previous two Fibonacci numbers? Is there a way we can just &lt;em&gt;remember&lt;/em&gt; the previous two Fibonacci numbers and &lt;em&gt;generate&lt;/em&gt; the next Fibonacci number in the sequence? Yes! We can use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator" rel="noopener noreferrer"&gt;generators&lt;/a&gt; to create an infinite sequence of &lt;code&gt;Fibonacci&lt;/code&gt; numbers. Generators are interesting. They are like regular functions but with super powers. They are able to return values without completely ending the execution of a function. It does this by making use of the special &lt;code&gt;yield&lt;/code&gt; statement. Let’s look at a trivial example of a generator function.&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;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;x&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// the "*" denotes that function x is a generator&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;One taught me love&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;One taught me patience&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;And one taught me pain&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Great! Let’s invoke this function to see how it works:&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;thanku&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;x&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// we invoke the generator&lt;/span&gt;

&lt;span class="c1"&gt;// invoke the `next` method on the generator prototype&lt;/span&gt;
&lt;span class="nx"&gt;thanku&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// returns {value: "One taught me love", done: false}&lt;/span&gt;
&lt;span class="nx"&gt;thanku&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// {value: "One taught me patience", done: false}&lt;/span&gt;
&lt;span class="nx"&gt;thanku&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// {value: "And one taught me pain", done: false}&lt;/span&gt;
&lt;span class="nx"&gt;thanku&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// {value: undefined, done: true}&lt;/span&gt;

&lt;span class="c1"&gt;// Now aren't you grateful for your x?&lt;/span&gt;


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

&lt;/div&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.giphy.com%2Fmedia%2F1gUWdf8Z8HCxpM8cUR%2Fgiphy.webp" 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.giphy.com%2Fmedia%2F1gUWdf8Z8HCxpM8cUR%2Fgiphy.webp" alt="Ariana Grande Wink - Thank u next."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For every call to the &lt;code&gt;next&lt;/code&gt; method on the generator prototype, we get an object with two properties: &lt;code&gt;value&lt;/code&gt; and &lt;code&gt;done&lt;/code&gt; which is the value you are yielding from the generator and whether or not your generator is done generating values, respectively. Let’s look at a more interesting example. Let’s generate an infinite sequence of even numbers:&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;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;even&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="c1"&gt;// yield 0 as our first even number&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// the start of our infinite sequence!&lt;/span&gt;
    &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="c1"&gt;// add 2 to start&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;evenNumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`NEXT: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;evenNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;even&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Let’s go though the execution of the code above together:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We first Initialize the variable &lt;code&gt;evenNumbers&lt;/code&gt; with the invocation of &lt;code&gt;even&lt;/code&gt; generator.&lt;/li&gt;
&lt;li&gt;We then wait &lt;code&gt;1000&lt;/code&gt; milliseconds for the first invocation of &lt;code&gt;helper&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1000&lt;/code&gt; milliseconds pass, and &lt;code&gt;helper&lt;/code&gt; is invoked

&lt;ol&gt;
&lt;li&gt;We initialize &lt;code&gt;value&lt;/code&gt; with the invocation of &lt;code&gt;evenNumbers.next&lt;/code&gt;

&lt;ol&gt;
&lt;li&gt;We initialize &lt;code&gt;start&lt;/code&gt; with &lt;code&gt;0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then we &lt;code&gt;yield&lt;/code&gt; &lt;code&gt;start&lt;/code&gt; and &lt;em&gt;pause&lt;/em&gt; the generator.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Now we &lt;code&gt;console.log&lt;/code&gt; the &lt;code&gt;value&lt;/code&gt;
&lt;/li&gt;

&lt;/ol&gt;

&lt;/li&gt;

&lt;li&gt;Wait another &lt;code&gt;1000&lt;/code&gt; milliseconds for the second invocation of &lt;code&gt;helper&lt;/code&gt;

&lt;ol&gt;
&lt;li&gt;We enter the &lt;code&gt;while&lt;/code&gt; loop

&lt;ol&gt;
&lt;li&gt;Increment &lt;code&gt;start&lt;/code&gt; by 2.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;yield&lt;/code&gt; &lt;code&gt;start&lt;/code&gt; and pause the generator.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Now we &lt;code&gt;console.log&lt;/code&gt; the &lt;code&gt;value&lt;/code&gt;.&lt;/li&gt;

&lt;/ol&gt;

&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;Great! So how do we use the generator function to get the nth Fibonacci number? What we want to do is&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an infinite sequence of Fibonacci numbers using a generator.&lt;/li&gt;
&lt;li&gt;Keep invoking &lt;code&gt;Fibonacci.next&lt;/code&gt; until we get the nth Fibonacci number.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  1. Create an infinite sequence of Fibonacci numbers using a generator
&lt;/h3&gt;

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

&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Fibonacci&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// base Fibonacci numbers&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="c1"&gt;// next Fibonacci number&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;
    &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="c1"&gt;// new a will be what used to be b&lt;/span&gt;
    &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="c1"&gt;// new b will be what used to be c&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;h3&gt;
  
  
  2. Keep invoking &lt;code&gt;Fibonacci.next&lt;/code&gt; until we have the nth number
&lt;/h3&gt;

&lt;p&gt;We can do this by using a standard &lt;code&gt;for&lt;/code&gt; loop:&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;function&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Fib&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Fibonacci&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Fib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&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;And there you have it: a faster function to find the nth Fibonacci number. Look at the difference in speed! ~8 minutes vs ~0.029 milliseconds!&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%2Fziywuaupkj6s2nw0dwww.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%2Fziywuaupkj6s2nw0dwww.png" alt="console showing a 0.03 millisecond execution of f(50)"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>career</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
