Docs
Launch GraphOS Studio
You're viewing documentation for a previous version of this software. Switch to the latest stable version.

Federation quickstart

Part 1 - Local schema composition


In addition to completing this tutorial, you can

. The GitHub example is not identical to the gateway you create in this tutorial, but it demonstrates many of the same concepts.

Hello! This tutorial gets you up and running with . It currently requires

.

Federation concepts

In a federated architecture, multiple APIs are composed into a single federated . The individual APIs are called subgraphs, and they're composed into a supergraph:

Supergraph (A + B + C)
Subgraph A
Subgraph B
Subgraph C

Usually, each corresponds to a different service in your backend. The is then represented by a gateway, which routes each incoming to the appropriate combination of and returns the combined result.

The supergraph's schema is the combination of each subgraph's schema, plus some special federation-specific . Each subgraph's schema can even reference and extend types that originate in a different subgraph. (

)

This architecture enables clients to query data from multiple services simultaneously, just by querying the gateway.

Now that we have a high-level understanding of federation concepts, let's jump in.

Example subgraphs

This tutorial uses two Apollo-hosted subgraphs for an example application: a Products subgraph and a Reviews subgraph. Here are their schemas for reference:

1. Install the Rover CLI

Rover is Apollo's CLI for managing , including federated ones.

Install it like so:

npm install -g @apollo/rover

After installing, run rover in your terminal with no to confirm that it installed successfully. We'll use various commands in later steps.

2. Create a gateway

You can also

. The GitHub example is not identical to the gateway you create in this tutorial, but it demonstrates the same concepts.

As mentioned

, your federated supergraph is represented by a gateway that routes queries to various subgraphs. For this tutorial, we'll use some Apollo-hosted example services as our subgraphs, and we'll set up our own gateway in front of them.

With the help of the @apollo/gateway library,

can act as our federated gateway.

On your development machine, first create a new project directory for your Node.js gateway. Then inside that directory, run the following to create a package.json file:

npm init

Next, install the following required libraries:

npm install apollo-server @apollo/gateway

Finally, create a file named index.js and paste the following into it as a minimal (not-yet-functional) gateway implementation:

index.js
const { ApolloServer } = require('apollo-server');
const { ApolloGateway } = require('@apollo/gateway');
const supergraphSdl = ''; // TODO!
const gateway = new ApolloGateway({
supergraphSdl
});
const server = new ApolloServer({
gateway,
});
server.listen().then(({ url }) => {
console.log(`🚀 Gateway ready at ${url}`);
}).catch(err => {console.error(err)});

This code demonstrates the basic flow for creating a gateway:

  1. We initialize an ApolloGateway instance and pass it the complete composed for our supergraph.

    • Note the TODO. We'll obtain the composed schema in the next section.
  2. We initialize an ApolloServer instance and pass it our gateway via the gateway option.

  3. We call listen on our server instance to begin listening for incoming requests.

If we run this code as-is with node index.js, we get an error:

GraphQLError [Object]: Syntax Error: Unexpected <EOF>.

That's because our supergraphSdl is currently empty! Next, we'll compose that schema.

3. Compose the supergraph schema

As a best practice, your gateway should not be responsible for composing its own . Instead, a separate process should compose the schema and provide it to the gateway. This helps improve reliability and reduce downtime when you make changes to a subgraph.

There are multiple ways to compose a supergraph schema from our :

  • On our local machine using the (we'll start with this)
  • Via in Apollo Studio (we'll switch to this in
    Part 2
    )

Using managed federation is strongly recommended for production environments. We'll start with local to get up and running.

Providing subgraph details

To compose our supergraph schema, the Rover CLI needs the following information about each of our subgraphs:

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

Because we're using Apollo-hosted example subgraphs, we know their endpoint URLs. And Rover can use those URLs to fetch their schemas via a special query.

Configuration file

We provide subgraph details to the Rover CLI in a YAML file. In your project directory, create a file called supergraph-config.yaml and paste the following into it:

supergraph-config.yaml
subgraphs:
products:
routing_url: https://rover.apollo.dev/quickstart/products/graphql
schema:
subgraph_url: https://rover.apollo.dev/quickstart/products/graphql
reviews:
routing_url: https://rover.apollo.dev/quickstart/reviews/graphql
schema:
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 . These fields serve different purposes:

  • routing_url is the URL the gateway will use to send GraphQL to the subgraph at runtime.
  • schema.subgraph_url is the URL that Rover will use to fetch the 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 that Rover can fetch from Apollo (for details,

).

Performing composition

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

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 supergraph compose --config ./supergraph-config.yaml > supergraph.graphql

4. Start the gateway

We can now edit our index.js file to pull in our composed schema. Replace the file's contents with the following:

index.js
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,
});
server.listen().then(({ url }) => {
console.log(`🚀 Gateway ready at ${url}`);
}).catch(err => {console.error(err)});

Now with our supergraphSdl properly populated, let's start up the gateway again with node index.js. This time, there's no error!

We can quickly open our browser to

to explore our composed schema in :

Schema view in Apollo Sandbox

While we're here, you can even execute some test queries against the supergraph.


Nice job! Our supergraph gateway is running locally and communicating with our Apollo-hosted subgraphs.

Next, we'll move our supergraph composition into Apollo Studio so our gateway can pull schema changes dynamically during runtime.

Previous
Introduction
Next
Part 2 - Composition in Apollo Studio
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company