Federation quickstart
Part 3 - Local schema composition
Back in Part 2, we used managed federation with Apollo Studio to compose our gateway's supergraph schema. Next, let's try out composing locally with the Rover CLI.
1. Provide subgraph details
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 gateway)
To provide these details to Rover, we define a YAML configuration file.
In your project directory, create a file called supergraph-config.yaml
and paste the following into it:
subgraphs:products:routing_url: https://rover.apollo.dev/quickstart/products/graphqlschema:subgraph_url: https://rover.apollo.dev/quickstart/products/graphqlreviews:routing_url: https://rover.apollo.dev/quickstart/reviews/graphqlschema:subgraph_url: https://rover.apollo.dev/quickstart/reviews/graphql
As you can see, we're providing the same URL as the value of two different fields. These fields serve different purposes:
routing_url
is the URL the gateway will use to send GraphQL operations to the subgraph at runtime.schema.subgraph_url
is the URL that Rover will use to fetch the subgraph schema during composition.
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).
2. Perform composition
Now that our configuration file is ready, we can compose our supergraph schema. To do that, we'll use Rover's fed2 supergraph compose
command.
From your project directory, run the following command in your terminal:
rover fed2 supergraph compose --config ./supergraph-config.yaml
📣 Federation 2 changes: Note that this command is rover fed2 supergraph compose
, not rover supergraph compose
(which uses Federation 1 composition).
Rover outputs the following 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 gateway uses to support our federated architecture.
Now, append > supergraph.graphql
to the above command to write the composed schema to a file:
rover fed2 supergraph compose --config ./supergraph-config.yaml > supergraph.graphql
3. Update the gateway
When we provide a Rover-composed supergraph schema to our gateway, we pass it as an option to the constructor of ApolloGateway
, instead of providing it as an environment variable.
Open your index.js
file and replace its contents with the following:
const { ApolloServer } = require('apollo-server');const { ApolloGateway } = require('@apollo/gateway');const { readFileSync } = require('fs');const supergraphSdl = readFileSync('./supergraph.graphql').toString();const gateway = new ApolloGateway({supergraphSdl});const server = new ApolloServer({gateway,// Subscriptions are not currently supported in Apollo Federationsubscriptions: false});server.listen().then(({ url }) => {console.log(`🚀 Gateway ready at ${url}`);}).catch(err => {console.error(err)});
If you run this code with node index.js
, the gateway 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 that 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.