TechnologyApril 27, 2022

Build a Crypto App Using GraphQL Federation

Build a Crypto App Using GraphQL Federation

If you’re already familiar with GraphQL, learning to federate data with GraphQL APIs is a good next step. It lets you serve output from several endpoints as if it was from a single database.

Federation lets companies with large, distributed architectures achieve crucial scalability and flexibility. Adobe uses GraphQL federation across 40 API endpoints to improve agility and velocity. Netflix developed a federated GraphQL platform to quickly and reliably scale their API for their studio ecosystem. And those are just two of countless examples.

After reading this post, you’ll be ready to go through the GraphQL federation workshop. Software Engineer Ania Kubów is the host and shows us how to build a simple cryptocurrency application with GraphQL federation.

In the process, you’ll learn how to create a database using DataStax Astra DB, interact with it using GraphQL, and federate data from several databases to populate your crypto dashboard.

There’s no special software to install. You'll only use free, open-source technologies, including:

  • React to develop the crypto app user interface
  • Astra DB as our free, serverless database
  • GraphQL to create, read, update, and delete app data
  • Apollo to federate data from our databases

Understanding GraphQL federation

As you probably know, GraphQL is an open-source query language, originally built by Facebook. It’s used to fetch data from an API into your application.

You can use it to hide the complexity of multiple systems behind a single interface. This lets you save bandwidth and improve performance, because you can fetch data from microservices, APIs, and several other sources in just one request.

Figure 1: GraphQL provides one interface with input from multiple services. (Source)

A federated architecture merges multiple schemas from different services into one unified API endpoint. This sounds simple. But having a single GraphQL API is tricky in enterprise-scale systems.

This is where the gateway layer Apollo Federation swoops in. It lets you easily consolidate multiple GraphQL API services into one. This way, an enterprise-scale graph can work as a collection of separately maintained GraphQL services. This gives teams the freedom to work on different parts of the graph schema independently. It also makes it faster and easier to scale, manage, and deploy.

At a high level, Apollo Federation architecture consists of:

  • Subgraph services, where each one corresponds to a specific GraphQL schema
  • A gateway that unifies schemas in a federated graph and handles queries across subgraphs

Graphic of Apollo Federation-GraphQL Architecture

Figure 2: How Apollo Federation consolidates GraphQL architecture. (Source)

In the workshop, Ania Kubów uses Apollo Federation to join two databases together. Their combined output is then channeled into a crypto app. She creates the database in DataStax Astra DB.

Your free database with DataStax Astra DB

Astra DB is a serverless database-as-a-service (DBaaS) that fully manages Apache Cassandra®. You get the high availability and reliability of Cassandra without having to operate or manage it.

The best part? You can tinker around with Astra DB and use up to 80 GB for free each month. So you can get familiar with deploying multi-tenant or dedicated databases on AWS, Azure, or GCP. If you need to scale later on, then you simply pay as you go and only get charged for what you use (remember to use the discount code shared in the workshop).

The second best part? Once you have your hands on Astra DB, you automatically get access to Stargate. It’s an open-source data gateway that makes it easy to query any Cassandra database through REST APIs. 

One such API is the Stargate GraphQL API. It allows you to:

  • Get the data you need from any Cassandra database with GraphQL types, queries, and mutations
  • Easily and flexibly combine data from multiple services
  • Create new database tables directly via the API
  • Prototype your mutations and queries using the built-in GraphQL Playground servlet

Graphic of Stargate API to Astra DB

Figure 3: How the Stargate GraphQL API works as the data source in Astra DB.

You’ll use the Stargate GraphQL API as your data source in Astra DB. Apollo will be your gateway to the federated databases.

Build a crypto app in just one hour

With a better idea of GraphQL federation, you’re ready to see it in action and build your very own crypto app. Here’s what you’ll be doing in the workshop:

  • Create an Astra DB database
  • Create another database with dummy data
  • Build a simple crypto dashboard using React
  • Federate data from both databases with GraphQL
  • View crypto information on your nifty new dashboard

By the end you’ll know how to federate two databases using GraphQL to populate a crypto app dashboard that looks something like this:

Screenshot of the final interface of the crypto app

Figure 4: Interface showing data on Bitcoin and Ethereum in a crypto app.

Ready to dig into the workshop? Take a moment to sign up for your free Astra DB account so you can keep up with the workshop video on YouTube. Find the source code in Ania’s GraphQL workshop repo on GitHub.

Building this simple crypto app will teach you the basics of a game-changing approach to GraphQL APIs. Once you’ve mastered this, keep the ball rolling with our blog post on data federation using Stargate GraphQL.

Explore more free tutorials on our DataStax Developers YouTube channel.

Resources

  1. Learn GraphQL Federation by building a Crypto App
  2. Deploy a Netflix Clone with GraphQL and DataStax Astra DB
  3. Join our Discord: Fellowship of the (Cassandra) Rings
  4. Astra DB — Managed Apache Cassandra as a Service
  5. Accessing Cassandra with the Stargate GraphQL API
  6. React — A JavaScript library for building user interfaces
  7. The what, when, why, and how of federated GraphQL

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.