GraphQL & Distributed SQL Tips and Tricks - Aug 10, 2020

GraphQL & Distributed SQL Tips and Tricks - Aug 10, 2020

Explore both beginner & advanced topics on how to combine GraphQL & YugabyteDB to develop scalable APIs & services, including scalar computed fields, event triggers, & more. First things first, for those of you who might be new to either GraphQL or distributed SQL.

Welcome to this week’s tips and tricks blog where we explore both beginner and advanced topics on how to combine GraphQL and YugabyteDB to develop scalable APIs and services.

First things first, for those of you who might be new to either GraphQL or distributed SQL.

What’s GraphQL?

GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. GraphQL allows for fetching, modifying, and subscribing to real-time data updates. Although GraphQL isn’t tied to any specific database or storage engine and is instead backed by your existing code and data, YugabyteDB is a perfect complement to GraphQL, giving you horizontal scalability and global data distribution in a single system. Use cases for GraphQL include microservices and mobile apps. Popular open source GraphQL projects include Hasura and Apollo.

What’s YugabyteDB?

YugabyteDB is an open source, high-performance distributed SQL database built on a scalable and fault-tolerant design inspired by Google Spanner. YugabyteDB is PostgreSQL wire compatible and supports GraphQL along with advanced RDBMS features like stored procedures, triggers and UDFs.

Got questions? Make sure to ask them in our YugabyteDB Slack channel. Ok, let’s dive in…

Can I use Hasura’s scalar computed fields with YugabyteDB?

Yes. A computed field in Hasura that has an associated SQL function and returns a base type is considered a scalar computed field. To illustrate how this can be done in a distributed SQL database, we’ll be using the Northwind sample database running on YugabyteDB in our example. The employees table has two varchar columns: first_name and last_name.

Let’s define an SQL function called employees_full_name:

CREATE FUNCTION employees_full_name(employees_row employees)
RETURNS TEXT AS $
  SELECT employees_row.first_name || ' ' || employees_row.last_name
$ LANGUAGE sql STABLE;

Next, in the Hasura console let’s add a computed field called full_name to the employees table using the employees_full_name function.

In the Hasura console let’s add a computed field called full_name to the employees table

Now we can query the employees table and retrieve the values for full_name.

query a table example graphql distributed sql yugabytedb

Does YugabyteDB support Hasura’s Event Triggers so I can be alerted when new data is inserted into a table?

Yes. Hasura has built in support for event triggers which work with YugabyteDB. In this example we’ll trigger an alert to Slack via Zapier whenever a new product is inserted into the products table of the Northwind sample database.

The first thing to do is within the Hasura console, navigate to the Events tab and click on the Create Trigger button. Name the trigger new_product_email and make sure it will go off when inserts are made to the products table.

Hasura Event Triggers and Distributed SQL with YugabyteDB

Note that in the example above, I am using a Zapier catch webhook that ultimately posts the raw body of the message to Slack. You can obviously have different types of webhooks that do things like send email, post to a spreadsheet, and more. For other options, check out Hasura’s “Create Event Trigger” tutorial.

Next, insert a row into the products table.

insert a row into the products table distributed sql yugabytedb graphql example

We should soon see a message like the one shown below post to Slack.

example of message yugabytedb distributed sql hasura graphql tips and tricks

And if we go to Events > Processed Events in the Hasura console, we should see a record of the event trigger.

Navigate to Processed Events in the Hasura console yugabytedb distributed sql tips and tricks

databases distributed sql graphql how to sql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Distributed SQL: An Evolution of the Database

The next step in the evolution of database architecture is distributed SQL. Take a look at some of the characteristics here.As organizations transition to the cloud, they eventually find that the legacy relational databases that are behind some of their most critical applications simply do not take advantage of the promise of the cloud and are difficult to scale.

Webinar Recap: CRUD Operations with Hasura GraphQL and Distributed SQL

Explore the possibilities of Hasura GraphQL and Distributed SQL with these resources, including webinar recap, blog post tutorials, and more

AlaSQL in Action: The JavaScript SQL Database

Overview on AlaSQL, the popular lightweight client-side in memory SQL database, including a real life example of AlaSQL in action. I was surprised to see that there aren’t more posts about this popular lightweight client-side in-memory SQL database online apart from this awesome article I found.