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

@pothos/plugin-grafast

v0.1.1

Published

A Pothos plugin for grafast plan resolvers

Readme

Grafast plugin for Pothos

⚠️ Experimental Package ⚠️

This package is currently experimental and will have breaking changes in the near future.

This plugin currently does not work with MOST other Pothos plugins. Many plugins depend on wrapping resolvers to add runtime functionality to your schema, which will not work with grafast.

Install

npm install --save @pothos/plugin-grafast grafast@>=0.1.1-beta.24

Setup

import GrafastPlugin from '@pothos/plugin-grafast';

declare global {
  namespace Grafast {
    // Define the Context type used by grafast
    interface Context extends YourContextType {}
  }
}

type BuilderTypes = {
  // This tells the builder to expect plans instead of resolvers
  InferredFieldOptionsKind: 'Grafast';
  Context: YourContextType;
};

const builder = new SchemaBuilder<BuilderTypes>({
  plugins: [GrafastPlugin],
});

Usage

For documentation on how to write plans, see the Grafast documentation.

Adding plans to fields

builder.queryType({
  fields: (t) => ({
    addTwoNumbers: t.int({
      args: {
        a: t.arg.int({ required: true }),
        b: t.arg.int({ required: true }),
      },
      plan: (_, { $a, $b }) => {
        return lambda([$a, $b], ([a, b]) => a + b);
      },
    }),
  }),
});

Using resolvers

Pothos and Grafast will still allow you to write resolvers when using grafast, but you will not have access to the 4th GraphqlResolveInfo argument:

builder.queryType({
  fields: (t) => ({
    addTwoNumbers: t.int({
      args: {
        a: t.arg.int({ required: true }),
        b: t.arg.int({ required: true }),
      },
      resolve: (_, { a, b }) => {
        return a + b;
      },
    }),
  }),
});

Resolvers should not be used to load data, but can make it easier to define a field that would otherwise use a simple lambda plan.

Abstract types

Abstract types (Unions and Interfaces) may require defining a plan to resolve to the correct type. For more details on how polymorphic types work in Grafast, see the Grafast documentation.

Interfaces

To implement an interface, you can implement it as you normally would in Pothos, and then call the .withPlan method on the interface ref to provide a plan for resolving the correct type.

interface AnimalData {
  id: string;
  kind: 'Dog' | 'Cat';
}

export const Dog = builder.objectRef<AnimalData>('Dog').implement({
  interfaces: [Animal],
});
export const Cat = builder.objectRef<AnimalData>('Cat').implement({
  interfaces: [Animal],
});

export const Animal = builder
  .interfaceRef<AnimalData>('Animal')
  .withPlan({
    planType: ($record) => ({
      $__typename: get($record, 'kind'),
    }),
  })

Animal.implement({
  fields: (t) => ({
    id: t.exposeID('id'),
  }),
});

You can now define a query to resolve this interface:

export const Animals = [
  {
    id: '1',
    kind: 'Dog',
  },
  {
    id: '2',
    kind: 'Cat',
  },
] satisfies AnimalData[];

function getAnimalsById(ids: readonly string[]): (AnimalData | null)[] {
  return ids.map((id) => Animals.find((entity) => entity.id === id) ?? null);
}

builder.queryFields((t) => ({
  animal: t.field({
    type: Animal,
    args: {
      id: t.arg.string({ required: true }),
    },
    plan: (_, $args) => loadOne($args.$id, getAnimalsById),
  }),
}));

Unions

Unions can be implemented just like interfaces:

interface AlienData {
  id: string;
  kind: 'Alien';
}

export const Alien = builder.objectRef<AlienData>('Alien').implement({
  fields: (t) => ({
    id: t.exposeID('id'),
  }),
});

export const Entity = builder
  .unionType('Entity', {
    types: [Cat, Dog, Alien],
  })
  .withPlan({
    planType: ($record) => ({
      $__typename: get($record, 'kind'),
    }),
  });

planForType

When planning polymorphic types, Grafast allows you to provide a planForType function that allows you to load the correct data for the current type.

This also enables changing the type of plan required for fields that return the abstract type:

This API is likely to change in the future.

export const Entity = builder
  .unionType('Entity', {
    types: [Cat, Dog, Alien],
  })
  .withPlan({
    planType: (
      // Provide an explicit type so that the query field only needs to return the ID
      $specifier: Step<string>,
    ) => {
      const $record = inhibitOnNull(loadOne($specifier, getEntitiesById));
      return {
        $__typename: get($record, 'kind'),
        planForType: () => $record,
      };
    },
  });

builder.queryFields((t) => ({
  entity: t.field({
    type: Entity,
    args: {
      id: t.arg.string({ required: true }),
    },
    // Because our Entity plan loads the record, we can just return the ID here
    plan: (_, $args) => $args.$id,
  }),
}));