Part 3 - Local composition
Back in Part 2, we used managed federation with Apollo Studio to compose our router's supergraph schema. Next, let's try out composing locally with the Rover CLI.
Like Apollo Studio, the Rover CLI needs the following information about each of our subgraphs to compose them:
- The subgraph's schema
- The URL of the subgraph's GraphQL endpoint (which must be accessible by the router)
To provide these details to Rover, we define a YAML configuration file.
In your router project directory, create a file called
supergraph-config.yaml and paste the following into it:
federation_version: 2subgraphs:locations:routing_url: https://flyby-locations-sub.herokuapp.com/schema:subgraph_url: https://flyby-locations-sub.herokuapp.com/reviews:routing_url: https://flyby-reviews-sub.herokuapp.com/schema:subgraph_url: https://flyby-reviews-sub.herokuapp.com/
As you can see, we're providing the same URL as the value of two different fields. These fields serve different purposes:
routing_urlis the URL the router will use to send GraphQL operations to the subgraph at runtime.
schema.subgraph_urlis the URL that Rover will use to fetch the subgraph schema for composition purposes.
These URLs might theoretically differ. The YAML file also supports providing a subgraph's schema as a local file path, or as a registered graph ref that Rover can fetch from Apollo (for details, see the Rover docs).
Now that our configuration file is ready, we can compose our supergraph schema. To do that, we'll use Rover'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, Rover 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.
Rover outputs the following supergraph schema:
As you can see, this composed schema includes all of the types and fields from our subgraph schemas, along with many additional directives that the router uses to route incoming operations.
> supergraph.graphql to the above command to write the composed schema to a file:
rover supergraph compose --config ./supergraph-config.yaml > supergraph.graphql
To provide a Rover-composed supergraph schema to our router, we pass it as a command-line option. Run the following in your router project directory:
For brevity, we're skipping setting the
APOLLO_KEY environment variable here. It isn't required if you provide your schema with
--supergraph, however you do need to provide it to enable federated trace reporting.
This time, the router reads your static supergraph schema 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 supergraph schema with both managed federation and the Rover CLI. Next, let's look at how to apply what we've learned to our own subgraphs. Go to part 4.