GraphQL Summit is back for three days of insights, hands-on learning, and fun to celebrate the GraphQL community. Join us in San Diego Oct 3-5.
Docs
Try Apollo Studio

Federation 2 quickstart

Part 1 - Project setup


Hello! This tutorial gets you up and running quickly with an Apollo Federation 2 supergraph.

Related resources:

  • Our in-depth Apollo Federation course goes into greater detail on fundamental federation concepts. It walks through creating a supergraph from scratch, along with federating an existing monolith graph.
    • Note that this course is currently in early access.
  • Our Federation 2 demo app on GitHub is distinct from the project you create in this tutorial. It demonstrates many powerful Federation 2 features.

Federation concepts

Before we set up our project, let's quickly cover what Apollo Federation is.

In a federated architecture, multiple GraphQL APIs are composed into a single federated graph. The individual APIs are called subgraphs, and they're composed into a supergraph:

Supergraph (A + B + C)
Subgraph A
Subgraph B
Subgraph C

Usually, each subgraph corresponds to a different service in your backend. The supergraph is then represented by another service called a graph router, which routes each incoming query to the appropriate set of subgraphs and returns the combined result.

The supergraph's schema is the combination of each subgraph's schema, plus some special federation-specific metadata.

This architecture enables clients to query data from all subgraphs simultaneously, just by querying the router. It also enables different teams to own independent parts of a unified graph.

Supergraph schema composition

As a best practice, your router does not compose its own supergraph schema. Instead, a separate process composes the schema and provides it to the router. This helps improve reliability and reduce downtime when you make changes to a subgraph.

There are multiple ways to compose a supergraph schema from subgraph schemas:

  • Via managed federation in Apollo Studio (we'll start with this)
  • On our local machine with the Rover CLI (we'll do this in Part 3)

Using managed federation is strongly recommended for production environments. Meanwhile, composing with Rover can be useful for local development and CI environments.

Now that we have a high-level understanding of federation concepts, let's jump in!

Example subgraphs

This tutorial uses two Apollo-hosted subgraphs (named Locations and Reviews) from an imaginary space tourism application called FlyBy. Here are their URLs and schemas for reference:

1. Set up Apollo tools

This quickstart uses a couple of Apollo tools to get the most out of federation:

  • Apollo Studio, a cloud platform for monitoring, managing, and collaborating on your supergraph
    • You DON'T need a paid Studio plan to complete this quickstart or use managed federation features!
  • The Rover CLI, a command-line interface for interacting with graphs and their schemas

Let's set these up first!

Sign up for Apollo Studio

Before we can use managed federation with Apollo Studio, we need a Studio account. Let's create one if you don't have one yet.

Complete the first two steps of Get started with Apollo Studio (Create your account and Create your first graph), then return here.

  • Make sure the Studio graph you create is a supergraph that uses Federation 2!
  • For future steps, you'll need the ID of the supergraph you create. Make sure to have it available.

Install the Rover CLI

Rover is Apollo's CLI for managing all kinds of graphs, including subgraphs and supergraphs. We'll use it throughout this quickstart.

Even if you already have Rover installed, you should update your version now by completing this step.

Install the latest Rover release with the appropriate command for your system:

MacOS / Unix-like
curl -sSL https://rover.apollo.dev/nix/latest | sh
Windows
iwr 'https://rover.apollo.dev/win/latest' | iex

After installing, run rover in your terminal with no arguments to confirm that it installed successfully. Verify that the printed version number matches the latest release (if it doesn't, you might need to manually delete a previous outdated installation).

Authenticate Rover with Studio

We'll use the Rover CLI to publish our subgraph schemas to Studio. To do that, we first need to authenticate Rover with Studio.

Complete the first two steps of Configuring Rover (Obtain an API key and Provide the API key to Rover), then return here.

2. Create a router project directory

As mentioned in Federation concepts, your federated supergraph is represented by a graph router that routes queries to various subgraphs. For this tutorial, we'll use some Apollo-hosted example services as our subgraphs, and we'll set up the Apollo Router in front of them.

The Apollo Router is a high-performance, precompiled Rust executable that acts as the router for a supergraph.

Alternatively, Apollo Server can act as your graph router, as documented in The graph router. This tutorial uses the Apollo Router because it's the recommended default for all new supergraphs (for details, see Choosing a router library).

On your development machine, first create a new directory for your router project. Then inside that directory, run the following to install the Apollo Router:

curl -sSL https://router.apollo.dev/download/nix/latest | sh

This installs the router executable in your project directory. You can try running it with the following command:

./router

If you do, you'll get a startup error message like the following:

That's because we aren't currently providing a supergraph schema to the router! We'll fix that soon.

3. Obtain your subgraph schemas

To compose our supergraph schema, Apollo Studio needs the following information about each of our subgraphs:

  • The subgraph's schema
  • The URL of the subgraph's GraphQL endpoint (which must be accessible by the router)

Fortunately, we have all of this information! Let's collect it in our project.

Do the following in your project's root directory:

  1. Create a new file called locations.graphql and paste the following schema into it:

  2. Create a new file called reviews.graphql and paste the following schema into it:

💡 In most federated graphs, each subgraph schema lives in the codebase for its associated subgraph. Because we're using remotely hosted example subgraphs in this tutorial, we're saving these subgraph schemas in our router project for convenience.


We have a Studio account, we've installed Rover, and our project directory is ready! Next, we'll start composing our supergraph schema in Studio.

Edit on GitHub
Previous
⬅️ Backward compatibility
Next
2️⃣ Composition in Apollo Studio