GraphQL is an MIT-licensed project originally developed at Facebook in 2012 and open-sourced a few years later. It is rapidly increasing in popularity with more than 29 million downloads to date. Why? Think of GraphQL as a query language for APIs and a runtime for fulfilling those queries with your existing data.
GraphQL provides a complete and understandable description of the data in your API and gives clients the power to ask for exactly what they need and nothing more. Plus, it makes it easier to evolve APIs over time. GraphQL is also a robust technology and is being used in production at Facebook, GitHub, Pinterest, and Intuit.
What Is Hasura?
Hasura is one of the leading vendors in the GraphQL ecosystem. They offer an open-source engine that connects to your databases and microservices, and then auto-generates a production-ready GraphQL backend.
What Is YugabyteDB?
YugabyteDB is an open-source, high-performance distributed SQL database built on a scalable and fault-tolerant design inspired by Google Spanner. Yugabyte’s SQL API (YSQL) is PostgreSQL wire compatible.
Why Hasura GraphQL and YugabyteDB?
Writing complex database queries can be hard and time-consuming; a GraphQL engine, like Hasura, can help you quickly alleviate these challenges and exploit the benefits of the GraphQL API against a database.
Hasura does the heavy lifting under the covers and automatically generates queries to fetch the data. When combined with YugabyteDB, you inherit all the benefits of a PostgreSQL-compatible, distributed SQL system, with features like high performance, extreme resilience to failures, and geodata distribution. The result is a fast and highly available backend that makes it easy to develop applications and microservices.
In this blog post, we are going to show you how to get started with Hasura and YugabyteDB running on Google Kubernetes Engine (GKE) and conclude by showing you how to perform some basic CRUD operations. Look for more in-depth blog posts concerning Hasura and YugabyteDB in the coming weeks where we will cover sophisticated CRUD operations and benchmarking results.
Here is the environment required for this tutorial:
Set Up a Kubernetes Cluster on Google Cloud Platform
To deploy YugabyteDB on the Google Cloud Platform (GCP), we first have to set up a cluster. To create the cluster in Google Kubernetes Engine (GKE), navigate to Kubernetes Engine> Clusters > Create Cluster.
For the configuration of the cluster, choose a Standard cluster using the default options.
- Name: yugabytedb-cluster1.
- Zone: us-central1-a.
- Master version: 1.14.10-gke 17 (default).
- Number of nodes: 3.
- Machine type: n1-standard-2 (2 vCPU, 7.5 GB memory).
Next, connect via the Google Cloud Shell and verify that the nodes are running by using the following command:
Installing YugabyteDB on the GKE Cluster Using Helm
In this section, we are going to install YugabyteDB on the cluster. The complete steps are documented here.
The first thing to do is to upgrade Helm.
Next, let’s create a service account.
The output should look like this:
The output should look like this:
Create a namespace. In this case, we’ll call it yb-demo.
Next, add the yugabytedb charts repo.
Fetch updates from the repository.
We are now ready to install YugabyteDB. In the command below we’ll be specifying values for a resource-constrained environment.
To check the status of the cluster, execute the below command:
Take a look at the v1/Service section:
Note, the external-IP for LoadBalancer that we are going to use to establish a connection between YugabyteDB and Hasura. From the screenshot above, we can see that the IP is 126.96.36.199 and the YSQL port is 5433.
Creating the Sample Database
The next step is to download a sample schema and data. You can find a variety of sample databases that are compatible with YugabyteDB in our Docs. For the purposes of this tutorial, we are going to use the northwind sample database.
The northwind database contains the sales data for a fictitious company called, “Northwind Traders,” which imports and exports specialty foods from around the world. The northwind database is an excellent tutorial schema for a small-business ERP, with customers, orders, inventory, purchasing, suppliers, shipping, employees, and single-entry accounting.
Connect to the yb-tserver-pod by running the following command:
To download the schema and data files, run the following commands:
Note: If the Google Cloud Shell tells you that the
wget command does not exist, you can execute:
To connect to the YSQL service, exit out of the pod shell and run the following command:
Create a database and connect to it using the following commands:
Create the database objects and load them up with data using the following commands:
Verify that the tables are created by running the following command:
Verify that we have data by issuing a few simple SELECTs:
Setting Up Hasura to Use YugabyteDB
Get the Hasura Kubernetes deployment and service files by executing the commands below.
Modify the database URL in deployment.yaml file. This file can be edited using a text editor like
vi. For the purposes of this tutorial, the modification should look like this:
After saving the file, use
kubectl to create a Hasura deployment using the commands below:
To find the external IP and open the Hasura console execute the command below:
Now, use http://<EXTERNAL-IP>/console to access the Hasura console. In this case, it is 188.8.131.52. You should see the Hasura console as shown below:
We are now ready to perform basic CRUD operations on the database using Hasura!
Querying Data Using Hasura
Click on Data in top nav, click on Track All for tables, views, and foreign key relations. Refresh your browser, and you should now see the 14 northwind tables in the Explorer tree on the left-hand side when you click on GraphQL in the top nav.
Next, select the table and the list of columns that you want to query. Hasura will automatically generate the query. In this example, we’ll use the products table.
Now, click the play button to run the query.
This will return all the entries in the products table. A sample of the output is below:
Query Entries With a
This time, let’s include a
where clause and return the product with the product_id equal to 21. In this case, we’ll click on where > product_id > _eq: and input 21.
Your output should look like this:
Adding a New Entry
Click the ADD NEW MUTATION at the bottom of the explorer tree to find all the mutations available.
In this exercise, let’s add a new product. We can do this by selecting the following items in the explorer tree and adding values for our new product, “Neal’s Greenies.”
Click the play button. We selected to return the product_name and units_in_stock. So, the output after running the insert should look like this:
At this point, we have successfully added a new entry. We can verify this by navigating to the Data tab and issuing a query like the one below:
Updating an Existing Entry
Let’s change the unit_price for “Neal’s Greenies” from 40 to 25. The query and output should like this:
We can verify that the unit price has been changed to 25 by navigating to the Data tab and issuing a query like the one below:
Deleting an Entry
Deleting data is similar to how we constructed queries for the INSERT and UPDATE mutations. In this case, let’s DELETE the “Neal’s Greenies” product from the database by using delete_products in the explorer tree.
The query and output should look like this:
We can verify that “Neal’s Greenies” has been removed from the products table by navigating to the Data tab and issuing a query like the one below. You can see that we get “No rows found.”
That’s it! We’ve seen how easy it is to get YugabyteDB and Hasura up and running on GKE and to perform basic CRUD operations against the database with GraphQL. You are now ready to perform more advanced GraphQL operations against YugabyteDB with help from Hasura. Check out their docs and tutorials on how to get started.