npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

apollo-subgraph-cli

v0.1.1

Published

A CLI for subgraph schema management

Downloads

5

Readme

apollo-subgraph-cli

npm Package License Build Status

Overview

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.

Key Features

  • 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.

init demo


Installation

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

Usage

The CLI provides commands under the primary subgraph command.

General Syntax

subgraph <command> [options]

Run subgraph --help to view available commands and options.


Commands

1. init

Sets up a new subgraph schema project interactively.

Usage:

subgraph schema init

The init command will:

  1. Prompt for Input:

    • Location of the type definitions (supports glob patterns).
    • Path for the generated schema file (recommended: project root).
    • Names for print and check commands as npm scripts.
    • Whether to stage newly created files in Git.
  2. 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.
  3. Update package.json Scripts:

    • Writes print and check npm scripts pointing to the subgraph.config.ts file.
  4. 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'

2. print

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\""
}

3. check

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

Configuration

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.

Example subgraph.config.ts

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;

Benefits of Using subgraph.config.ts

  • Centralized schema locations and output paths.
  • Simplifies script definitions in package.json.
  • Reduces command-line complexity.
  • Supports arg overrides for flexibility.

Example Workflow

  1. Initialize a subgraph project:

    npx subgraph schema init
  2. Validate your schema:

    npx subgraph schema check

    The CLI uses the subgraph.config.ts file for defaults.

  3. Generate a consolidated schema:

    npx subgraph schema print
  4. Enable watch mode for automatic updates:

    npx subgraph schema print -w

Support

For issues or feature requests, visit GitHub Issues.


Contributors

This project follows the all-contributors specification. Contributions of any kind welcome!


License

This project is licensed under the MIT License.

npx subgraph schema init

./src/gql/schema/typeDefs/*.ts

!./src/gql/schema/typeDefs/index.ts