1. Course overview and setup
5m

👋 Welcome to GraphQL Federation with Java & DGS!

We're so excited you've joined us for this course in our and Java series.

We're ready to build upon foundations and dive into a scalable federated architecture. In this course, we'll learn about the components of what's called a supergraph, also known as a federated architecture. And we have lots of hands-on opportunities ahead of us, building new features on top of an existing GraphQL API. We'll use some of the primary components to ship these changes safely: , , and rover dev.

Let's jump in!

Starting from a GraphQL API

First, let's take a look at how a single API works.

A GraphQL server receives requests from clients and resolves them

When a client needs some data, it sends a to the . The server uses its schema, , and to retrieve and resolve that data, then sends it back to the client. It's a pretty great experience!

This setup works well for smaller projects or teams, but what happens as our API grows? As more teams start adding types, , and features to our schema, our API becomes harder to manage, scale, and deploy. This is a common bottleneck problem with monolithic backend services.

Introducing federation

To solve this problem, we can divide our API's capabilities across multiple -powered microservices, with each one taking responsibility for a different part of our API's schema. When we adopt this federated architecture, each of our microservices is called a subgraph, and together, they form the foundation of our supergraph.

An illustration of two subgraphs, with each subgraph having its own schema file, resolvers and data sources.

As our grows, it can include one, two, or even two hundred ! It all depends on how we want to divide our features and capabilities to help our teams build and collaborate.

If we split our API's capabilities between lots of different microservices, how do clients across all of them? For that, we need the other piece of our : the router.

The knows about all of our , and it also knows which subgraph is responsible for each in our API's schema. Clients send queries to the router, and the router intelligently divides them up across the appropriate subgraphs.

The journey of a GraphQL operation, in a supergraph with the router and subgraphs

The acts as the single access point for our API, kind of like an API gateway. Because the router manages how requests are routed and resolved, clients don't need to worry about communicating with our individual !

Following this approach, we can grow our API to include numerous and services: with all of its capabilities unified behind a single, intelligent .

Why use federation?

With a architecture, nothing changes from the client perspective. They still communicate with a single endpoint (the ), and they don't need to know a thing about how the graph is built under the hood.

On the backend API-side, we have a clear separation of concerns. By splitting up our schema into , backend teams can work on their own subgraphs independently, without impacting developers working on other subgraphs. And since each subgraph is a separate server, teams have the flexibility to choose the language, infrastructure, and policies that work best for them.

To give us the observability and governance that we need to manage and build a federated , we'll power up our API with Apollo GraphOS. Apollo GraphOS is a complete cloud platform for building, managing, and scaling your : it provides a set of tools and services so that product developers can focus on building better apps, faster. We'll explore the main components of throughout this course!

What we're building

As we learn and practice these federation concepts, we'll be building our course project: MusicMatcher.

MusicMatcher is a music catalog app: a resource we can use to find the right soundtrack for the right moment. In this course, we're bringing our soundtrack recommendations into a common activity in our daily lives: cooking! Find the perfect playlist to listen to while you're spicing up your latest creation.

We built the basics of this in the Intro to GraphQL course. This server will become the first in our . It takes responsibility for all of the schema's types and about our playlists and tracks.

Let's get everything ready to start building!

Project setup

To follow along with the course, you will need the following:

Prerequisite knowledge

We assume that you are familiar with concepts like types, queries, and . Check out our Intro to GraphQL with Java & DGS course if you need a refresher.

You should also be familiar with Java programming concepts, (this course uses JDK 17) and the basics of Spring Boot.

Code editor or IDE

Many popular IDEs offer plugins that enable syntax highlighting. For IntelliJ, we recommend the GraphQL plugin.

Clone the repository

Let's get our code set up. This course picks up where Intro to GraphQL with Java & DGS left off, with just one addition.

The we built lives in the soundtracks directory, right next to a new directory called router. The router directory contains a file called config.yaml.

📦 dgs-federation
┣ 📂 router
┃ ┣ 📄 config.yaml
┗ 📂 soundtracks

You can use your completed project from the first course in this series, but be sure to add a router directory, with a config.yaml file containing the code in the collapsible below. This will come in handy later!

If you would prefer to start fresh, you can clone the starter code by opening up a terminal to a new directory and running the following command:

git clone https://github.com/apollographql-education/dgs-federation.git

Running the app

Finally, let's get our soundtracks server up and running.

Pull open the soundtracks directory in your IDE and navigate to the main SoundtracksApplication file located in the com.example.soundtracks package. This is the starting point for our app.

@SpringBootApplication
public class SoundtracksApplication {
public static void main(String[] args) {
SpringApplication.run(SoundtracksApplication.class, args);
}
}

In IntelliJ, we have the handy green play button in the margin next to the main function, or the one located at the top of the interface.

Alternatively, you can open a new terminal to the root of your project and run the following command:

./gradlew bootRun

In the IDE Run output, we should see that our app is running!

> Task :SoundtracksApplication.main()
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.0)

Checkpoint

Setup checklist

Sign up for an Apollo GraphOS account with an Enterprise plan

This course uses (more on this in the next lesson!), which requires an Apollo account with an . You can still follow along if your organization is on a different plan, but you won't be able to complete certain hands-on tasks. You can also test out this functionality by signing up for a free Enterprise trial.

Task!

Rover setup

is Apollo's command line interface (CLI) tool that helps developers work with and interact with . It's a handy and versatile tool that can be used for both local development and CI/CD.

In upcoming lessons, we'll use to validate our local changes before we publish them.

Installing Rover

is Apollo's command line interface (CLI) tool that helps developers work with and interact with .

Task!

Practice

Which of the following are benefits of using Apollo Federation?

Key takeaways

  • Federation is an architecture for creating modular , also known as a architecture.
  • A is composed of one or more and the .
  • A is a microservice responsible for its own domain.
  • The is the single endpoint that manages how requests are routed and executed, kind of like an API gateway.

Up next

Our soundtracks API is just crooning 🎷 to provide something with musical accompaniment. In the next lesson, we'll take our first steps toward growing our API and learn about the components of a supergraph.

Next

Share your questions and comments about this lesson

This course is currently in

beta
. Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.