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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@pothos/plugin-sub-graph

v4.5.3

Published

A Pothos plugin for creating multiple variants or sub-selections of the same graph

Downloads

50,875

Readme

SubGraph plugin

Use one set of type definitions to build several API variants. Tag types and fields with named sub-graphs, then pass subGraph to builder.toSchema() to select a variant. This is separate from Apollo Federation: a sub-graph here is a filtered view of your own schema.

Install

npm install --save @pothos/plugin-sub-graph

Build public and internal schemas

Declare the sub-graph names in SubGraphs. In this example, types and fields belong to both variants by default, while internalNotes is available only in the internal API:

import SchemaBuilder from '@pothos/core';
import SubGraphPlugin from '@pothos/plugin-sub-graph';

const builder = new SchemaBuilder<{
  SubGraphs: 'Public' | 'Internal';
}>({
  plugins: [SubGraphPlugin],
  subGraphs: {
    defaultForTypes: ['Public', 'Internal'],
    fieldsInheritFromTypes: true,
  },
});

const Product = builder
  .objectRef<{
    id: string;
    name: string;
    internalNotes: string;
  }>('Product')
  .implement({
    fields: (t) => ({
      id: t.exposeID('id'),
      name: t.exposeString('name'),
      internalNotes: t.exposeString('internalNotes', {
        subGraphs: ['Internal'],
      }),
    }),
  });

builder.queryType({
  fields: (t) => ({
    product: t.field({
      type: Product,
      resolve: () => ({ id: '1', name: 'Notebook', internalNotes: 'Restock next week' }),
    }),
  }),
});

The subGraph option selects which types and fields appear in the built schema:

Public

export const schema = builder.toSchema({ subGraph: 'Public' });

Internal

export const schema = builder.toSchema({ subGraph: 'Internal' });

Combined

export const schema = builder.toSchema({ subGraph: ['Internal', 'Public'] });

Shared

export const schema = builder.toSchema({ subGraph: { all: ['Internal', 'Public'] } });

{ product { name } } works in either variant. { product { internalNotes } } fails GraphQL validation against the Public variant. Calling toSchema() without a sub-graph retains the full schema.

Combine variants

An array includes types and fields belonging to any listed sub-graph:

const combinedSchema = builder.toSchema({ subGraph: ['Internal', 'Public'] });

The all form includes only membership shared by every listed sub-graph:

const sharedSchema = builder.toSchema({ subGraph: { all: ['Internal', 'Public'] } });

For the example above, the combined schema includes internalNotes; the shared schema omits it.

Membership options

Set subGraphs on a type or field to override its defaults. A type without this option uses subGraphs.defaultForTypes from the builder.

A field's membership is determined in this order:

  1. Its own subGraphs option.
  2. Its parent type's defaultSubGraphsForFields option.
  3. Its parent type's membership, when subGraphs.fieldsInheritFromTypes is true.
  4. The builder's subGraphs.defaultForFields option.
  5. An empty array.

fieldsInheritFromTypes defaults to false. An explicit empty array overrides the fallback, so defaultSubGraphsForFields: [] on a type makes its fields opt in individually.

Inputs and missing types

Nullable arguments and input fields can also have subGraphs. Required arguments and input fields cannot be removed from a retained field or input object: resolvers may depend on them being present. The plugin rejects a schema that tries to remove one.

An output field is omitted when its return type is excluded. Other references can make a filtered schema invalid, such as a retained union containing an excluded member or a required argument using an excluded input type. Include those dependencies in the variant or exclude the referring field or type as well. Build and validate each variant you intend to serve.

Include unreachable types

Types that remain unreachable after filtering are normally omitted. The subGraphs.explicitlyIncludeType callback retains matching types that already belong to the selected sub-graph, even when no field reaches them.

For federation entities, hasResolvableKey can retain an otherwise unreachable external reference. Use this as a separate builder setup with both plugins:

import SchemaBuilder from '@pothos/core';
import FederationPlugin, { hasResolvableKey } from '@pothos/plugin-federation';
import SubGraphPlugin from '@pothos/plugin-sub-graph';

const builder = new SchemaBuilder<{
  SubGraphs: 'Public' | 'Internal';
}>({
  plugins: [SubGraphPlugin, FederationPlugin],
  subGraphs: {
    defaultForTypes: ['Public', 'Internal'],
    fieldsInheritFromTypes: true,
    explicitlyIncludeType: (type) => hasResolvableKey(type),
  },
});