Join us for GraphQL Summit, October 10-12 in San Diego. Use promo code ODYSSEY for $400 off your pass.
Launch GraphOS Studio

Federation quickstart

Part 3 - Local composition

Back in Part 2, we used managed federation with Apollo Studio to compose our 's . Next, let's try out composing locally with the CLI.

1. Provide subgraph details

Like Apollo Studio, the CLI needs the following information about each of our s to compose them:

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

To provide these details to , we define a YAML configuration file.

In your project directory, create a file called supergraph-config.yaml and paste the following into it:

federation_version: 2

As you can see, we're providing the same URL as the value of two different s. These fields serve different purposes:

  • routing_url is the URL the router will use to send GraphQL s to the at runtime.
  • schema.subgraph_url is the URL that Rover will use to fetch the for composition purposes.

These URLs might theoretically differ. The YAML file also supports providing a 's as a local file path, or as a registered graph ref that can fetch from Apollo (for details, see the Rover docs).

2. Perform composition

Now that our configuration file is ready, we can compose our . To do that, we'll use 's supergraph compose command.

From your project directory, run the following command in your terminal:

rover supergraph compose --config ./supergraph-config.yaml

⚠️ Important: The first time you run this command with a Federation 2 YAML configuration, installs a separate plugin and prompts you to accept the terms and conditions of the ELv2 license.

To automatically accept this prompt (especially important for CI environments), add APOLLO_ELV2_LICENSE=accept to the beginning of the command.

Learn more about ELv2.

outputs the following :

As you can see, this composed includes all of the types and s from our schemas, along with many additional s that the uses to route incoming s.

Now, append > supergraph.graphql to the above command to write the composed to a file:

rover supergraph compose --config ./supergraph-config.yaml > supergraph.graphql

3. Provide the composed schema to the router

To provide a -composed to our , we pass it as a command-line option. Run the following in your router project directory:

./router --supergraph=supergraph.graphql

For brevity, we're skipping setting the APOLLO_KEY environment here. It isn't required if you provide your with --supergraph, however you do need to provide it to enable federated trace reporting.

This time, the reads your static from a file. This does not communicate with Apollo Studio, which means it's well suited to local development or CI environments where you don't want to introduce an external dependency.

Great job! We've seen how to compose our with both managed federation and the CLI. Next, let's look at how to apply what we've learned to our own s. Go to part 4.

2️⃣ Composition in Apollo Studio
4️⃣ Working with subgraphs
Edit on GitHubEditForumsDiscord