Launch GraphOS Studio

Federation 2 quickstart

Part 2 - Composition in Apollo Studio

Now that our project and tools are set up, we can start composing our with Apollo Studio.

Managed federation concepts

To compose our with Studio, we use a feature called managed federation. With , each of our publishes its schema to Apollo. Whenever a changes, Apollo attempts to compose a new .

Publishes schema
Publishes schema
Composes supergraph schema
Locations subgraph
Reviews subgraph
Apollo Schema Registry

Whenever succeeds, Apollo pushes an updated to Apollo Uplink, a special endpoint that use to fetch their configuration:

Updates config
Apollo Schema Registry
Apollo Uplink

Meanwhile, our running regularly polls Uplink for changes to its :

Polls for changes
Downloads changes
Graph router
Apollo Uplink

Whenever an update is available, the downloads it and automatically begins using the new , without requiring a restart.

We strongly recommended for reducing downtime in your production . Let's set it up!

1. Register your subgraph schemas

Let's return to our project. We can now use 's subgraph publish command to register our with Apollo.

Run the following from your project directory, substituting your Studio 's graph ref where indicated:

rover subgraph publish YOUR_GRAPH_REF \
--routing-url \
--schema ./locations.graphql \
--name locations

If the command is successful, you'll see output like the following:

A new subgraph called 'locations' for the 'docs-example-graph@current' graph was created
The gateway for the 'docs-example-graph' graph was updated with a new schema, composed from the updated 'locations' subgraph

Nice! If you open your 's details in Studio now, you'll see types and from our locations listed in the Schema tab:

Locations subgraph schema in Studio

Now, let's do the same thing for our reviews , again substituting your graph ref where indicated:

rover subgraph publish YOUR_GRAPH_REF \
--routing-url \
--schema ./reviews.graphql \
--name reviews

If you refresh the Schema tab in Studio, you'll now see types and from our reviews service as well.

Now that we've published our , Apollo Studio automatically composes them into a ! However, our doesn't know how to fetch that schema from Apollo. We'll tackle that next.

2. Authenticate the router with Apollo Studio

It's time to enable our to fetch its from Apollo. To do that, we'll need a graph API key that we set as the value of an environment variable.

⚠️ API keys are secret credentials. Never share them outside your organization or commit them to version control. Delete and replace API keys that you believe are compromised.

  1. Obtain a API key for your Studio graph by following these steps. If you have an , set the API key's role to Contributor.

    Make sure to copy and paste the API key's value somewhere so you can reference it (for security, API keys are not visible in Studio after creation).

  2. Paste the following terminal command into your text editor so you can make changes to it:

    APOLLO_KEY=your-api-key \
    APOLLO_GRAPH_REF=your-graph-id@your-variant \

    Then, make the following changes:

    • Replace your-api-key with your API key.
    • Replace your-graph-id@your-variant with your 's graph ref.
      • For a refresher on graph refs, see Step 1.
  3. Paste the edited command into your terminal and run it.

    This time there's no error, and you'll see output similar to the following:

    By providing an API key to the , you also automatically enable federated trace reporting to Apollo Studio, enabling you to view helpful performance metrics. Learn more about federated traces.

  4. Optionally, to help you keep track of your 's environment variables, consider setting up a tool like direnv so you don't need to set the every time you run ./router.

    (Most cloud deployment environments provide a helpful way to set environment variables, so this issue is mostly limited to local development.)

Now that our is running, we can quickly open our browser to localhost:4000 to explore our composed schema in :

Schema view in Apollo Sandbox

While we're here, try executing some test queries against the !

Moving forward

Nice work! We've registered two subgraph schemas with Apollo, and we have a graph router that then fetches the composed supergraph schema.

If we now publish changes to one of our , our running automatically fetches the corresponding changes to the (assuming succeeds).

Next, let's look at how to use the to compose a locally or in a CI environment. Go to part 3.

Project setup
Local composition
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy