<?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: gedunlap</title>
    <description>The latest articles on DEV Community by gedunlap (@gedunlap).</description>
    <link>https://dev.to/gedunlap</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%2F753848%2F0fe998b3-9f7a-4970-a964-ca4f029d4c2b.jpeg</url>
      <title>DEV Community: gedunlap</title>
      <link>https://dev.to/gedunlap</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gedunlap"/>
    <language>en</language>
    <item>
      <title>Using Hasura GraphQL Db with React and ApolloClient</title>
      <dc:creator>gedunlap</dc:creator>
      <pubDate>Tue, 16 Nov 2021 15:50:37 +0000</pubDate>
      <link>https://dev.to/gedunlap/using-hasura-graphql-db-with-react-and-apolloclient-4k71</link>
      <guid>https://dev.to/gedunlap/using-hasura-graphql-db-with-react-and-apolloclient-4k71</guid>
      <description>&lt;h2&gt;
  
  
  Creating and Consuming a Hasura GraphQL Db with React and Apollo Client
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Why GraphQL?!
&lt;/h2&gt;

&lt;p&gt;GraphQL is a way to make specific queries to a single URL endpoint when needed, that returns specific data, rather than multiple calls to various endpoints across an app, as you would in REST.&lt;/p&gt;

&lt;p&gt;This example from the documentation shows two seperate queries returning different data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hhml-pB---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ehhauWm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hhml-pB---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ehhauWm.png" alt="hasura response" width="736" height="622"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(source &lt;a href="https://hasura.io/learn/graphql/intro-graphql/graphql-vs-rest/"&gt;Learn Hasura&lt;/a&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Hasura?
&lt;/h2&gt;

&lt;p&gt;Hasura is an instant realtime GraphQL API engine.&lt;/p&gt;

&lt;p&gt;In just minutes you can have a custom GraphQL API up and running and ready to plug into your app.&lt;/p&gt;

&lt;p&gt;If you'd like to see it in action follow this tutorial of my Todo list API.&lt;/p&gt;

&lt;p&gt;You can also see my &lt;a href="https://github.com/gedunlap/consuming-hasura"&gt;Consuming Hasura GitHub&lt;/a&gt; here!&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started with Hasura
&lt;/h2&gt;

&lt;p&gt;Head over to &lt;a href="https://hasura.io/"&gt;https://hasura.io/&lt;/a&gt; and click the &lt;strong&gt;'Get Started In 30 Second'&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;Use  GitHub, Google or email to create a new Hasura account.&lt;/p&gt;

&lt;p&gt;Verify email address to be redirected to Hasura with this poppup:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wlmCY204--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/QxzPCRk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wlmCY204--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/QxzPCRk.png" alt="popup" width="880" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;'Launch Connsole'&lt;/strong&gt; to be redirected again to the GraphiQL dev environment to get started on the API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the GraphQL API
&lt;/h2&gt;

&lt;p&gt;First connect a database which is easy and free by navigating to the &lt;strong&gt;'DATA'&lt;/strong&gt; tab:&lt;/p&gt;

&lt;p&gt;Then click the &lt;strong&gt;'Create Heroku Database'&lt;/strong&gt; and below that &lt;strong&gt;'Create Database'&lt;/strong&gt; button&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iweBSv6r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/zGhcYYj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iweBSv6r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/zGhcYYj.png" alt="navbar" width="880" height="54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_Pf8r1hI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/PTNSqf9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_Pf8r1hI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/PTNSqf9.png" alt="Database" width="880" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Log in to Heroku via the pop-up and a status bar will appear showing status.&lt;/p&gt;

&lt;p&gt;Once finished click &lt;strong&gt;'View Database'&lt;/strong&gt; button and then the &lt;strong&gt;'Create Table'&lt;/strong&gt; button at the top of that page.&lt;/p&gt;

&lt;p&gt;Name the table 'Todo'&lt;/p&gt;

&lt;p&gt;Configure fields and set the Primare Key to id.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GKj-KrkM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/JewSX1D.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GKj-KrkM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/JewSX1D.png" alt="configure fields" width="880" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Foreign Keys and Unique Keys can be set up later.&lt;/p&gt;

&lt;p&gt;Then click &lt;strong&gt;'Add Table'&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Now you have a new GraphQL API !!!
&lt;/h3&gt;

&lt;p&gt;Next add data to the API by selecting the &lt;strong&gt;'Todo'&lt;/strong&gt; table in the &lt;strong&gt;'Data Manager'&lt;/strong&gt; to the left side of the screen then select the &lt;strong&gt;'Insert Row'&lt;/strong&gt; tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oocSPN17--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/RZsmpbo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oocSPN17--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/RZsmpbo.png" alt="insert row" width="880" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fill the required fields with data excluding the id as it will be generated automatically.&lt;/p&gt;

&lt;p&gt;Press the &lt;strong&gt;'Save'&lt;/strong&gt; button, then repeat this step but make sure to change the data fields then click &lt;strong&gt;'Insert Again'&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once there is at least one row inserted, select the API tab at the top of the page to return to the GraphiQL IDE.&lt;/p&gt;

&lt;p&gt;Here you will now see some options in your GraphiQL Explorer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xl8rcAA5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/N0c4wEF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xl8rcAA5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/N0c4wEF.png" alt="navbar" width="880" height="55"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y2Sb_eb3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/3n9ntR5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y2Sb_eb3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/3n9ntR5.png" alt="explorer" width="632" height="620"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each of these can be use to make different queries and mutations to your API. Start clicking them to see what they do!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--732GN7lu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/JIZjXSv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--732GN7lu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/JIZjXSv.png" alt="query" width="880" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice the &lt;strong&gt;'Docs'&lt;/strong&gt; pop out button in the top right corner. It has documentation created specifically for this table to show how to query and mutate any way you might need.&lt;/p&gt;

&lt;h4&gt;
  
  
  Keep this dashboard open as it will be needed later !!!
&lt;/h4&gt;

&lt;h2&gt;
  
  
  Using React and Apollo Client to consume the API
&lt;/h2&gt;

&lt;p&gt;(Most of this section is directly from &lt;a href="https://www.apollographql.com/docs/react/get-started/"&gt;Apollo Client Documentation&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Start in terminal by navigating to desktop or a folder of choice, next &lt;code&gt;npx create-react-app project-name&lt;/code&gt;, then cd into &lt;code&gt;project-name&lt;/code&gt; and &lt;code&gt;npm install @apollo/client graphql&lt;/code&gt;. Open the file in code editor of choice.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;index.js&lt;/code&gt; initialize ApolloClient by importing:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
    ApolloClient,
    InMemoryCache,
    ApolloProvider,
    HttpLink,
    ApolloLink,
} from "@apollo/client";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next create a &lt;code&gt;.env&lt;/code&gt; file in the root of the project. &lt;/p&gt;

&lt;p&gt;Return to the Hasura Dashboard where two needed items will be found:&lt;/p&gt;

&lt;p&gt;'GraphQL Endpoint' and 'x-hasura-admin-key'.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aCRCoHtn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/6MZ7oBc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aCRCoHtn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/6MZ7oBc.png" alt="key" width="880" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the &lt;code&gt;.env&lt;/code&gt; file and create two variables:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;REACT_APP_KEY=&lt;/code&gt; (paste 'x-hasura-admin-key' here)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;REACT_APP_URI=&lt;/code&gt; (paste 'GraphQL Endpoint' here)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Make sure to use **NO&lt;/strong&gt; quotations on key or url.**&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you have been running the dev server you will need to restart it now for these variables to work&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now back in &lt;code&gt;index.js&lt;/code&gt; use env variables to connect to the API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0dJ96vyz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/hoBRsNB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0dJ96vyz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/hoBRsNB.png" alt="code image" width="573" height="523"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set the endpoint in HttpLink using the created env variable.&lt;/li&gt;
&lt;li&gt;Use ApolloLink to pass headers with admin key env variable.&lt;/li&gt;
&lt;li&gt;ApolloClient concatinates these to send the full request to the API.&lt;/li&gt;
&lt;li&gt;ApolloProvider wraps the App to enable access to ApolloClient throughout.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return to Hasura and copy a database request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--faIXpBzQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/JCK68em.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--faIXpBzQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/JCK68em.png" alt="query" width="880" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, move to &lt;code&gt;App.js&lt;/code&gt; and import:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useQuery, gql } from '@apollo/client'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Write a variable holding the Hasura request inside the backticks of a &lt;code&gt;gql&lt;/code&gt; tag.&lt;/li&gt;
&lt;li&gt;Set a variable with &lt;strong&gt;'loading, error, data'&lt;/strong&gt; equal to &lt;code&gt;useQuery(GET_TODOS)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J1F-O234--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/jDG4Wku.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J1F-O234--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/jDG4Wku.png" alt="use query" width="459" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When your component renders, &lt;code&gt;useQuery&lt;/code&gt; returns an object from Apollo Client that contains loading, error, and data properties you can use to render your UI.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log&lt;/code&gt; &lt;strong&gt;'loading, error, data'&lt;/strong&gt; to see your data on the front end in devtools.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eMEWTGcG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/bnWbFLW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eMEWTGcG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/bnWbFLW.png" alt="devtools" width="880" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now use the data to build out the front end !!!&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wkrLOS2_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/YqAkqWO.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wkrLOS2_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/YqAkqWO.jpg" alt="neo" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>hasura</category>
      <category>graphql</category>
      <category>apolloclient</category>
    </item>
  </channel>
</rss>
