The Apollo Subgraph CLI is a powerful tool for managing federated subgraph schemas in Apollo Federation. It enables easy validation, transformation, and generation of schema files, while integrating seamlessly with configuration files for streamlined workflows.
- Schema Validation and Generation: Validate and generate consolidated schema files from multiple type definitions.
- Watch Mode: Automatically regenerate schema files when type definitions change.
- Config-Driven Workflow: Use a
subgraph.config.ts
file to centralize and simplify CLI command configurations. - Interactive Initialization: Quickly set up a new subgraph schema project with helpful prompts.
Install the CLI as a development dependency, along with the required peer dependencies, using your preferred package manager:
npm
npm add graphql @apollo/subgraph
npm add -D apollo-subgraph-cli
pnpm
pnpm add graphql @apollo/subgraph
pnpm add -D apollo-subgraph-cli
yarn
yarn add graphql @apollo/subgraph
yarn add -D apollo-subgraph-cli
The CLI provides commands under the primary subgraph
command.
subgraph <command> [options]
Run subgraph --help
to view available commands and options.
Sets up a new subgraph schema project interactively.
Usage:
subgraph schema init
The init
command will:
-
Prompt for Input:
- Location of the type definitions (supports glob patterns).
- Path for the generated schema file (recommended: project root).
- Names for
print
andcheck
commands as npm scripts. - Whether to stage newly created files in Git.
-
Generate Configuration and Schema Files:
- Creates a
subgraph.config.ts
file, used to centralize CLI configuration. This file is parsed by cosmiconfig, and its TypeScript definitions are published for consumer use. - Consolidates the type definitions into the specified schema file.
- Creates a
-
Update
package.json
Scripts:- Writes
print
andcheck
npm scripts pointing to thesubgraph.config.ts
file.
- Writes
-
Git Integration:
- Stages the newly created files (if approved by the user).
Note: Consumers can specify files to exclude/ignore using the !
sign. This can be added via prompts
when running the init command, updated or added to the subgraph.config.ts
config file, or passed as variadic
arguments when running the print
or check
commands.
-
via config file:
const config: SubgraphConfig = { schema: ['./src/gql/schema/typeDefs/*.ts', '!./src/gql/schema/typeDefs/index.ts'], output: './schema.graphql', };
-
As variadic arguments:
npx subgraph schema print --config subgraph.config.ts --schema './src/gql/schema/typeDefs/*.ts' '!./src/gql/schema/typeDefs/index.ts'
Generates a consolidated schema file from the provided or configured type definitions.
Usage:
subgraph schema print [options]
Options:
-c, --config <path-to-subgraph-config>
Path to the subgraph configuration file.-s, --schema <schema-location...>
Location(s) of the subgraph's type definitions.-o, --output <output-path>
Path to the output schema file (recommended: project root).-w, --watch
Watch for changes in schema files and automatically generate the output.
Example:
npx subgraph schema print --schema './src/gql/schema/typeDefs/*.graphql' --output ./schema.graphql
Tip: For development environments, it is recommended that you utilize the watch flag when running your server. This package uses chokidar for file watching. Chokidar is a peer dependency, so if you decide to utilize the watch flag your will need to install this package in your codebase. To run the file watcher and your server, it is recommended that you use the concurrently package. Example here:
npm add -D chokidar concurrently
"scripts": {
"dev": "concurrently \"pnpm schema:print --watch\" \"tsx watch src/index.ts\""
}
Validates the subgraph schema using provided or configured type definitions.
Usage:
subgraph schema check [options]
Options:
-c, --config <path-to-subgraph-config>
Path to the subgraph configuration file.-s, --schema <schema-location...>
Location(s) of the subgraph's type definitions (e.g.,.graphql
files).
Example:
npx subgraph schema check -c subgraph.config.ts -s ./src/gql/schema/typeDefs/*.graphql
The subgraph.config.ts
file simplifies CLI usage by centralizing schema management configurations. It eliminates the need to repeatedly specify command-line arguments, while still allowing overrides.
import type { SubgraphConfig } from 'apollo-subgraph-cli';
/**
* @property schema - The location of your subgraph's typeDefs
* @property output - The path to the printed schema file
*/
const config: SubgraphConfig = {
schema: ['./src/gql/schema/typeDefs/*.graphql', '!./src/gql/schema/typeDefs/index.graphql'],
output: './src/gql/schema/schema.graphql',
};
export default config;
- Centralized schema locations and output paths.
- Simplifies script definitions in
package.json
. - Reduces command-line complexity.
- Supports arg overrides for flexibility.
-
Initialize a subgraph project:
npx subgraph schema init
-
Validate your schema:
npx subgraph schema check
The CLI uses the
subgraph.config.ts
file for defaults. -
Generate a consolidated schema:
npx subgraph schema print
-
Enable watch mode for automatic updates:
npx subgraph schema print -w
For issues or feature requests, visit GitHub Issues.
This project follows the all-contributors specification. Contributions of any kind welcome!
This project is licensed under the MIT License.
npx subgraph schema init
./src/gql/schema/typeDefs/*.ts
!./src/gql/schema/typeDefs/index.ts