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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pothos/plugin-sub-graph

v4.2.0

Published

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

Downloads

16,047

Readme

SubGraph Plugin for Pothos

A plugin for creating sub-selections of your graph. This allows you to use the same code/types for multiple variants of your API.

One common use case for this is to share implementations between your public and internal APIs, by only exposing a subset of your graph publicly.

Usage

Install

yarn add @pothos/plugin-sub-graph

Setup

import SubGraphPlugin from '@pothos/plugin-sub-graph';
const builder = new SchemaBuilder<{
  SubGraphs: 'Public' | 'Internal';
}>({
  plugins: [SubGraphPlugin],
  subGraphs: {
    defaultForTypes: [],
    fieldsInheritFromTypes: true,
  },
});

//in another file:

const schema = builder.toSchema();
const publicSchema = builder.toSchema({ subGraph: 'Public' });
const internalSchema = builder.toSchema({ subGraph: 'Internal' });

// You can also build a graph using multiple subgraphs:
const combinedSchema = builder.toSchema({ subGraph: ['Internal', 'Public'] });

Options on Types

  • subGraphs: An optional array of sub-graph the type should be included in.

Object and Interface types:

  • defaultSubGraphsForFields: Default sub-graph for fields of the type to be included in.

Options on Fields

  • subGraphs: An optional array of sub-graph the field to be included in. If not provided, will

    fallback to:

    • defaultSubGraphsForFields if set on type
    • subGraphs of the type if subGraphs.fieldsInheritFromTypes was set in the builder
    • an empty array

Options on Builder

  • subGraphs.defaultForTypes: Specifies what sub-graph a type is part of by default.
  • subGraphs.fieldsInheritFromTypes: defaults to false. When true, fields on a type will default to being part of the same sub-graph as their parent type. Only applies when type does not have defaultSubGraphsForFields set.

Usage

builder.queryType({
  // Query type will be available in default, Public, and Internal schemas
  subGraphs: ['Public', 'Internal'],
  // Fields on the Query object will now default to not being a part of any subgraph
  defaultSubGraphsForFields: [];
  fields: (t) => ({
    someField: t.string({
      // someField will be in the default schema and "Internal" sub graph, but
      // not present in the Public sub graph
      subGraphs: ['Internal']
      resolve: () => {
        throw new Error('Not implemented');
      },
    }),
  }),
});

Missing types

When creating a sub-graph, the plugin will only copy in types that are included in the sub-graph, either by explicitly setting it on the type, or because the sub-graph is included in the default list. Like types, output fields that are not included in a sub-graph will also be omitted. Arguments and fields on Input types can not be removed because that would break assumptions about argument types in resolvers.

If a type that is not included in the sub-graph is referenced by another part of the graph that is included in the graph, a runtime error will be thrown when the sub graph is constructed. This can happen in a number of cases including cases where a removed type is used in the interfaces of an object, a member of a union, or the type of a field argument.

Explicitly including types

You can use the explicitlyIncludeType option to explicitly include types in a sub-graph that are unreachable. This isn't normally required, but there are some edge cases where this may be useful.

For instance, when extending external references with the federation plugin, the externalRef may not be reachable directly through your schema, but you may still want to include it when building the schema. To work around this, we can explicitly include any any types that have a key directive:

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: {
    explicitlyIncludeType: (type, subGraphs) => hasResolvableKey(type)
  },
});