1. Course overview and setup
5m

A diagram showing an app running in space, with data circling it

Overview

In this course, we'll dive into subscriptions and see how we can enable them as part of our federated .

We'll start by exploring how to define in (). We'll talk about the requirements for realtime data in , along with the two subscription methods the supports: WebSockets and HTTP callbacks. Then we'll dive deeper into callbacks, implementing real-time data capabilities in our . Finally, we'll take things even further with some cool optimizations and federated graph features.

Ready to jump in? Let's go!

What we're building

As we learn and practice these concepts, we'll bring them into our course project: Airlock.

The Airlock app homepage with a list of places to book.

Airlock is an intergalactic trip booking app: a resource we can use to find a cool place to stay in the wide open universe.

In this course, we'll equip our app with real-time data capabilities. First, we'll wire up a chat feature that uses federated . Then, we'll power up our subscription with data from across our federated .

A mockup of an Airlock themed chat window

Let's get everything ready so we can 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 , as well as the basics of federation. Check out our other courses, Intro to GraphQL TypeScript and Federation with TypeScript & Apollo if you need a refresher.

You should also be familiar with TypeScript programming concepts.

Code editor or IDE

We're using VS Code.

Many popular IDEs offer plugins that enable syntax highlighting. For VS Code, we recommend the GraphQL: Syntax Highlighting extension.

Clone the repository

Let's get our code set up. Run the command below to clone the project repository.

git clone https://github.com/apollographql-education/odyssey-federated-subscriptions.git

Running the project

Our project repository consists of three directories: accounts, messages, and router. We'll set up our in an upcoming lesson, so for now we'll install dependencies and run the accounts and messages .

In a terminal window, navigate to the accounts directory and run the following command to install dependencies:

accounts
npm install

And then the command to start up the :

accounts
npm run dev

Then we'll open up a new terminal window and do the same for the messages ! With both subgraphs running (accounts on port 4002, messages on 4001), we're good to go.

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.

Task!

Sign up for an Apollo GraphOS account with an Enterprise or GraphOS Trial plan

This course requires an Apollo account with an Enterprise or Trial plan. 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 trial.

Task!

Creating a supergraph

Finally, let's create a in . We'll need these (enterprise) graph's credentials to work with in our local environment.

Check your plan: Part of this course covers the self-hosted GraphOS Router, which requires an Enterprise or GraphOS Trial plan. 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 trial.

  1. Open a new browser window and go to GraphOS Studio.

  2. If you haven't created a in Studio before now, you'll be prompted to do so. Otherwise, we can create a new graph by clicking the + Create New Graph button in the upper right corner of the dashboard.

    studio.apollographql.com

    The New Graph button in Studio

  3. We'll give our a descriptive title, keep the default settings for Graph Architecture as "", then click Next.

    studio.apollographql.com

    The create graph modal in Studio, filled out with the name federated-subscriptions

    If you don't see the modal above, you may be on the wrong plan.

    Check your plan: Part of this course covers the self-hosted GraphOS Router, which requires an Enterprise or GraphOS Trial plan. 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 trial.

  4. We should now see a modal with options for publishing a schema.

studio.apollographql.com

A modal window showing options for publishing the schema for our graph

Task!

Saving our graph credentials

To run our locally, we'll need two values:

  • APOLLO_KEY: Your 's API key, used to interact with a single in . It starts with something like service:your-graph-name.
  • APOLLO_GRAPH_REF: The erence (or graph ref) for our . A graph ref starts with the graph's ID, followed by an @ symbol, followed by the .

We'll use these values every time we run the , so we'll store them in our project's router directory, in a new file called .env. Let's add that now.

📦 router
┣ 📄 router-config.yaml
┣ 📄 supergraph.yaml
┗ 📄 .env

Note: We've provided a couple of configuration files here in the router directory; they contain some boilerplate that we'll get to later in the course.

Next, we'll locate the values we need to save.

Go back to the configuration options in Studio that appeared after you created your . Make sure you're on the Schema Document tab.

  1. First, make sure that the Supergraph Pipeline Track dropdown is set to Federation 2.8 Supergraph. This specifies that our should be built using the latest features of .

    studio.apollographql.com

    The schema publish modal, highlighting the Supergraph Pipeline Track dropdown

  2. Below, take a little peek at the command for publishing a . This shows how we can use to publish our subgraphs from the command line. We'll run this command for both subgraphs in just a moment, but for now let's just focus on the APOLLO_KEY value included here.

    APOLLO_KEY=your-graphs-apollo-key \
    rover subgraph publish your-graph-name@current \
    --name products --schema ./products-schema.graphql \
    --routing-url http://products.prod.svc.cluster.local:4001/graphql
  3. Click on the eye icon on the code block to reveal the full value of APOLLO_KEY. Copy APOLLO_KEY and its value into your project's .env file. We'll need it for the next step, and won't have access to the same key again through Studio.

    studio.apollographql.com

    The Apollo Key value outlined, along with the eye icon that toggles the value from hidden to visible

    .env
    APOLLO_KEY=your-unique-apollo-api-key
  4. Now let's go back to Studio to get our . The value we're looking for appears in the same code block, directly after the rover subgraph publish part of the command. We'll save this value as an environment variable as well, but we can access it anytime from our 's home page.

    studio.apollographql.com

    The graph ref value outlined

    APOLLO_KEY=your-graphs-apollo-key \
    rover subgraph publish your-graph-name@current \
    --name products --schema ./products-schema.graphql \
    --routing-url http://products.prod.svc.cluster.local:4001/graphql

Great! Your .env file should now have both values for APOLLO_KEY and APOLLO_GRAPH_REF.

.env
APOLLO_KEY=your-unique-apollo-api-key
APOLLO_GRAPH_REF=your-graph-name@current

Publishing the subgraph schemas

has a command ready to help us with this important task: rover subgraph publish. This command pushes the latest version of a single to Apollo Studio.

rover subgraph publish <APOLLO_GRAPH_REF> \
--name <SUBGRAPH NAME> \
--schema <SCHEMA FILE PATH> \
--routing-url <ROUTING URL>

To use this command, we need the for the we want to publish to and the following command line options:

OptionWhat is it?
--nameWhat we want to call our subgraph in Apollo Studio
--schemaThe relative path to our subgraph's schema file
--routing-urlThe URL where our subgraph runs (locally, for now)

Let's fill out this command for our accounts .

Note: If your accounts server is not already running, boot it up now with npm run dev!

  1. Bounce back to the terminal and make sure we're in the accounts directory of our project.

  2. Now let's type out the rover subgraph publish command:

    Next, we'll paste in the value of our APOLLO_GRAPH_REF environment variable.

    For the name option, we'll pass in accounts.

    For the schema option, we'll pass in the path to our schema, which from the root of the accounts directory is ./src/schema.graphql.

    And for the routing-url option, we'll pass in http://localhost:4002.

    rover subgraph publish <APOLLO_GRAPH_REF> \
    --name accounts \
    --schema ./src/schema.graphql \
    --routing-url http://localhost:4002

    Note: We've used the \ character in this command to improve legibility by putting each command-line option on its own line. If you choose to type the entire rover subgraph publish command on a single line, you don't need to include the \.

  3. You'll see the following message: The host localhost is not routable via the public internet. Continuing the publish will make this subgraph reachable in local environments only. Would you still like to publish? [y/N] Go ahead and tap the y key. We'll stick to local environments for this course!

  4. If all is well in the world, running this command should output a message confirming that the has been published and the has been updated!

Great, that's the accounts taken care of: let's do the same for messages! Navigate up one level in your terminal, and into the messages directory. We'll fill out the same command using our messages 's details.

rover subgraph publish <APOLLO_GRAPH_REF> \
--name messages \
--schema ./src/schema.graphql \
--routing-url http://localhost:4001

With that, our are published and ready to go.

Up next

With our setup out of the way, let's get a feel for what are and how they fit into a federated architecture.

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.