Connect GraphQL APIs (Subgraphs)

Quickly bring a GraphQL API into your graph


This guide walks through a test use case for building, developing, and managing a GraphQL API with Apollo Server subgraphs.

Prerequisites

By the end of the milestone, you'll have:

  • Installed the Rover CLI

  • A federated graph with subgraphs running locally

  • Sent an operation to your local GraphQL endpoint

Before you start

Rover is Apollo's official CLI for managing graphs and subgraphs. It's the main tool for setup, development, publishing schemas, and more.

Install and authenticate Rover by following the Rover CLI Getting Started guide.

Steps to complete

1. Set up your subgraph server locally

If you don't have an existing GraphQL API, choose your preferred programming language from the Rover templates to create a new server.

shell
1rover template use my-new-subgraph

2. Store graph credentials

Copy the APOLLO_GRAPH_REF and APOLLO_KEY from your GraphOS account and paste into a .env file:

Bash
1# .env
2# (Formatted graph-id@variant, references a graph in the Apollo GraphOS platform)
3APOLLO_GRAPH_REF=
4
5# (This is your graph's API key)
6APOLLO_KEY=

You need these credentials to start the local dev server, even for local-only work.

3. Configure supergraph composition

Create a supergraph.yaml file in your project root to tell Rover how to compose your local graph:

YAML
supergraph.yaml
1federation_version: =2.11.0
2subgraphs:
3  users:
4    routing_url: http://localhost:4001
5    schema:
6      file: ./users-schema.graphql
7  products:
8    routing_url: http://localhost:4002
9    schema:
10      file: ./products-schema.graphql

The routing_url must match the port where your subgraph server is running.

4. Start the local development server

Set your environment variables, then run:

sh
1APOLLO_KEY=<YOUR_APOLLO_KEY> \
2APOLLO_GRAPH_REF=<YOUR_APOLLO_GRAPH_REF> \
3rover dev --supergraph-config supergraph.yaml

This command launches the router locally, composing your schema automatically. The Apollo Router serves your federated API at http://localhost:4000.

5. Test and iterate

Use Apollo Sandbox at http://localhost:4000 or any GraphQL client to send queries.

Modify schemas or resolvers as needed to refine your data model. The rover dev command automatically restarts and recomposes if your subgraph schema file changes.

note
rover dev is ideal for quick prototyping, schema validation, and integration testing. For advanced scenarios (multiple subgraphs, versioning, CI/CD integration), see the Federation Testing documentation.

Resources

Feedback

Edit in VSCode

Ask Community