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 closer to the end of the course, but for now we can 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

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.