Welcome to this month’s tips and tricks post where we explore both beginner and advanced topics on how to combine GraphQL and Distributed SQL to develop scalable APIs and services.
First things first, for those of you who might be new to either GraphQL or Distributed SQL.
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.
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…
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
:
SQL
1
CREATE FUNCTION employees_full_name(employees_row employees)
2
RETURNS TEXT AS $
3
SELECT employees_row.first_name || ' ' || employees_row.last_name
4
$ 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.
Now we can query the employees
table and retrieve the values for full_name
.
#database #kubernetes #postgresql #postgres #graphql #distributed sql #apollo #yugabyte